EntityInterface.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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\Datasource;
  16. use ArrayAccess;
  17. use JsonSerializable;
  18. /**
  19. * Describes the methods that any class representing a data storage should
  20. * comply with.
  21. *
  22. * In 4.x the following methods will officially be added to the interface:
  23. *
  24. * @method $this setHidden(array $properties, $merge = false)
  25. * @method array getHidden()
  26. * @method $this setVirtual(array $properties, $merge = false)
  27. * @method array getVirtual()
  28. * @method $this setDirty($property, $isDirty)
  29. * @method bool isDirty($property = null)
  30. * @method bool hasErrors($includeNested = true)
  31. * @method array getErrors()
  32. * @method array getError($field)
  33. * @method array setErrors(array $fields, $overwrite = false)
  34. * @method array setError($field, $errors, $overwrite = false)
  35. * @method $this setAccess($property, $set)
  36. * @method bool isAccessible($property)
  37. * @method $this setSource($source)
  38. * @method string getSource()
  39. * @method array extractOriginal(array $properties)
  40. * @method array extractOriginalChanged(array $properties)
  41. *
  42. * @property mixed $id Alias for commonly used primary key.
  43. */
  44. interface EntityInterface extends ArrayAccess, JsonSerializable
  45. {
  46. /**
  47. * Sets one or multiple properties to the specified value
  48. *
  49. * @param string|array $property the name of property to set or a list of
  50. * properties with their respective values
  51. * @param mixed $value The value to set to the property or an array if the
  52. * first argument is also an array, in which case will be treated as $options
  53. * @param array $options options to be used for setting the property. Allowed option
  54. * keys are `setter` and `guard`
  55. * @return \Cake\Datasource\EntityInterface
  56. */
  57. public function set($property, $value = null, array $options = []);
  58. /**
  59. * Returns the value of a property by name
  60. *
  61. * @param string $property the name of the property to retrieve
  62. * @return mixed
  63. */
  64. public function &get($property);
  65. /**
  66. * Returns whether this entity contains a property named $property
  67. * regardless of if it is empty.
  68. *
  69. * @param string|array $property The property to check.
  70. * @return bool
  71. */
  72. public function has($property);
  73. /**
  74. * Removes a property or list of properties from this entity
  75. *
  76. * @param string|array $property The property to unset.
  77. * @return \Cake\Datasource\EntityInterface
  78. */
  79. public function unsetProperty($property);
  80. /**
  81. * Get/Set the hidden properties on this entity.
  82. *
  83. * If the properties argument is null, the currently hidden properties
  84. * will be returned. Otherwise the hidden properties will be set.
  85. *
  86. * @param null|array $properties Either an array of properties to hide or null to get properties
  87. * @return array|\Cake\Datasource\EntityInterface
  88. */
  89. public function hiddenProperties($properties = null);
  90. /**
  91. * Get/Set the virtual properties on this entity.
  92. *
  93. * If the properties argument is null, the currently virtual properties
  94. * will be returned. Otherwise the virtual properties will be set.
  95. *
  96. * @param null|array $properties Either an array of properties to treat as virtual or null to get properties
  97. * @return array|\Cake\Datasource\EntityInterface
  98. */
  99. public function virtualProperties($properties = null);
  100. /**
  101. * Get the list of visible properties.
  102. *
  103. * @return array A list of properties that are 'visible' in all representations.
  104. */
  105. public function visibleProperties();
  106. /**
  107. * Returns an array with all the visible properties set in this entity.
  108. *
  109. * *Note* hidden properties are not visible, and will not be output
  110. * by toArray().
  111. *
  112. * @return array
  113. */
  114. public function toArray();
  115. /**
  116. * Returns an array with the requested properties
  117. * stored in this entity, indexed by property name
  118. *
  119. * @param array $properties list of properties to be returned
  120. * @param bool $onlyDirty Return the requested property only if it is dirty
  121. * @return array
  122. */
  123. public function extract(array $properties, $onlyDirty = false);
  124. /**
  125. * Sets the dirty status of a single property. If called with no second
  126. * argument, it will return whether the property was modified or not
  127. * after the object creation.
  128. *
  129. * When called with no arguments it will return whether or not there are any
  130. * dirty property in the entity
  131. *
  132. * @deprecated 3.4.0 Use setDirty() and isDirty() instead.
  133. * @param string|null $property the field to set or check status for
  134. * @param null|bool $isDirty true means the property was changed, false means
  135. * it was not changed and null will make the function return current state
  136. * for that property
  137. * @return bool whether the property was changed or not
  138. */
  139. public function dirty($property = null, $isDirty = null);
  140. /**
  141. * Sets the entire entity as clean, which means that it will appear as
  142. * no properties being modified or added at all. This is an useful call
  143. * for an initial object hydration
  144. *
  145. * @return void
  146. */
  147. public function clean();
  148. /**
  149. * Returns whether or not this entity has already been persisted.
  150. * This method can return null in the case there is no prior information on
  151. * the status of this entity.
  152. *
  153. * If called with a boolean, this method will set the status of this instance.
  154. * Using `true` means that the instance has not been persisted in the database, `false`
  155. * that it already is.
  156. *
  157. * @param bool|null $new Indicate whether or not this instance has been persisted.
  158. * @return bool If it is known whether the entity was already persisted
  159. * null otherwise
  160. */
  161. public function isNew($new = null);
  162. /**
  163. * Sets the error messages for a field or a list of fields. When called
  164. * without the second argument it returns the validation
  165. * errors for the specified fields. If called with no arguments it returns
  166. * all the validation error messages stored in this entity.
  167. *
  168. * When used as a setter, this method will return this entity instance for method
  169. * chaining.
  170. *
  171. * @deprecated 3.4.0 Use setErrors() and getErrors() instead.
  172. * @param string|array|null $field The field to get errors for.
  173. * @param string|array|null $errors The errors to be set for $field
  174. * @param bool $overwrite Whether or not to overwrite pre-existing errors for $field
  175. * @return array|\Cake\Datasource\EntityInterface
  176. */
  177. public function errors($field = null, $errors = null, $overwrite = false);
  178. /**
  179. * Stores whether or not a property value can be changed or set in this entity.
  180. * The special property `*` can also be marked as accessible or protected, meaning
  181. * that any other property specified before will take its value. For example
  182. * `$entity->accessible('*', true)` means that any property not specified already
  183. * will be accessible by default.
  184. *
  185. * @deprecated 3.4.0 Use setAccess() and isAccessible() instead.
  186. * @param string|array $property Either a single or list of properties to change its accessibility.
  187. * @param bool|null $set true marks the property as accessible, false will
  188. * mark it as protected.
  189. * @return \Cake\Datasource\EntityInterface|bool
  190. */
  191. public function accessible($property, $set = null);
  192. }