AbstractModel.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Interfaces;
  3. /**
  4. *
  5. * Created by PhpStorm.
  6. * User: Tomasz Bielecki ( tomasz.bi@thurdata.com )
  7. * Date: 05.09.19
  8. * Time: 13:34
  9. * Class AbstractModel
  10. */
  11. abstract class AbstractModel
  12. {
  13. /**
  14. * @description storage other resources
  15. * @var array
  16. */
  17. protected $resources = [];
  18. /**
  19. * AbstractModel constructor.
  20. * @param array $data
  21. */
  22. public function __construct($data = [])
  23. {
  24. $this->fill($data);
  25. }
  26. /**
  27. * @param array $data
  28. * @return $this
  29. */
  30. public function fill($data = [])
  31. {
  32. foreach ($data as $key => $row)
  33. {
  34. $property = strtolower($key);
  35. /* method Name */
  36. $method = 'set' . ucfirst($property);
  37. /* property name */
  38. if (method_exists($this, $method)) {
  39. /**
  40. * set through method if exists
  41. *
  42. */
  43. $this->$method($row);
  44. } elseif (property_exists($this, $property)) {
  45. /**
  46. * set as property if exists
  47. */
  48. $this->{$property} = $row;
  49. } else {
  50. /**
  51. * add data to resources if property or methods doesnt exists
  52. */
  53. $this->addResource($property, $row);
  54. }
  55. }
  56. return $this;
  57. }
  58. /**
  59. * @param $name
  60. * @return |null
  61. */
  62. public function getDataResourceA($name)
  63. {
  64. foreach ($this->resources['a'] as $res) {
  65. if ($res['N'] === $name) {
  66. return $res['DATA'];
  67. }
  68. }
  69. return null;
  70. }
  71. /**
  72. * @param $name
  73. * @return array
  74. */
  75. public function getDataResourceACollection($name)
  76. {
  77. foreach ($this->resources['a'] as $res) {
  78. if ($res['N'] === $name) {
  79. $tmp[] = $res['DATA'];
  80. }
  81. }
  82. return $tmp;
  83. }
  84. /**
  85. * @return mixed
  86. */
  87. public function getAllDataResourcesAAttributes()
  88. {
  89. foreach ($this->resources['a'] as $res) {
  90. $tmp[$res['N']] = $res['DATA'];
  91. }
  92. return $tmp;
  93. }
  94. /**
  95. * @param $key
  96. * @param array $data
  97. * @return $this
  98. */
  99. public function addResource($key, $data = [])
  100. {
  101. $this->resources[$key] = $data;
  102. return $this;
  103. }
  104. }