• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rotexsoft / gdao / 6758244680

05 Sep 2023 08:29PM UTC coverage: 97.059% (-2.9%) from 100.0%
6758244680

push

github

rotimi
PHP 8.1 minimum version readiness

66 of 68 relevant lines covered (97.06%)

11.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/GDAO/Model/RecordInterface.php
1
<?php
2
declare(strict_types=1);
3
namespace GDAO\Model;
4

5
/**
6
 * @psalm-suppress MissingTemplateParam
7
 * 
8
 * Contains a list of methods that Record classes must implement.
9
 *
10
 * @copyright (c) 2023, Rotexsoft
11
 */
12
interface RecordInterface extends \ArrayAccess, \Countable, \IteratorAggregate, \Stringable
13
{
14
    ////////////////////////////////////////////////////////////////////////////
15
    ////////////////////////////////////////////////////////////////////////////
16
    ////
17
    //// RECOMMENDATIONS
18
    //// * Data for the record ([to be saved to the DB] or [as read from the DB]) 
19
    ////   should be stored in a property of the record class implementing this
20
    ////   interface. It could be an array, ArrayObject or any other suitable 
21
    ////   data structure. Data from this property should be returned via 
22
    ////   $this->getData() & $this->getDataByRef(). Data contained in this 
23
    ////   property should be set via 
24
    ////   $this->loadData($data_2_load, array $cols_2_load = array()).
25
    ////   For documentation purposes this property will be refered to as
26
    ////   $this->data (with an assumption that it is an array).
27
    ////   
28
    //// * Another property of the same data type as the one described above should
29
    ////   be present in the class implementing this interface. This property should 
30
    ////   only be set the first time record data is fetched from the DB (it holds
31
    ////   a copy of the data initially loaded into the record from the DB). This 
32
    ////   property's values should be compared with the values of the property 
33
    ////   described above in order to be able to determine if the record's data 
34
    ////   has changed (this should be implemented in $this->isChanged($col=null)). 
35
    ////   Data from this property should be returned via $this->getInitialData() 
36
    ////   & $this->getInitialDataByRef(). Data contained in this property should 
37
    ////   be set ONCE via $this->loadData($data_2_load, array $cols_2_load = array())
38
    ////   the first time loadData is called on a record.
39
    ////   For documentation purposes this property will be refered to as
40
    ////   $this->initial_data (with an assumption that it is an array).
41
    ////   
42
    //// * Another property should be present in the class implementing this 
43
    ////   interface. This property should hold data related to a record (ie.
44
    ////   has-many, has-one, belongs-to and has-many-through relationship data).
45
    ////   Data from this property should be returned via $this->getRelatedData()
46
    ////   & $this->getRelatedDataByRef(). Data contained in this property should 
47
    ////   be set via $this->setRelatedData($key, $value).
48
    ////   For documentation purposes this property will be refered to as
49
    ////   $this->related_data (with an assumption that it is an array).
50
    ////   
51
    //// * Another property should be present in the class implementing this 
52
    ////   interface. This property should hold other data for a record instance 
53
    ////   (i.e. data not from any actual db column and not related data).
54
    ////   Data from this property should be returned via $this->getNonTableColAndNonRelatedData()
55
    ////   & $this->getNonTableColAndNonRelatedDataByRef().
56
    ////   For documentation purposes this property will be refered to as
57
    ////   $this->non_table_col_and_non_related_data (with an assumption that it is an array).
58
    ////   
59
    //// * A boolean property should be present in the class implementing this 
60
    ////   interface. This property should be set to true if a record is new
61
    ////   (ie. its data has never been saved to the DB), else false.
62
    ////   For documentation purposes this property will be refered to as
63
    ////   $this->is_new.
64
    ////   
65
    //// * A property of type \GDAO\Model should be present in a class implementing
66
    ////   this interface. This is the model object that will perform database 
67
    ////   operations on behalf of the record.
68
    ////   For documentation purposes this property will be refered to as
69
    ////   $this->model
70
    ////   
71
    ////////////////////////////////////////////////////////////////////////////
72
    ////////////////////////////////////////////////////////////////////////////    
73
    
74
    /**
75
     * @param array $data associative array of data to be loaded into this record.
76
     *                    [
77
     *                      'col_name1'=>'value_for_col1', 
78
     *                      .............................,
79
     *                      .............................,
80
     *                      'col_nameN'=>'value_for_colN'
81
     *                    ]
82
     * @param \GDAO\Model $model The model object that transfers data between the db and this record.
83
     */
84
    public function __construct(array $data, \GDAO\Model $model);
85
        
86
    /**
87
     * Delete the record from the db. 
88
     * 
89
     * If deletion was successful and the primary key column for the record's db
90
     * table is auto-incrementing, then unset the primary key field in the data 
91
     * contained in the record object.
92
     * 
93
     * NOTE: data contained in the record include $this->data, $this->related_data
94
     *       and $this->initial_data.
95
     * 
96
     * @param bool $set_record_objects_data_to_empty_array true to reset the record object's data to an empty array if db deletion was successful, false to keep record object's data
97
     * 
98
     * @return bool true if record was successfully deleted from db or false if not
99
     */
100
    public function delete(bool $set_record_objects_data_to_empty_array=false): bool;
101
    
102
    /**
103
     * Get the data for this record.
104
     * Modifying the returned data will not affect the data inside this record.
105
     * 
106
     * @return array a copy of the current data for this record
107
     */
108
    public function getData(): array;
109
    
110
    /**
111
     * Get a copy of the initial data loaded into this record.
112
     * Modifying the returned data will not affect the initial data inside this record.
113
     * 
114
     * @return array a copy of the initial data loaded into this record.
115
     */
116
    public function getInitialData(): array;
117
    
118
    
119
    /**
120
     * Get all the related data loaded into this record.
121
     * Modifying the returned data will not affect the related data inside this record.
122
     * 
123
     * @return array a reference to all the related data loaded into this record.
124
     */
125
    public function getRelatedData(): array;
126
    
127
    /**
128
     * Get data for this record that does not belong to any of it's table columns and is not related data.
129
     * 
130
     * @return array Data for this record (not to be saved to the db i.e. not from any actual db column and not related data).
131
     */
132
    public function getNonTableColAndNonRelatedData(): array;
133
    
134
    /**
135
     * Get a reference to the data for this record.
136
     * Modifying the returned data will affect the data inside this record.
137
     * 
138
     * @return array a reference to the current data for this record.
139
     */
140
    public function &getDataByRef(): array;
141
    
142
    /**
143
     * Get a reference to the initial data loaded into this record.
144
     * Modifying the returned data will affect the initial data inside this record.
145
     * 
146
     * @return array a reference to the initial data loaded into this record.
147
     */
148
    public function &getInitialDataByRef(): array;
149
    
150
    /**
151
     * Get a reference to all the related data loaded into this record.
152
     * Modifying the returned data will affect the related data inside this record.
153
     * 
154
     * @return array a reference to all the related data loaded into this record.
155
     */
156
    public function &getRelatedDataByRef(): array;
157
    
158
    /**
159
     * Get data for this record that does not belong to any of it's table columns and is not related data.
160
     * 
161
     * @return array reference to the data for this record (not from any actual db column and not related data).
162
     */
163
    public function &getNonTableColAndNonRelatedDataByRef(): array;
164
    
165
    /**
166
     * Set relation data for this record.
167
     * 
168
     * @param string $key relation name
169
     * @param array|RecordInterface|CollectionInterface $value an array or record or collection containing related data
170
     * 
171
     * @throws \GDAO\Model\RecordRelationWithSameNameAsAnExistingDBTableColumnNameException
172
     * 
173
     * @return $this
174
     */
175
    public function setRelatedData(string $key, array|RecordInterface|CollectionInterface $value): static;
176
    
177
    /**
178
     * Get the model object that saves and reads data to and from the db on behalf of this record
179
     */
180
    public function getModel(): \GDAO\Model;
181
    
182
    /**
183
     * @return string name of the primary-key column of the db table this record belongs to
184
     */
185
    public function getPrimaryCol(): string;
186
    
187
    /**
188
     * @return mixed the value stored in the primary-key column for this record.
189
     */
190
    public function getPrimaryVal(): mixed;
191
    
192
    /**
193
     * Tells if the record, or a particular table-column in the record, has 
194
     * changed from its initial value.
195
     * 
196
     * @param null|string $col The table-column name.
197
     * 
198
     * @return null|bool Returns null if the table-column name does not exist,
199
     *                   true if the data is changed, false if not changed.
200
     */
201
    public function isChanged(?string $col = null): ?bool;
202
    
203
    /**
204
     * Is the record new? (I.e. its data has never been saved to the db)
205
     */
206
    public function isNew(): bool;
207
    
208
    /**
209
     * This method partially or completely overwrites pre-existing data for a 
210
     * record and replaces it with the new data (this does not include related
211
     * data). If no data has previously been loaded into the record, keep a copy
212
     * of the loaded data for comparison in $this->isChanged($col=null)).
213
     * 
214
     * Note if $cols_2_load === null all data should be replaced, else only
215
     * replace data for the cols in $cols_2_load.
216
     * 
217
     * If $data_2_load is an instance of \GDAO\Model\RecordInterface and if 
218
     * $data_2_load->getModel()->getTableName() !== $this->getModel()->getTableName(), 
219
     * then the exception below should be thrown:
220
     * 
221
     *      \GDAO\Model\LoadingDataFromInvalidSourceIntoRecordException
222
     * 
223
     * @param \GDAO\Model\RecordInterface|array $data_2_load
224
     * @param array $cols_2_load name of field to load from $data_2_load. 
225
     *                           If empty, load all fields in $data_2_load.
226
     * 
227
     * @throws \GDAO\Model\LoadingDataFromInvalidSourceIntoRecordException
228
     * 
229
     * @return $this
230
     */
231
    public function loadData(RecordInterface|array $data_2_load, array $cols_2_load = []): static;
×
232
    
233
    /**
234
     * Set the _is_new attribute of this record to true (meaning that the data
235
     * for this record has never been saved to the db).
236
     * 
237
     * @return $this
238
     */
239
    public function markAsNew(): static;
240
    
241
    /**
242
     * Set the _is_new attribute of this record to false (meaning that the data
243
     * for this record has been saved to the db or was read from the db).
244
     * 
245
     * @return $this
246
     */
247
    public function markAsNotNew(): static;
248
    
249
    /**
250
     * Set all properties of this record to the state they should be in for a new record.
251
     * For example:
252
     *  - unset its primary key value via unset($this[$this->getPrimaryCol()]);
253
     *  - call $this->markAsNew()
254
     *  - etc.
255
     * 
256
     * The _data & _initial_data properties can be updated as needed by the 
257
     * implementing sub-class. 
258
     * For example:
259
     *  - they could be left as is 
260
     *  - or the value of _data could be copied to _initial_data
261
     *  - or the value of _initial_data could be copied to _data
262
     *  - etc.
263
     * 
264
     * @return $this
265
     */
266
    public function setStateToNew(): static;
267

268
    /**
269
     * Save the specified or already existing data for this record to the db.
270
     * Since this record can only talk to the db via its model property (_model)
271
     * the save operation will actually be done via $this->model.
272
     * 
273
     * @param null|\GDAO\Model\RecordInterface|array $data_2_save if null try to save data already contained in the record
274
     * 
275
     * @return null|bool true: successful save, false: failed save, null: no changed data to save
276
     */
277
    public function save(null|RecordInterface|array $data_2_save = null): ?bool;
278
    
279
    /**
280
     * Save the specified or already existing data for this record to the db.
281
     * Since this record can only talk to the db via its model property (_model)
282
     * the save operation will actually be done via $this->model.
283
     * This save operation shoould be gaurded by the PDO transaction mechanism
284
     * if available or another transaction mechanism. If the save operation 
285
     * fails all changes should be rolled back. If there is not transaction
286
     * mechanism available an Exception must be thrown alerting the caller to
287
     * use the save method instead.
288
     * 
289
     * @param null|\GDAO\Model\RecordInterface|array $data_2_save if null try to save data already contained in the record
290
     * 
291
     * @return bool|null true for a successful save, false for failed save, null: no changed data to save
292
     */
293
    public function saveInTransaction(null|RecordInterface|array $data_2_save = null): ?bool;
294
    
295
    /**
296
     * Set the \GDAO\Model object for this record
297
     * 
298
     * @return $this
299
     */
300
    public function setModel(\GDAO\Model $model): static;
301
    
302
    /**
303
     * Get all the data and property (name & value pairs) for this record.
304
     * 
305
     * @return array of all data & property (name & value pairs) for this record.
306
     */
307
    public function toArray(): array;
308
    
309
    //Magic Methods
310
    
311
    /**
312
     * Gets a data value.
313
     * 
314
     * @param string $key The requested data key.
315
     * 
316
     * @return mixed The data value.
317
     */
318
    public function __get($key): mixed;
319

320
    /**
321
     * Does a certain key exist in the data?
322
     * 
323
     * Note that this is slightly different from normal PHP isset(); it will
324
     * say the key is set, even if the key value is null or otherwise empty.
325
     * 
326
     * @param string $key The requested data key.
327
     */
328
    public function __isset($key): bool;
329

330
    /**
331
     * Sets a key value.
332
     * 
333
     * @param string $key The requested data key.
334
     * 
335
     * @param mixed $val The value to set the data to.
336
     */
337
    public function __set($key, mixed $val): void;
338

339
    /**
340
     * Get the string representation of all the data and property 
341
     * (name & value pairs) for this record.
342
     * 
343
     * @return string string representation of all the data & property 
344
     *                (name & value pairs) for this record.
345
     */
346
    public function __toString(): string;
347

348
    /**
349
     * Removes a key and its value in the data.
350
     * 
351
     * @param string $key The requested data key.
352
     */
353
    public function __unset($key): void;
354
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc