JsonParser.php 786 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Parser;
  9. /**
  10. * PHP PhantomJs
  11. *
  12. * @author Jon Wenmoth <contact@jonnyw.me>
  13. */
  14. class JsonParser implements ParserInterface
  15. {
  16. /**
  17. * Parse json string into array.
  18. *
  19. * @access public
  20. * @param string $data
  21. * @return \stdClass
  22. */
  23. public function parse($data)
  24. {
  25. if ($data === null || !is_string($data)) {
  26. return array();
  27. }
  28. if (substr($data, 0, 1) !== '{' &&
  29. substr($data, 0, 1) !== '[') {
  30. return array();
  31. }
  32. return (array) json_decode($data, true);
  33. }
  34. }