PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/application/Espo/Entities/Integration.php

https://gitlab.com/johanlindberg/irvato-crm
PHP | 179 lines | 131 code | 21 blank | 27 comment | 33 complexity | db784418a0fb863b3be22b72ee3cd5d2 MD5 | raw file
  1. <?php
  2. /************************************************************************
  3. * This file is part of EspoCRM.
  4. *
  5. * EspoCRM - Open Source CRM application.
  6. * Copyright (C) 2014-2015 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
  7. * Website: http://www.espocrm.com
  8. *
  9. * EspoCRM is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * EspoCRM is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with EspoCRM. If not, see http://www.gnu.org/licenses/.
  21. *
  22. * The interactive user interfaces in modified source and object code versions
  23. * of this program must display Appropriate Legal Notices, as required under
  24. * Section 5 of the GNU General Public License version 3.
  25. *
  26. * In accordance with Section 7(b) of the GNU General Public License version 3,
  27. * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
  28. ************************************************************************/
  29. namespace Espo\Entities;
  30. class Integration extends \Espo\Core\ORM\Entity
  31. {
  32. public function get($name)
  33. {
  34. if ($name == 'id') {
  35. return $this->id;
  36. }
  37. if ($this->hasField($name)) {
  38. if (array_key_exists($name, $this->valuesContainer)) {
  39. return $this->valuesContainer[$name];
  40. }
  41. } else {
  42. if ($this->get('data')) {
  43. $data = $this->get('data');
  44. } else {
  45. $data = new \stdClass();
  46. }
  47. if (isset($data->$name)) {
  48. return $data->$name;
  49. }
  50. }
  51. return null;
  52. }
  53. public function clear($name)
  54. {
  55. parent::clear($name);
  56. $data = $this->get('data');
  57. if (empty($data)) {
  58. $data = new \stdClass();
  59. }
  60. unset($data->$name);
  61. $this->set('data', $data);
  62. }
  63. public function set($p1, $p2 = null)
  64. {
  65. if (is_array($p1)) {
  66. if ($p2 === null) {
  67. $p2 = false;
  68. }
  69. $this->populateFromArray($p1, $p2);
  70. return;
  71. }
  72. $name = $p1;
  73. $value = $p2;
  74. if ($name == 'id') {
  75. $this->id = $value;
  76. return;
  77. }
  78. if ($this->hasField($name)) {
  79. $this->valuesContainer[$name] = $value;
  80. } else {
  81. $data = $this->get('data');
  82. if (empty($data)) {
  83. $data = new \stdClass();
  84. }
  85. $data->$name = $value;
  86. $this->set('data', $data);
  87. }
  88. }
  89. public function populateFromArray(array $arr, $onlyAccessible = true, $reset = false)
  90. {
  91. if ($reset) {
  92. $this->reset();
  93. }
  94. foreach ($arr as $field => $value) {
  95. if (is_string($field)) {
  96. if ($this->hasField($field)) {
  97. $fields = $this->getFields();
  98. $fieldDefs = $fields[$field];
  99. if (!is_null($value)) {
  100. switch ($fieldDefs['type']) {
  101. case self::VARCHAR:
  102. break;
  103. case self::BOOL:
  104. $value = ($value === 'true' || $value === '1' || $value === true);
  105. break;
  106. case self::INT:
  107. $value = intval($value);
  108. break;
  109. case self::FLOAT:
  110. $value = floatval($value);
  111. break;
  112. case self::JSON_ARRAY:
  113. $value = is_string($value) ? json_decode($value) : $value;
  114. if (!is_array($value)) {
  115. $value = null;
  116. }
  117. break;
  118. case self::JSON_OBJECT:
  119. $value = is_string($value) ? json_decode($value) : $value;
  120. if (!($value instanceof \stdClass) && !is_array($value)) {
  121. $value = null;
  122. }
  123. break;
  124. default:
  125. break;
  126. }
  127. }
  128. }
  129. $this->set($field, $value);
  130. }
  131. }
  132. }
  133. public function toArray()
  134. {
  135. $arr = array();
  136. if (isset($this->id)) {
  137. $arr['id'] = $this->id;
  138. }
  139. foreach ($this->fields as $field => $defs) {
  140. if ($field == 'id') {
  141. continue;
  142. }
  143. if ($field == 'data') {
  144. continue;
  145. }
  146. if ($this->has($field)) {
  147. $arr[$field] = $this->get($field);
  148. }
  149. }
  150. $data = $this->get('data');
  151. if (empty($data)) {
  152. $data = new \stdClass();
  153. }
  154. $dataArr = get_object_vars($data);
  155. $arr = array_merge($arr, $dataArr);
  156. return $arr;
  157. }
  158. }