EntityInterface.php 7.7 KB

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