Entity.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\ORM;
  16. use Cake\Datasource\EntityInterface;
  17. use Cake\Datasource\EntityTrait;
  18. use Cake\Datasource\InvalidPropertyInterface;
  19. /**
  20. * An entity represents a single result row from a repository. It exposes the
  21. * methods for retrieving and storing properties associated in this row.
  22. */
  23. class Entity implements EntityInterface, InvalidPropertyInterface
  24. {
  25. use EntityTrait;
  26. /**
  27. * Initializes the internal properties of this entity out of the
  28. * keys in an array. The following list of options can be used:
  29. *
  30. * - useSetters: whether use internal setters for properties or not
  31. * - markClean: whether to mark all properties as clean after setting them
  32. * - markNew: whether this instance has not yet been persisted
  33. * - guard: whether to prevent inaccessible properties from being set (default: false)
  34. * - source: A string representing the alias of the repository this entity came from
  35. *
  36. * ### Example:
  37. *
  38. * ```
  39. * $entity = new Entity(['id' => 1, 'name' => 'Andrew'])
  40. * ```
  41. *
  42. * @param array $properties hash of properties to set in this entity
  43. * @param array $options list of options to use when creating this entity
  44. */
  45. public function __construct(array $properties = [], array $options = [])
  46. {
  47. $options += [
  48. 'useSetters' => true,
  49. 'markClean' => false,
  50. 'markNew' => null,
  51. 'guard' => false,
  52. 'source' => null,
  53. ];
  54. if (!empty($options['source'])) {
  55. $this->setSource($options['source']);
  56. }
  57. if ($options['markNew'] !== null) {
  58. $this->setNew($options['markNew']);
  59. }
  60. if (!empty($properties) && $options['markClean'] && !$options['useSetters']) {
  61. $this->_properties = $properties;
  62. return;
  63. }
  64. if (!empty($properties)) {
  65. $this->set($properties, [
  66. 'setter' => $options['useSetters'],
  67. 'guard' => $options['guard'],
  68. ]);
  69. }
  70. if ($options['markClean']) {
  71. $this->clean();
  72. }
  73. }
  74. }