Complex classes like Entity often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Entity, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | abstract class Entity implements \Serializable |
||
| 31 | { |
||
| 32 | /** @deprecated Use Relation::OPT_CLASS instead */ |
||
| 33 | const OPT_RELATION_CLASS = 'class'; |
||
| 34 | /** @deprecated Use Relation::OPT_CARDINALITY instead */ |
||
| 35 | const OPT_RELATION_CARDINALITY = 'cardinality'; |
||
| 36 | /** @deprecated Use Relation::OPT_REFERENCE instead */ |
||
| 37 | const OPT_RELATION_REFERENCE = 'reference'; |
||
| 38 | /** @deprecated Use Relation::OPT_OPPONENT instead */ |
||
| 39 | const OPT_RELATION_OPPONENT = 'opponent'; |
||
| 40 | /** @deprecated Use Relation::OPT_TABLE instead */ |
||
| 41 | const OPT_RELATION_TABLE = 'table'; |
||
| 42 | |||
| 43 | /** The template to use to calculate the table name. |
||
| 44 | * @var string */ |
||
| 45 | protected static $tableNameTemplate; |
||
| 46 | |||
| 47 | /** The naming scheme to use for table names. |
||
| 48 | * @var string */ |
||
| 49 | protected static $namingSchemeTable; |
||
| 50 | |||
| 51 | /** The naming scheme to use for column names. |
||
| 52 | * @var string */ |
||
| 53 | protected static $namingSchemeColumn; |
||
| 54 | |||
| 55 | /** The naming scheme to use for method names. |
||
| 56 | * @var string */ |
||
| 57 | protected static $namingSchemeMethods; |
||
| 58 | |||
| 59 | /** Fixed table name (ignore other settings) |
||
| 60 | * @var string */ |
||
| 61 | protected static $tableName; |
||
| 62 | |||
| 63 | /** The variable(s) used for primary key. |
||
| 64 | * @var string[]|string */ |
||
| 65 | protected static $primaryKey = ['id']; |
||
| 66 | |||
| 67 | /** Fixed column names (ignore other settings) |
||
| 68 | * @var string[] */ |
||
| 69 | protected static $columnAliases = []; |
||
| 70 | |||
| 71 | /** A prefix for column names. |
||
| 72 | * @var string */ |
||
| 73 | protected static $columnPrefix; |
||
| 74 | |||
| 75 | /** Whether or not the primary key is auto incremented. |
||
| 76 | * @var bool */ |
||
| 77 | protected static $autoIncrement = true; |
||
| 78 | |||
| 79 | /** Whether or not the validator for this class is enabled. |
||
| 80 | * @var bool */ |
||
| 81 | protected static $enableValidator = false; |
||
| 82 | |||
| 83 | /** Whether or not the validator for a class got enabled during runtime. |
||
| 84 | * @var bool[] */ |
||
| 85 | protected static $enabledValidators = []; |
||
| 86 | |||
| 87 | /** Relation definitions |
||
| 88 | * @var array */ |
||
| 89 | protected static $relations = []; |
||
| 90 | |||
| 91 | /** The reflections of the classes. |
||
| 92 | * @internal |
||
| 93 | * @var \ReflectionClass[] */ |
||
| 94 | protected static $reflections = []; |
||
| 95 | |||
| 96 | /** The current data of a row. |
||
| 97 | * @var mixed[] */ |
||
| 98 | protected $data = []; |
||
| 99 | |||
| 100 | /** The original data of the row. |
||
| 101 | * @var mixed[] */ |
||
| 102 | protected $originalData = []; |
||
| 103 | |||
| 104 | /** The entity manager from which this entity got created |
||
| 105 | * @var EM */ |
||
| 106 | protected $entityManager; |
||
| 107 | |||
| 108 | /** Related objects for getRelated |
||
| 109 | * @var array */ |
||
| 110 | protected $relatedObjects = []; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Constructor |
||
| 114 | * |
||
| 115 | * It calls ::onInit() after initializing $data and $originalData. |
||
| 116 | * |
||
| 117 | * @param mixed[] $data The current data |
||
| 118 | * @param EM $entityManager The EntityManager that created this entity |
||
| 119 | * @param bool $fromDatabase Whether or not the data comes from database |
||
| 120 | */ |
||
| 121 | 112 | final public function __construct(array $data = [], EM $entityManager = null, $fromDatabase = false) |
|
| 122 | { |
||
| 123 | 112 | if ($fromDatabase) { |
|
| 124 | 14 | $this->originalData = $data; |
|
| 125 | } |
||
| 126 | 112 | $this->data = array_merge($this->data, $data); |
|
| 127 | 112 | $this->entityManager = $entityManager ?: EM::getInstance(static::class); |
|
| 128 | 112 | $this->onInit(!$fromDatabase); |
|
| 129 | 112 | } |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Get a description for this table. |
||
| 133 | * |
||
| 134 | * @return Table|Column[] |
||
| 135 | * @codeCoverageIgnore This is just a proxy |
||
| 136 | */ |
||
| 137 | public static function describe() |
||
| 138 | { |
||
| 139 | return EM::getInstance(static::class)->describe(static::getTableName()); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Get the column name of $attribute |
||
| 144 | * |
||
| 145 | * The column names can not be specified by template. Instead they are constructed by $columnPrefix and enforced |
||
| 146 | * to $namingSchemeColumn. |
||
| 147 | * |
||
| 148 | * **ATTENTION**: If your overwrite this method remember that getColumnName(getColumnName($name)) have to be exactly |
||
| 149 | * the same as getColumnName($name). |
||
| 150 | * |
||
| 151 | * @param string $attribute |
||
| 152 | * @return string |
||
| 153 | * @throws InvalidConfiguration |
||
| 154 | */ |
||
| 155 | 148 | public static function getColumnName($attribute) |
|
| 156 | { |
||
| 157 | 148 | if (isset(static::$columnAliases[$attribute])) { |
|
| 158 | 6 | return static::$columnAliases[$attribute]; |
|
| 159 | } |
||
| 160 | |||
| 161 | 146 | return EM::getInstance(static::class)->getNamer() |
|
| 162 | 146 | ->getColumnName(static::class, $attribute, static::$columnPrefix, static::$namingSchemeColumn); |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Get the primary key vars |
||
| 167 | * |
||
| 168 | * The primary key can consist of multiple columns. You should configure the vars that are translated to these |
||
| 169 | * columns. |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | 56 | public static function getPrimaryKeyVars() |
|
| 174 | { |
||
| 175 | 56 | return !is_array(static::$primaryKey) ? [static::$primaryKey] : static::$primaryKey; |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Get the definition for $relation |
||
| 180 | * |
||
| 181 | * It normalize the short definition form and create a Relation object from it. |
||
| 182 | * |
||
| 183 | * @param string $relation |
||
| 184 | * @return Relation |
||
| 185 | * @throws InvalidConfiguration |
||
| 186 | * @throws UndefinedRelation |
||
| 187 | */ |
||
| 188 | 85 | public static function getRelation($relation) |
|
| 189 | { |
||
| 190 | 85 | if (!isset(static::$relations[$relation])) { |
|
| 191 | 3 | throw new UndefinedRelation('Relation ' . $relation . ' is not defined'); |
|
| 192 | } |
||
| 193 | |||
| 194 | 84 | $relDef = &static::$relations[$relation]; |
|
| 195 | |||
| 196 | 84 | if (!$relDef instanceof Relation) { |
|
| 197 | 15 | $relDef = Relation::createRelation($relation, $relDef); |
|
| 198 | } |
||
| 199 | |||
| 200 | 83 | return $relDef; |
|
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get the table name |
||
| 205 | * |
||
| 206 | * The table name is constructed by $tableNameTemplate and $namingSchemeTable. It can be overwritten by |
||
| 207 | * $tableName. |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | * @throws InvalidName|InvalidConfiguration |
||
| 211 | */ |
||
| 212 | 147 | public static function getTableName() |
|
| 213 | { |
||
| 214 | 147 | if (static::$tableName) { |
|
| 215 | 11 | return static::$tableName; |
|
| 216 | } |
||
| 217 | |||
| 218 | 136 | return EM::getInstance(static::class)->getNamer() |
|
| 219 | 136 | ->getTableName(static::class, static::$tableNameTemplate, static::$namingSchemeTable); |
|
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Check if the table has a auto increment column |
||
| 224 | * |
||
| 225 | * @return bool |
||
| 226 | */ |
||
| 227 | 14 | public static function isAutoIncremented() |
|
| 228 | { |
||
| 229 | 14 | return count(static::getPrimaryKeyVars()) > 1 ? false : static::$autoIncrement; |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Check if the validator is enabled |
||
| 234 | * |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 34 | public static function isValidatorEnabled() |
|
| 238 | { |
||
| 239 | 34 | return isset(self::$enabledValidators[static::class]) ? |
|
| 240 | 34 | self::$enabledValidators[static::class] : static::$enableValidator; |
|
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Enable validator |
||
| 245 | * |
||
| 246 | * @param bool $enable |
||
| 247 | */ |
||
| 248 | 8 | public static function enableValidator($enable = true) |
|
| 249 | { |
||
| 250 | 8 | self::$enabledValidators[static::class] = $enable; |
|
| 251 | 8 | } |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Disable validator |
||
| 255 | * |
||
| 256 | * @param bool $disable |
||
| 257 | */ |
||
| 258 | 33 | public static function disableValidator($disable = true) |
|
| 259 | { |
||
| 260 | 33 | self::$enabledValidators[static::class] = !$disable; |
|
| 261 | 33 | } |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Validate $value for $attribute |
||
| 265 | * |
||
| 266 | * @param string $attribute |
||
| 267 | * @param mixed $value |
||
| 268 | * @return bool|Error |
||
| 269 | * @throws Exception |
||
| 270 | */ |
||
| 271 | 10 | public static function validate($attribute, $value) |
|
| 272 | { |
||
| 273 | 10 | return static::describe()->validate(static::getColumnName($attribute), $value); |
|
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Validate $data |
||
| 278 | * |
||
| 279 | * $data has to be an array of $attribute => $value |
||
| 280 | * |
||
| 281 | * @param array $data |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | 1 | public static function validateArray(array $data) |
|
| 285 | { |
||
| 286 | 1 | $result = $data; |
|
| 287 | 1 | foreach ($result as $attribute => &$value) { |
|
| 288 | 1 | $value = static::validate($attribute, $value); |
|
| 289 | } |
||
| 290 | 1 | return $result; |
|
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param EM $entityManager |
||
| 295 | * @return self |
||
| 296 | */ |
||
| 297 | 2 | public function setEntityManager(EM $entityManager) |
|
| 298 | { |
||
| 299 | 2 | $this->entityManager = $entityManager; |
|
| 300 | 2 | return $this; |
|
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Get the value from $attribute |
||
| 305 | * |
||
| 306 | * If there is a custom getter this method get called instead. |
||
| 307 | * |
||
| 308 | * @param string $attribute The variable to get |
||
| 309 | * @return mixed|null |
||
| 310 | * @throws IncompletePrimaryKey |
||
| 311 | * @throws InvalidConfiguration |
||
| 312 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 313 | */ |
||
| 314 | 96 | public function __get($attribute) |
|
| 315 | { |
||
| 316 | 96 | $em = EM::getInstance(static::class); |
|
| 317 | 96 | $getter = $em->getNamer()->getMethodName('get' . ucfirst($attribute), self::$namingSchemeMethods); |
|
| 318 | |||
| 319 | 96 | if (method_exists($this, $getter) && is_callable([$this, $getter])) { |
|
| 320 | 4 | return $this->$getter(); |
|
| 321 | } else { |
||
| 322 | 92 | $col = static::getColumnName($attribute); |
|
| 323 | 92 | $result = isset($this->data[$col]) ? $this->data[$col] : null; |
|
| 324 | |||
| 325 | 92 | if (!$result && isset(static::$relations[$attribute]) && isset($this->entityManager)) { |
|
| 326 | 1 | return $this->getRelated($attribute); |
|
| 327 | } |
||
| 328 | |||
| 329 | 91 | return $result; |
|
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Set $attribute to $value |
||
| 335 | * |
||
| 336 | * Tries to call custom setter before it stores the data directly. If there is a setter the setter needs to store |
||
| 337 | * data that should be updated in the database to $data. Do not store data in $originalData as it will not be |
||
| 338 | * written and give wrong results for dirty checking. |
||
| 339 | * |
||
| 340 | * The onChange event is called after something got changed. |
||
| 341 | * |
||
| 342 | * The method throws an error when the validation fails (also when the column does not exist). |
||
| 343 | * |
||
| 344 | * @param string $attribute The variable to change |
||
| 345 | * @param mixed $value The value to store |
||
| 346 | * @throws Error |
||
| 347 | * @link https://tflori.github.io/orm/entities.html Working with entities |
||
| 348 | */ |
||
| 349 | 33 | public function __set($attribute, $value) |
|
| 350 | { |
||
| 351 | 33 | $col = $this->getColumnName($attribute); |
|
| 352 | |||
| 353 | 33 | $em = EM::getInstance(static::class); |
|
| 354 | 33 | $setter = $em->getNamer()->getMethodName('set' . ucfirst($attribute), self::$namingSchemeMethods); |
|
| 355 | |||
| 356 | 33 | if (method_exists($this, $setter) && is_callable([$this, $setter])) { |
|
| 357 | 3 | $oldValue = $this->__get($attribute); |
|
| 358 | 3 | $md5OldData = md5(serialize($this->data)); |
|
| 359 | 3 | $this->$setter($value); |
|
| 360 | 3 | $changed = $md5OldData !== md5(serialize($this->data)); |
|
| 361 | } else { |
||
| 362 | 30 | if (static::isValidatorEnabled() && |
|
| 363 | 30 | ($error = static::validate($attribute, $value)) instanceof Error |
|
| 364 | ) { |
||
| 365 | 1 | throw $error; |
|
| 366 | } |
||
| 367 | |||
| 368 | 26 | $oldValue = $this->__get($attribute); |
|
| 369 | 26 | $changed = (isset($this->data[$col]) ? $this->data[$col] : null) !== $value; |
|
| 370 | 26 | $this->data[$col] = $value; |
|
| 371 | } |
||
| 372 | |||
| 373 | 29 | if ($changed) { |
|
| 374 | 26 | $this->onChange($attribute, $oldValue, $this->__get($attribute)); |
|
| 375 | } |
||
| 376 | 29 | } |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Fill the entity with $data |
||
| 380 | * |
||
| 381 | * When $checkMissing is set to true it also proves that the absent columns are nullable. |
||
| 382 | * |
||
| 383 | * @param array $data |
||
| 384 | * @param bool $ignoreUnknown |
||
| 385 | * @param bool $checkMissing |
||
| 386 | * @throws Error |
||
| 387 | * @throws UnknownColumn |
||
| 388 | */ |
||
| 389 | 4 | public function fill(array $data, $ignoreUnknown = false, $checkMissing = false) |
|
| 390 | { |
||
| 391 | 4 | foreach ($data as $attribute => $value) { |
|
| 392 | try { |
||
| 393 | 4 | $this->__set($attribute, $value); |
|
| 394 | 2 | } catch (UnknownColumn $e) { |
|
| 395 | 2 | if (!$ignoreUnknown) { |
|
| 396 | 4 | throw $e; |
|
| 397 | } |
||
| 398 | } |
||
| 399 | } |
||
| 400 | |||
| 401 | 3 | if ($checkMissing && is_array($errors = $this->isValid())) { |
|
| 402 | 1 | throw $errors[0]; |
|
| 403 | } |
||
| 404 | 2 | } |
|
| 405 | |||
| 406 | /** |
||
| 407 | * Get related objects |
||
| 408 | * |
||
| 409 | * The difference between getRelated and fetch is that getRelated stores the fetched entities. To refresh set |
||
| 410 | * $refresh to true. |
||
| 411 | * |
||
| 412 | * @param string $relation |
||
| 413 | * @param bool $refresh |
||
| 414 | * @return mixed |
||
| 415 | * @throws Exception\NoConnection |
||
| 416 | * @throws Exception\NoEntity |
||
| 417 | * @throws IncompletePrimaryKey |
||
| 418 | * @throws InvalidConfiguration |
||
| 419 | * @throws NoEntityManager |
||
| 420 | * @throws UndefinedRelation |
||
| 421 | */ |
||
| 422 | 11 | public function getRelated($relation, $refresh = false) |
|
| 423 | { |
||
| 424 | 11 | if ($refresh || !isset($this->relatedObjects[$relation])) { |
|
| 425 | 9 | $this->relatedObjects[$relation] = $this->fetch($relation, true); |
|
| 426 | } |
||
| 427 | |||
| 428 | 11 | return $this->relatedObjects[$relation]; |
|
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Set $relation to $entity |
||
| 433 | * |
||
| 434 | * This method is only for the owner of a relation. |
||
| 435 | * |
||
| 436 | * @param string $relation |
||
| 437 | * @param Entity $entity |
||
| 438 | * @throws IncompletePrimaryKey |
||
| 439 | * @throws InvalidRelation |
||
| 440 | */ |
||
| 441 | 7 | public function setRelated($relation, Entity $entity = null) |
|
| 442 | { |
||
| 443 | 7 | $this::getRelation($relation)->setRelated($this, $entity); |
|
| 444 | |||
| 445 | 4 | $this->relatedObjects[$relation] = $entity; |
|
| 446 | 4 | } |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Add relations for $relation to $entities |
||
| 450 | * |
||
| 451 | * This method is only for many-to-many relations. |
||
| 452 | * |
||
| 453 | * This method does not take care about already existing relations and will fail hard. |
||
| 454 | * |
||
| 455 | * @param string $relation |
||
| 456 | * @param Entity[] $entities |
||
| 457 | * @throws NoEntityManager |
||
| 458 | */ |
||
| 459 | 8 | public function addRelated($relation, array $entities) |
|
| 460 | { |
||
| 461 | // @codeCoverageIgnoreStart |
||
| 462 | if (func_num_args() === 3 && func_get_arg(2) instanceof EM) { |
||
| 463 | trigger_error( |
||
| 464 | 'Passing EntityManager to addRelated is deprecated. Use ->setEntityManager() to overwrite', |
||
| 465 | E_USER_DEPRECATED |
||
| 466 | ); |
||
| 467 | } |
||
| 468 | // @codeCoverageIgnoreEnd |
||
| 469 | |||
| 470 | 8 | $this::getRelation($relation)->addRelated($this, $entities, $this->entityManager); |
|
| 471 | 4 | } |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Delete relations for $relation to $entities |
||
| 475 | * |
||
| 476 | * This method is only for many-to-many relations. |
||
| 477 | * |
||
| 478 | * @param string $relation |
||
| 479 | * @param Entity[] $entities |
||
| 480 | * @throws NoEntityManager |
||
| 481 | */ |
||
| 482 | 8 | public function deleteRelated($relation, $entities) |
|
| 483 | { |
||
| 484 | // @codeCoverageIgnoreStart |
||
| 485 | if (func_num_args() === 3 && func_get_arg(2) instanceof EM) { |
||
| 486 | trigger_error( |
||
| 487 | 'Passing EntityManager to deleteRelated is deprecated. Use ->setEntityManager() to overwrite', |
||
| 488 | E_USER_DEPRECATED |
||
| 489 | ); |
||
| 490 | } |
||
| 491 | // @codeCoverageIgnoreEnd |
||
| 492 | |||
| 493 | 8 | $this::getRelation($relation)->deleteRelated($this, $entities, $this->entityManager); |
|
| 494 | 4 | } |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Resets the entity or $attribute to original data |
||
| 498 | * |
||
| 499 | * @param string $attribute Reset only this variable or all variables |
||
| 500 | * @throws InvalidConfiguration |
||
| 501 | */ |
||
| 502 | 8 | public function reset($attribute = null) |
|
| 503 | { |
||
| 504 | 8 | if (!empty($attribute)) { |
|
| 505 | 3 | $col = static::getColumnName($attribute); |
|
| 506 | 3 | if (isset($this->originalData[$col])) { |
|
| 507 | 2 | $this->data[$col] = $this->originalData[$col]; |
|
| 508 | } else { |
||
| 509 | 1 | unset($this->data[$col]); |
|
| 510 | } |
||
| 511 | 3 | return; |
|
| 512 | } |
||
| 513 | |||
| 514 | 5 | $this->data = $this->originalData; |
|
| 515 | 5 | } |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Save the entity to EntityManager |
||
| 519 | * |
||
| 520 | * @return Entity |
||
| 521 | * @throws Exception\NoConnection |
||
| 522 | * @throws Exception\NoEntity |
||
| 523 | * @throws Exception\NotScalar |
||
| 524 | * @throws Exception\UnsupportedDriver |
||
| 525 | * @throws IncompletePrimaryKey |
||
| 526 | * @throws InvalidConfiguration |
||
| 527 | * @throws InvalidName |
||
| 528 | * @throws NoEntityManager |
||
| 529 | */ |
||
| 530 | 10 | public function save() |
|
| 531 | { |
||
| 532 | // @codeCoverageIgnoreStart |
||
| 533 | if (func_num_args() === 1 && func_get_arg(0) instanceof EM) { |
||
| 534 | trigger_error( |
||
| 535 | 'Passing EntityManager to save is deprecated. Use ->setEntityManager() to overwrite', |
||
| 536 | E_USER_DEPRECATED |
||
| 537 | ); |
||
| 538 | } |
||
| 539 | // @codeCoverageIgnoreEnd |
||
| 540 | |||
| 541 | 10 | $inserted = false; |
|
| 542 | 10 | $updated = false; |
|
| 543 | |||
| 544 | try { |
||
| 545 | // this may throw if the primary key is auto incremented but we using this to omit duplicated code |
||
| 546 | 10 | if (!$this->entityManager->sync($this)) { |
|
| 547 | 1 | $this->prePersist(); |
|
| 548 | 1 | $inserted = $this->entityManager->insert($this, false); |
|
| 549 | 4 | } elseif ($this->isDirty()) { |
|
| 550 | 3 | $this->preUpdate(); |
|
| 551 | 5 | $updated = $this->entityManager->update($this); |
|
| 552 | } |
||
| 553 | 5 | } catch (IncompletePrimaryKey $e) { |
|
| 554 | 5 | if (static::isAutoIncremented()) { |
|
| 555 | 4 | $this->prePersist(); |
|
| 556 | 4 | $inserted = $this->entityManager->insert($this); |
|
| 557 | } else { |
||
| 558 | 1 | throw $e; |
|
| 559 | } |
||
| 560 | } |
||
| 561 | |||
| 562 | 9 | $inserted && $this->postPersist(); |
|
| 563 | 9 | $updated && $this->postUpdate(); |
|
| 564 | |||
| 565 | 9 | return $this; |
|
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Checks if entity or $attribute got changed |
||
| 570 | * |
||
| 571 | * @param string $attribute Check only this variable or all variables |
||
| 572 | * @return bool |
||
| 573 | * @throws InvalidConfiguration |
||
| 574 | */ |
||
| 575 | 17 | public function isDirty($attribute = null) |
|
| 576 | { |
||
| 577 | 17 | if (!empty($attribute)) { |
|
| 578 | 4 | $col = static::getColumnName($attribute); |
|
| 579 | 4 | return (isset($this->data[$col]) ? $this->data[$col] : null) !== |
|
| 580 | 4 | (isset($this->originalData[$col]) ? $this->originalData[$col] : null); |
|
| 581 | } |
||
| 582 | |||
| 583 | 14 | ksort($this->data); |
|
| 584 | 14 | ksort($this->originalData); |
|
| 585 | |||
| 586 | 14 | return serialize($this->data) !== serialize($this->originalData); |
|
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Check if the current data is valid |
||
| 591 | * |
||
| 592 | * Returns boolean true when valid otherwise an array of Errors. |
||
| 593 | * |
||
| 594 | * @return bool|Error[] |
||
| 595 | */ |
||
| 596 | 1 | public function isValid() |
|
| 597 | { |
||
| 598 | 1 | $result = []; |
|
| 599 | |||
| 600 | 1 | $presentColumns = []; |
|
| 601 | 1 | foreach ($this->data as $column => $value) { |
|
| 602 | 1 | $presentColumns[] = $column; |
|
| 603 | 1 | $result[] = static::validate($column, $value); |
|
| 604 | } |
||
| 605 | |||
| 606 | 1 | foreach (static::describe() as $column) { |
|
| 607 | 1 | if (!in_array($column->name, $presentColumns)) { |
|
| 608 | 1 | $result[] = static::validate($column->name, null); |
|
| 609 | } |
||
| 610 | } |
||
| 611 | |||
| 612 | 1 | $result = array_values(array_filter($result, function ($error) { |
|
| 613 | 1 | return $error instanceof Error; |
|
| 614 | 1 | })); |
|
| 615 | |||
| 616 | 1 | return count($result) === 0 ? true : $result; |
|
| 617 | } |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Empty event handler |
||
| 621 | * |
||
| 622 | * Get called when something is changed with magic setter. |
||
| 623 | * |
||
| 624 | * @param string $attribute The variable that got changed.merge(node.inheritedProperties) |
||
| 625 | * @param mixed $oldValue The old value of the variable |
||
| 626 | * @param mixed $value The new value of the variable |
||
| 627 | * @codeCoverageIgnore dummy event handler |
||
| 628 | */ |
||
| 629 | public function onChange($attribute, $oldValue, $value) |
||
| 630 | { |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Empty event handler |
||
| 635 | * |
||
| 636 | * Get called when the entity get initialized. |
||
| 637 | * |
||
| 638 | * @param bool $new Whether or not the entity is new or from database |
||
| 639 | * @codeCoverageIgnore dummy event handler |
||
| 640 | */ |
||
| 641 | public function onInit($new) |
||
| 642 | { |
||
| 643 | } |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Empty event handler |
||
| 647 | * |
||
| 648 | * Get called before the entity get updated in database. |
||
| 649 | * @codeCoverageIgnore dummy event handler |
||
| 650 | */ |
||
| 651 | public function preUpdate() |
||
| 652 | { |
||
| 653 | } |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Empty event handler |
||
| 657 | * |
||
| 658 | * Get called before the entity get inserted in database. |
||
| 659 | * @codeCoverageIgnore dummy event handler |
||
| 660 | */ |
||
| 661 | public function prePersist() |
||
| 662 | { |
||
| 663 | } |
||
| 664 | |||
| 665 | |||
| 666 | /** |
||
| 667 | * Empty event handler |
||
| 668 | * |
||
| 669 | * Get called after the entity got inserted in database. |
||
| 670 | * @codeCoverageIgnore dummy event handler |
||
| 671 | */ |
||
| 672 | public function postPersist() |
||
| 673 | { |
||
| 674 | } |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Empty event handler |
||
| 678 | * |
||
| 679 | * Get called after the entity got updated in database. |
||
| 680 | * @codeCoverageIgnore dummy event handler |
||
| 681 | */ |
||
| 682 | public function postUpdate() |
||
| 683 | { |
||
| 684 | } |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Fetches related objects |
||
| 688 | * |
||
| 689 | * For relations with cardinality many it returns an EntityFetcher. Otherwise it returns the entity. |
||
| 690 | * |
||
| 691 | * It will throw an error for non owner when the key is incomplete. |
||
| 692 | * |
||
| 693 | * @param string $relation The relation to fetch |
||
| 694 | * @param bool $getAll |
||
| 695 | * @return Entity|Entity[]|EntityFetcher |
||
| 696 | * @throws NoEntityManager |
||
| 697 | */ |
||
| 698 | 19 | public function fetch($relation, $getAll = false) |
|
| 699 | { |
||
| 700 | // @codeCoverageIgnoreStart |
||
| 701 | if ($getAll instanceof EM || func_num_args() === 3 && $getAll === null) { |
||
| 702 | $getAll = func_num_args() === 3 ? func_get_arg(2) : false; |
||
| 703 | trigger_error( |
||
| 704 | 'Passing EntityManager to fetch is deprecated. Use ->setEntityManager() to overwrite', |
||
| 705 | E_USER_DEPRECATED |
||
| 706 | ); |
||
| 707 | } |
||
| 708 | // @codeCoverageIgnoreEnd |
||
| 709 | |||
| 710 | 19 | $relation = $this::getRelation($relation); |
|
| 711 | |||
| 712 | 19 | if ($getAll) { |
|
| 713 | 4 | return $relation->fetchAll($this, $this->entityManager); |
|
| 714 | } else { |
||
| 715 | 15 | return $relation->fetch($this, $this->entityManager); |
|
| 716 | } |
||
| 717 | } |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Get the primary key |
||
| 721 | * |
||
| 722 | * @return array |
||
| 723 | * @throws IncompletePrimaryKey |
||
| 724 | */ |
||
| 725 | 38 | public function getPrimaryKey() |
|
| 737 | |||
| 738 | /** |
||
| 739 | * Get current data |
||
| 740 | * |
||
| 741 | * @return array |
||
| 742 | * @internal |
||
| 743 | */ |
||
| 744 | 24 | public function getData() |
|
| 748 | |||
| 749 | /** |
||
| 750 | * Set new original data |
||
| 751 | * |
||
| 752 | * @param array $data |
||
| 753 | * @internal |
||
| 754 | */ |
||
| 755 | 24 | public function setOriginalData(array $data) |
|
| 759 | |||
| 760 | /** |
||
| 761 | * String representation of data |
||
| 762 | * |
||
| 763 | * @link http://php.net/manual/en/serializable.serialize.php |
||
| 764 | * @return string |
||
| 765 | */ |
||
| 766 | 2 | public function serialize() |
|
| 767 | { |
||
| 768 | 2 | return serialize([$this->data, $this->relatedObjects]); |
|
| 769 | } |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Constructs the object |
||
| 773 | * |
||
| 774 | * @link http://php.net/manual/en/serializable.unserialize.php |
||
| 775 | * @param string $serialized The string representation of data |
||
| 776 | */ |
||
| 777 | 3 | public function unserialize($serialized) |
|
| 778 | { |
||
| 779 | 3 | list($this->data, $this->relatedObjects) = unserialize($serialized); |
|
| 780 | 3 | $this->entityManager = EM::getInstance(static::class); |
|
| 781 | 3 | $this->onInit(false); |
|
| 782 | 3 | } |
|
| 783 | |||
| 784 | // DEPRECATED stuff |
||
| 785 | |||
| 786 | /** |
||
| 787 | * @return string |
||
| 788 | * @deprecated use getOption from EntityManager |
||
| 789 | * @codeCoverageIgnore deprecated |
||
| 790 | */ |
||
| 791 | public static function getTableNameTemplate() |
||
| 795 | |||
| 796 | /** |
||
| 797 | * @param string $tableNameTemplate |
||
| 798 | * @deprecated use setOption from EntityManager |
||
| 799 | * @codeCoverageIgnore deprecated |
||
| 800 | */ |
||
| 801 | public static function setTableNameTemplate($tableNameTemplate) |
||
| 805 | |||
| 806 | /** |
||
| 807 | * @return string |
||
| 808 | * @deprecated use getOption from EntityManager |
||
| 809 | * @codeCoverageIgnore deprecated |
||
| 810 | */ |
||
| 811 | public static function getNamingSchemeTable() |
||
| 815 | |||
| 816 | /** |
||
| 817 | * @param string $namingSchemeTable |
||
| 818 | * @deprecated use setOption from EntityManager |
||
| 819 | * @codeCoverageIgnore deprecated |
||
| 820 | */ |
||
| 821 | public static function setNamingSchemeTable($namingSchemeTable) |
||
| 825 | |||
| 826 | /** |
||
| 827 | * @return string |
||
| 828 | * @deprecated use getOption from EntityManager |
||
| 829 | * @codeCoverageIgnore deprecated |
||
| 830 | */ |
||
| 831 | public static function getNamingSchemeColumn() |
||
| 835 | |||
| 836 | /** |
||
| 837 | * @param string $namingSchemeColumn |
||
| 838 | * @deprecated use setOption from EntityManager |
||
| 839 | * @codeCoverageIgnore deprecated |
||
| 840 | */ |
||
| 841 | public static function setNamingSchemeColumn($namingSchemeColumn) |
||
| 845 | |||
| 846 | /** |
||
| 847 | * @return string |
||
| 848 | * @deprecated use getOption from EntityManager |
||
| 849 | * @codeCoverageIgnore deprecated |
||
| 850 | */ |
||
| 851 | public static function getNamingSchemeMethods() |
||
| 855 | |||
| 856 | /** |
||
| 857 | * @param string $namingSchemeMethods |
||
| 858 | * @deprecated use setOption from EntityManager |
||
| 859 | * @codeCoverageIgnore deprecated |
||
| 860 | */ |
||
| 861 | public static function setNamingSchemeMethods($namingSchemeMethods) |
||
| 865 | } |
||
| 866 |