janisbiz /
light-orm
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace Janisbiz\LightOrm\Entity; |
||
| 4 | |||
| 5 | class BaseEntity implements EntityInterface |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var array |
||
| 9 | */ |
||
| 10 | protected $primaryKeys = []; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected $primaryKeysAutoIncrement = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | protected $columns = []; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected $data = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $dataOriginal = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var bool $isNew |
||
| 34 | */ |
||
| 35 | protected $isNew = true; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var bool $isSaved |
||
| 39 | */ |
||
| 40 | protected $isSaved = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @deprecated 30.04.2019 Should have proper getters & setters |
||
| 44 | * |
||
| 45 | * @param string $name |
||
| 46 | * |
||
| 47 | * @return string|int|bool |
||
| 48 | */ |
||
| 49 | public function __get(string $name) |
||
| 50 | { |
||
| 51 | if (\array_key_exists($name, $this->data)) { |
||
| 52 | return $this->data[$name]; |
||
| 53 | } |
||
| 54 | |||
| 55 | return null; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @deprecated 30.04.2019 Should have proper getters & setters |
||
| 60 | * |
||
| 61 | * @param string $name |
||
| 62 | * @param string|int|bool $value |
||
| 63 | */ |
||
| 64 | public function __set(string $name, $value) |
||
| 65 | { |
||
| 66 | $this->data[$name] = $value; |
||
| 67 | |||
| 68 | /** Construct values, for update queries */ |
||
| 69 | if (!isset($this->dataOriginal[$name])) { |
||
| 70 | $this->dataOriginal[$name] = $this->data[$name]; |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param string $name |
||
| 76 | * @param array|null $arguments |
||
| 77 | * |
||
| 78 | * @throws EntityException |
||
| 79 | * @return bool|null|string|$this |
||
| 80 | */ |
||
| 81 | public function __call(string $name, ?array $arguments) |
||
| 82 | { |
||
| 83 | $methodName = \substr($name, 0, 3); |
||
| 84 | \preg_match_all('/[A-Z0-9][^A-Z0-9]*/', $name, $results); |
||
| 85 | |||
| 86 | switch ($methodName) { |
||
| 87 | case 'get': |
||
| 88 | if (\array_key_exists(\mb_strtolower(\implode('_', $results[0])), $this->data)) { |
||
| 89 | $variable = $this->data[\strtolower(\implode('_', $results[0]))]; |
||
| 90 | |||
| 91 | if ($arguments && $arguments[0] === true && \is_string($variable)) { |
||
|
0 ignored issues
–
show
|
|||
| 92 | /** Stripping tags, output for user */ |
||
| 93 | return \nl2br(\htmlspecialchars(\trim($variable), ENT_QUOTES, 'UTF-8')); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** Returning unchanged variable */ |
||
| 97 | return $variable; |
||
| 98 | } |
||
| 99 | |||
| 100 | break; |
||
| 101 | |||
| 102 | case 'set': |
||
| 103 | if (!isset($arguments[0]) || $arguments[0] === '') { |
||
| 104 | $arguments[0] = null; |
||
| 105 | } |
||
| 106 | |||
| 107 | $this->data[\strtolower(\implode('_', $results[0]))] = $arguments[0]; |
||
| 108 | |||
| 109 | return $this; |
||
| 110 | } |
||
| 111 | |||
| 112 | throw new EntityException(\sprintf('Call to undefined method %s::%s()', __CLASS__, $name)); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param null|string $key |
||
| 117 | * |
||
| 118 | * @return null|array|string|int|double |
||
| 119 | * @throws EntityException |
||
| 120 | */ |
||
| 121 | public function &data(?string $key = null) |
||
| 122 | { |
||
| 123 | if (null !== $key && \is_string($key)) { |
||
| 124 | if (!\array_key_exists($key, $this->data)) { |
||
| 125 | throw new EntityException(\sprintf('There is no key "%s" present in data!', $key)); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $this->data[$key]; |
||
| 129 | } |
||
| 130 | |||
| 131 | return $this->data; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param null|string $key |
||
| 136 | * |
||
| 137 | * @return null|array|string|int|double |
||
| 138 | * @throws EntityException |
||
| 139 | */ |
||
| 140 | public function &dataOriginal(?string $key = null) |
||
| 141 | { |
||
| 142 | if (null !== $key && \is_string($key)) { |
||
| 143 | if (!\array_key_exists($key, $this->data)) { |
||
| 144 | throw new EntityException(\sprintf('There is no key "%s" present in data original!', $key)); |
||
| 145 | } |
||
| 146 | |||
| 147 | return $this->data[$key]; |
||
| 148 | } |
||
| 149 | |||
| 150 | return $this->dataOriginal; |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | public function isNew(): bool |
||
| 157 | { |
||
| 158 | return $this->isNew; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public function isSaved(): bool |
||
| 165 | { |
||
| 166 | return $this->isSaved; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return string[] |
||
| 171 | */ |
||
| 172 | public function primaryKeys(): array |
||
| 173 | { |
||
| 174 | return $this->primaryKeys; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return string[] |
||
| 179 | */ |
||
| 180 | public function primaryKeysAutoIncrement(): array |
||
| 181 | { |
||
| 182 | return $this->primaryKeysAutoIncrement; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return string[] |
||
| 187 | */ |
||
| 188 | public function columns(): array |
||
| 189 | { |
||
| 190 | return $this->columns; |
||
| 191 | } |
||
| 192 | } |
||
| 193 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.