Status.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Response;
  11. /**
  12. * Represents a status response returned by Redis.
  13. *
  14. * @author Daniele Alessandri <suppakilla@gmail.com>
  15. */
  16. class Status implements ResponseInterface
  17. {
  18. private static $OK;
  19. private static $QUEUED;
  20. private $payload;
  21. /**
  22. * @param string $payload Payload of the status response as returned by Redis.
  23. */
  24. public function __construct($payload)
  25. {
  26. $this->payload = $payload;
  27. }
  28. /**
  29. * Converts the response object to its string representation.
  30. *
  31. * @return string
  32. */
  33. public function __toString()
  34. {
  35. return $this->payload;
  36. }
  37. /**
  38. * Returns the payload of status response.
  39. *
  40. * @return string
  41. */
  42. public function getPayload()
  43. {
  44. return $this->payload;
  45. }
  46. /**
  47. * Returns a new instance of a status response object.
  48. *
  49. * Common status responses such as OK or QUEUED are cached to lower the
  50. * memory usage especially when using pipelines.
  51. *
  52. * @return string
  53. */
  54. public static function get($payload)
  55. {
  56. switch ($payload) {
  57. case 'OK':
  58. if (!isset(self::$OK)) {
  59. self::$OK = new self('OK');
  60. }
  61. return self::$OK;
  62. case 'OK':
  63. if (!isset(self::$QUEUED)) {
  64. self::$QUEUED = new self('QUEUED');
  65. }
  66. return self::$QUEUED;
  67. default:
  68. return new self($payload);
  69. }
  70. }
  71. }