ResponseBulkHandler.php 980 B

123456789101112131415161718192021222324252627282930313233343536373839
  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\Protocol\Text;
  11. use Predis\Helpers;
  12. use Predis\Protocol\IResponseHandler;
  13. use Predis\Protocol\ProtocolException;
  14. use Predis\Network\IConnectionComposable;
  15. class ResponseBulkHandler implements IResponseHandler
  16. {
  17. public function handle(IConnectionComposable $connection, $lengthString)
  18. {
  19. $length = (int) $lengthString;
  20. if ($length != $lengthString) {
  21. Helpers::onCommunicationException(new ProtocolException(
  22. $connection, "Cannot parse '$length' as data length"
  23. ));
  24. }
  25. if ($length >= 0) {
  26. return substr($connection->readBytes($length + 2), 0, -2);
  27. }
  28. if ($length == -1) {
  29. return null;
  30. }
  31. }
  32. }