Response.php 1.9KB

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