JsonFile.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace RocketTheme\Toolbox\File;
  3. /**
  4. * Implements Json File reader.
  5. *
  6. * @package RocketTheme\Toolbox\File
  7. * @author RocketTheme
  8. * @license MIT
  9. */
  10. class JsonFile extends File
  11. {
  12. /**
  13. * @var string
  14. */
  15. protected $extension = '.json';
  16. /**
  17. * @var array|File[]
  18. */
  19. static protected $instances = array();
  20. /**
  21. * Check contents and make sure it is in correct format.
  22. *
  23. * @param array $var
  24. * @return array
  25. */
  26. protected function check($var)
  27. {
  28. return (array) $var;
  29. }
  30. /**
  31. * Encode contents into RAW string.
  32. *
  33. * @param string $var
  34. * @param int $options
  35. * @return string
  36. */
  37. protected function encode($var, $options = 0)
  38. {
  39. return (string) json_encode($var, $options);
  40. }
  41. /**
  42. * Decode RAW string into contents.
  43. *
  44. * @param string $var
  45. * @param bool $assoc
  46. * @return array mixed
  47. */
  48. protected function decode($var, $assoc = false)
  49. {
  50. return (array) json_decode($var, $assoc);
  51. }
  52. }