JsonEofPacker.php 809 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\JsonRpc\Packer;
  12. use Hyperf\Contract\PackerInterface;
  13. class JsonEofPacker implements PackerInterface
  14. {
  15. protected string $eof;
  16. public function __construct(array $options = [])
  17. {
  18. $this->eof = $options['settings']['package_eof'] ?? "\r\n";
  19. }
  20. public function pack($data): string
  21. {
  22. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  23. return $data . $this->eof;
  24. }
  25. public function unpack(string $data)
  26. {
  27. $data = rtrim($data, $this->eof);
  28. return json_decode($data, true);
  29. }
  30. }