-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
EntityInterface.php
331 lines (294 loc) · 9.74 KB
/
EntityInterface.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Datasource;
use ArrayAccess;
use JsonSerializable;
use Stringable;
/**
* Describes the methods that any class representing a data storage should
* comply with.
*
* @property mixed $id Alias for commonly used primary key.
* @template-extends \ArrayAccess<string, mixed>
* @method bool hasValue(string $field)
*/
interface EntityInterface extends ArrayAccess, JsonSerializable, Stringable
{
/**
* Sets hidden fields.
*
* @param list<string> $fields An array of fields to hide from array exports.
* @param bool $merge Merge the new fields with the existing. By default false.
* @return $this
*/
public function setHidden(array $fields, bool $merge = false);
/**
* Gets the hidden fields.
*
* @return array<string>
*/
public function getHidden(): array;
/**
* Sets the virtual fields on this entity.
*
* @param list<string> $fields An array of fields to treat as virtual.
* @param bool $merge Merge the new fields with the existing. By default false.
* @return $this
*/
public function setVirtual(array $fields, bool $merge = false);
/**
* Gets the virtual fields on this entity.
*
* @return array<string>
*/
public function getVirtual(): array;
/**
* Returns whether a field is an original one.
* Original fields are those that an entity was instantiated with.
*
* @return bool
*/
public function isOriginalField(string $name): bool;
/**
* Returns an array of original fields.
* Original fields are those that an entity was initialized with.
*
* @return array<string>
*/
public function getOriginalFields(): array;
/**
* Sets the dirty status of a single field.
*
* @param string $field the field to set or check status for
* @param bool $isDirty true means the field was changed, false means
* it was not changed. Default true.
* @return $this
*/
public function setDirty(string $field, bool $isDirty = true);
/**
* Checks if the entity is dirty or if a single field of it is dirty.
*
* @param string|null $field The field to check the status for. Null for the whole entity.
* @return bool Whether the field was changed or not
*/
public function isDirty(?string $field = null): bool;
/**
* Gets the dirty fields.
*
* @return array<string>
*/
public function getDirty(): array;
/**
* Returns whether this entity has errors.
*
* @param bool $includeNested true will check nested entities for hasErrors()
* @return bool
*/
public function hasErrors(bool $includeNested = true): bool;
/**
* Returns all validation errors.
*
* @return array
*/
public function getErrors(): array;
/**
* Returns validation errors of a field
*
* @param string $field Field name to get the errors from
* @return array
*/
public function getError(string $field): array;
/**
* Sets error messages to the entity
*
* @param array $errors The array of errors to set.
* @param bool $overwrite Whether to overwrite pre-existing errors for $fields
* @return $this
*/
public function setErrors(array $errors, bool $overwrite = false);
/**
* Sets errors for a single field
*
* @param string $field The field to get errors for, or the array of errors to set.
* @param array|string $errors The errors to be set for $field
* @param bool $overwrite Whether to overwrite pre-existing errors for $field
* @return $this
*/
public function setError(string $field, array|string $errors, bool $overwrite = false);
/**
* Stores whether a field value can be changed or set in this entity.
*
* @param array<string>|string $field single or list of fields to change its accessibility
* @param bool $set true marks the field as accessible, false will
* mark it as protected.
* @return $this
*/
public function setAccess(array|string $field, bool $set);
/**
* Accessible configuration for this entity.
*
* @return array<bool>
*/
public function getAccessible(): array;
/**
* Checks if a field is accessible
*
* @param string $field Field name to check
* @return bool
*/
public function isAccessible(string $field): bool;
/**
* Sets the source alias
*
* @param string $alias the alias of the repository
* @return $this
*/
public function setSource(string $alias);
/**
* Returns the alias of the repository from which this entity came from.
*
* @return string
*/
public function getSource(): string;
/**
* Returns an array with the requested original fields
* stored in this entity, indexed by field name.
*
* @param list<string> $fields List of fields to be returned
* @return array<string, mixed>
*/
public function extractOriginal(array $fields): array;
/**
* Returns an array with only the original fields
* stored in this entity, indexed by field name.
*
* @param list<string> $fields List of fields to be returned
* @return array<string, mixed>
*/
public function extractOriginalChanged(array $fields): array;
/**
* Sets one or multiple fields to the specified value
*
* @param array<string, mixed>|string $field the name of field to set or a list of
* fields with their respective values
* @param mixed $value The value to set to the field or an array if the
* first argument is also an array, in which case will be treated as $options
* @param array<string, mixed> $options Options to be used for setting the field. Allowed option
* keys are `setter` and `guard`
* @return $this
*/
public function set(array|string $field, mixed $value = null, array $options = []);
/**
* Returns the value of a field by name
*
* @param string $field the name of the field to retrieve
* @return mixed
*/
public function &get(string $field): mixed;
/**
* Enable/disable field presence check when accessing a property.
*
* If enabled an exception will be thrown when trying to access a non-existent property.
*
* @param bool $value `true` to enable, `false` to disable.
*/
public function requireFieldPresence(bool $value = true): void;
/**
* Returns whether a field has an original value
*
* @param string $field
* @return bool
*/
public function hasOriginal(string $field): bool;
/**
* Returns the original value of a field.
*
* @param string $field The name of the field.
* @param bool $allowFallback whether to allow falling back to the current field value if no original exists
* @return mixed
*/
public function getOriginal(string $field, bool $allowFallback = true): mixed;
/**
* Gets all original values of the entity.
*
* @return array
*/
public function getOriginalValues(): array;
/**
* Returns whether this entity contains a field named $field.
*
* The method will return `true` even when the field is set to `null`.
*
* @param array<string>|string $field The field to check.
* @return bool
*/
public function has(array|string $field): bool;
/**
* Removes a field or list of fields from this entity
*
* @param array<string>|string $field The field to unset.
* @return $this
*/
public function unset(array|string $field);
/**
* Get the list of visible fields.
*
* @return array<string> A list of fields that are 'visible' in all representations.
*/
public function getVisible(): array;
/**
* Returns an array with all the visible fields set in this entity.
*
* *Note* hidden fields are not visible, and will not be output
* by toArray().
*
* @return array
*/
public function toArray(): array;
/**
* Returns an array with the requested fields
* stored in this entity, indexed by field name
*
* @param list<string> $fields list of fields to be returned
* @param bool $onlyDirty Return the requested field only if it is dirty
* @return array<string, mixed>
*/
public function extract(array $fields, bool $onlyDirty = false): array;
/**
* Sets the entire entity as clean, which means that it will appear as
* no fields being modified or added at all. This is an useful call
* for an initial object hydration
*
* @return void
*/
public function clean(): void;
/**
* Set the status of this entity.
*
* Using `true` means that the entity has not been persisted in the database,
* `false` indicates that the entity has been persisted.
*
* @param bool $new Indicate whether this entity has been persisted.
* @return $this
*/
public function setNew(bool $new);
/**
* Returns whether this entity has already been persisted.
*
* @return bool Whether the entity has been persisted.
*/
public function isNew(): bool;
}