/src/plugins/orangehrmRESTPlugin/lib/Api/Model/ModelTrait.php

https://github.com/orangehrm/OrangeHRM · PHP · 118 lines · 59 code · 12 blank · 47 comment · 8 complexity · 4e68d7507c256ec6b45c18c8f390e98d MD5 · raw file

  1. <?php
  2. /**
  3. * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
  4. * all the essential functionalities required for any enterprise.
  5. * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
  6. *
  7. * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program;
  16. * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA
  18. */
  19. namespace Orangehrm\Rest\Api\Model;
  20. use Orangehrm\Rest\Api\Entity\Serializable;
  21. trait ModelTrait
  22. {
  23. /**
  24. * @var array
  25. */
  26. private $filter = [];
  27. /**
  28. * @var array
  29. */
  30. private $attributeNames = [];
  31. /**
  32. * @var null|Serializable|object
  33. */
  34. private $entity = null;
  35. /**
  36. * Set attribute names which should include to model
  37. * @param array $filter
  38. */
  39. public function setFilters(array $filter)
  40. {
  41. $this->filter = $filter;
  42. }
  43. /**
  44. * Override output array attributes
  45. * @param array $attributeNames
  46. */
  47. public function setAttributeNames(array $attributeNames)
  48. {
  49. $this->attributeNames = $attributeNames;
  50. }
  51. /**
  52. * Set Api entity class
  53. * @param Serializable|object $entity
  54. */
  55. public function setEntity($entity)
  56. {
  57. $this->entity = $entity;
  58. }
  59. /**
  60. * @return array
  61. */
  62. public function toArray()
  63. {
  64. $array = [];
  65. if (!is_null($this->entity)) {
  66. foreach ($this->filter as $index => $attribute) {
  67. if (is_array($attribute)) {
  68. $value = $this->entity;
  69. foreach ($attribute as $func) {
  70. if (!is_null($value)) {
  71. $value = call_user_func([$value, $func]);
  72. }
  73. }
  74. } else {
  75. // Only work with camel cased get methods with particular attribute
  76. $getMethodName = "get" . ucfirst($attribute);
  77. $value = $this->entity->$getMethodName();
  78. }
  79. $key = empty($this->attributeNames[$index]) ? $attribute :
  80. $this->attributeNames[$index];
  81. if (is_array($key)) {
  82. $array = array_merge_recursive($array, $this->makeNestedArray($key, $value));
  83. } else {
  84. $array[$key] = $value;
  85. }
  86. }
  87. }
  88. return $array;
  89. }
  90. /**
  91. * @param array $keys
  92. * @param $value
  93. * @return array
  94. */
  95. private function makeNestedArray(array $keys, $value): array
  96. {
  97. $array = [];
  98. $key = array_shift($keys);
  99. if (!isset($keys[0])) {
  100. $array[$key] = $value;
  101. } else {
  102. $array[$key] = $this->makeNestedArray($keys, $value);
  103. }
  104. return $array;
  105. }
  106. }