Response.php 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Muzich\CoreBundle\lib\Api;
  3. class Response
  4. {
  5. protected $content = array();
  6. public function __construct($content)
  7. {
  8. $this->content = $content;
  9. }
  10. public function haveNot($searched)
  11. {
  12. return !$this->have($searched);
  13. }
  14. public function have($searched, $not_empty = true, $content = null)
  15. {
  16. if (!$content)
  17. $content = $this->content;
  18. if (is_array($searched))
  19. {
  20. foreach ($searched as $searched_key => $searched_subvalue)
  21. {
  22. return $this->have($searched_subvalue, $not_empty, $this->get($searched_key, false, $content));
  23. }
  24. }
  25. if (array_key_exists($searched, $content))
  26. {
  27. if ($not_empty)
  28. {
  29. if ((is_null($content[$searched]) || !count($content[$searched]) || !$content[$searched]) && ($content[$searched] !== 0 && $content[$searched] !== '0'))
  30. {
  31. return false;
  32. }
  33. if (is_string($content[$searched]))
  34. {
  35. if (trim($content[$searched]) == '')
  36. {
  37. return false;
  38. }
  39. }
  40. }
  41. return true;
  42. }
  43. return false;
  44. }
  45. public function get($searched, $return_null_if_empty = true, $content = null)
  46. {
  47. if (!$content)
  48. $content = $this->content;
  49. if (is_array($searched))
  50. {
  51. foreach ($searched as $searched_key => $searched_subvalue)
  52. {
  53. if ($this->have($searched_key, true, $content))
  54. {
  55. return $this->get($searched_subvalue, $return_null_if_empty, $this->get($searched_key, $return_null_if_empty, $content));
  56. }
  57. else
  58. {
  59. return null;
  60. }
  61. }
  62. }
  63. if ($this->have($searched, $return_null_if_empty, $content))
  64. {
  65. return $content[$searched];
  66. }
  67. return null;
  68. }
  69. public function getContent()
  70. {
  71. return $this->content;
  72. }
  73. }