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

codeigniter4 / CodeIgniter4 / 21568681844

01 Feb 2026 07:16PM UTC coverage: 85.41% (+1.0%) from 84.387%
21568681844

push

github

web-flow
Merge pull request #9916 from codeigniter4/4.7

4.7.0 Merge code

1603 of 1888 new or added lines in 101 files covered. (84.9%)

31 existing lines in 11 files now uncovered.

22163 of 25949 relevant lines covered (85.41%)

205.52 hits per line

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

91.82
/system/Entity/Entity.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Entity;
15

16
use BackedEnum;
17
use CodeIgniter\DataCaster\DataCaster;
18
use CodeIgniter\Entity\Cast\ArrayCast;
19
use CodeIgniter\Entity\Cast\BooleanCast;
20
use CodeIgniter\Entity\Cast\CSVCast;
21
use CodeIgniter\Entity\Cast\DatetimeCast;
22
use CodeIgniter\Entity\Cast\EnumCast;
23
use CodeIgniter\Entity\Cast\FloatCast;
24
use CodeIgniter\Entity\Cast\IntBoolCast;
25
use CodeIgniter\Entity\Cast\IntegerCast;
26
use CodeIgniter\Entity\Cast\JsonCast;
27
use CodeIgniter\Entity\Cast\ObjectCast;
28
use CodeIgniter\Entity\Cast\StringCast;
29
use CodeIgniter\Entity\Cast\TimestampCast;
30
use CodeIgniter\Entity\Cast\URICast;
31
use CodeIgniter\Entity\Exceptions\CastException;
32
use CodeIgniter\I18n\Time;
33
use DateTimeInterface;
34
use Exception;
35
use JsonSerializable;
36
use Traversable;
37
use UnitEnum;
38

39
/**
40
 * Entity encapsulation, for use with CodeIgniter\Model
41
 *
42
 * @see \CodeIgniter\Entity\EntityTest
43
 */
44
class Entity implements JsonSerializable
45
{
46
    /**
47
     * Maps names used in sets and gets against unique
48
     * names within the class, allowing independence from
49
     * database column names.
50
     *
51
     * Example:
52
     *  $datamap = [
53
     *      'class_property_name' => 'db_column_name'
54
     *  ];
55
     *
56
     * @var array<string, string>
57
     */
58
    protected $datamap = [];
59

60
    /**
61
     * The date fields.
62
     *
63
     * @var list<string>
64
     */
65
    protected $dates = [
66
        'created_at',
67
        'updated_at',
68
        'deleted_at',
69
    ];
70

71
    /**
72
     * Array of field names and the type of value to cast them as when
73
     * they are accessed.
74
     *
75
     * @var array<string, string>
76
     */
77
    protected $casts = [];
78

79
    /**
80
     * Custom convert handlers.
81
     *
82
     * @var array<string, string>
83
     */
84
    protected $castHandlers = [];
85

86
    /**
87
     * Default convert handlers.
88
     *
89
     * @var array<string, string>
90
     */
91
    private array $defaultCastHandlers = [
92
        'array'     => ArrayCast::class,
93
        'bool'      => BooleanCast::class,
94
        'boolean'   => BooleanCast::class,
95
        'csv'       => CSVCast::class,
96
        'datetime'  => DatetimeCast::class,
97
        'double'    => FloatCast::class,
98
        'enum'      => EnumCast::class,
99
        'float'     => FloatCast::class,
100
        'int'       => IntegerCast::class,
101
        'integer'   => IntegerCast::class,
102
        'int-bool'  => IntBoolCast::class,
103
        'json'      => JsonCast::class,
104
        'object'    => ObjectCast::class,
105
        'string'    => StringCast::class,
106
        'timestamp' => TimestampCast::class,
107
        'uri'       => URICast::class,
108
    ];
109

110
    /**
111
     * Holds the current values of all class vars.
112
     *
113
     * @var array<string, mixed>
114
     */
115
    protected $attributes = [];
116

117
    /**
118
     * Holds original copies of all class vars so we can determine
119
     * what's actually been changed and not accidentally write
120
     * nulls where we shouldn't.
121
     *
122
     * @var array<string, mixed>
123
     */
124
    protected $original = [];
125

126
    /**
127
     * The data caster.
128
     */
129
    protected ?DataCaster $dataCaster = null;
130

131
    /**
132
     * Holds info whenever properties have to be casted.
133
     */
134
    private bool $_cast = true;
135

136
    /**
137
     * Indicates whether all attributes are scalars (for optimization).
138
     */
139
    private bool $_onlyScalars = true;
140

141
    /**
142
     * Allows filling in Entity parameters during construction.
143
     *
144
     * @param array<string, mixed> $data
145
     */
146
    public function __construct(?array $data = null)
147
    {
148
        $this->dataCaster = $this->dataCaster();
168✔
149

150
        $this->syncOriginal();
168✔
151

152
        $this->fill($data);
168✔
153
    }
154

155
    /**
156
     * Takes an array of key/value pairs and sets them as class
157
     * properties, using any `setCamelCasedProperty()` methods
158
     * that may or may not exist.
159
     *
160
     * @param array<string, array<int|string, mixed>|bool|float|int|object|string|null> $data
161
     *
162
     * @return $this
163
     */
164
    public function fill(?array $data = null)
165
    {
166
        if (! is_array($data)) {
168✔
167
            return $this;
162✔
168
        }
169

170
        foreach ($data as $key => $value) {
18✔
171
            $this->__set($key, $value);
18✔
172
        }
173

174
        return $this;
18✔
175
    }
176

177
    /**
178
     * General method that will return all public and protected values
179
     * of this entity as an array. All values are accessed through the
180
     * __get() magic method so will have any casts, etc applied to them.
181
     *
182
     * @param bool $onlyChanged If true, only return values that have changed since object creation.
183
     * @param bool $cast        If true, properties will be cast.
184
     * @param bool $recursive   If true, inner entities will be cast as array as well.
185
     *
186
     * @return array<string, mixed>
187
     */
188
    public function toArray(bool $onlyChanged = false, bool $cast = true, bool $recursive = false): array
189
    {
190
        $originalCast = $this->_cast;
11✔
191
        $this->_cast  = $cast;
11✔
192

193
        $keys = array_filter(array_keys($this->attributes), static fn ($key): bool => ! str_starts_with($key, '_'));
11✔
194

195
        if (is_array($this->datamap)) {
11✔
196
            $keys = array_unique(
11✔
197
                [...array_diff($keys, $this->datamap), ...array_keys($this->datamap)],
11✔
198
            );
11✔
199
        }
200

201
        $return = [];
11✔
202

203
        // Loop over the properties, to allow magic methods to do their thing.
204
        foreach ($keys as $key) {
11✔
205
            if ($onlyChanged && ! $this->hasChanged($key)) {
11✔
206
                continue;
2✔
207
            }
208

209
            $return[$key] = $this->__get($key);
11✔
210

211
            if ($recursive) {
11✔
212
                if ($return[$key] instanceof self) {
1✔
213
                    $return[$key] = $return[$key]->toArray($onlyChanged, $cast, $recursive);
1✔
214
                } elseif (is_callable([$return[$key], 'toArray'])) {
1✔
215
                    $return[$key] = $return[$key]->toArray();
×
216
                }
217
            }
218
        }
219

220
        $this->_cast = $originalCast;
11✔
221

222
        return $return;
11✔
223
    }
224

225
    /**
226
     * Returns the raw values of the current attributes.
227
     *
228
     * @param bool $onlyChanged If true, only return values that have changed since object creation.
229
     * @param bool $recursive   If true, inner entities will be cast as array as well.
230
     *
231
     * @return array<string, mixed>
232
     */
233
    public function toRawArray(bool $onlyChanged = false, bool $recursive = false): array
234
    {
235
        $convert = static function ($value) use (&$convert, $recursive) {
44✔
236
            if (! $recursive) {
8✔
NEW
237
                return $value;
×
238
            }
239

240
            if ($value instanceof self) {
8✔
241
                // Always output full array for nested entities
242
                return $value->toRawArray(false, true);
2✔
243
            }
244

245
            if (is_array($value)) {
8✔
246
                $result = [];
1✔
247

248
                foreach ($value as $k => $v) {
1✔
249
                    $result[$k] = $convert($v);
1✔
250
                }
251

252
                return $result;
1✔
253
            }
254

255
            if (is_object($value) && is_callable([$value, 'toRawArray'])) {
8✔
NEW
256
                return $value->toRawArray();
×
257
            }
258

259
            return $value;
8✔
260
        };
44✔
261

262
        // When returning everything
263
        if (! $onlyChanged) {
44✔
264
            return $recursive
36✔
265
                ? array_map($convert, $this->attributes)
8✔
266
                : $this->attributes;
36✔
267
        }
268

269
        // When filtering by changed values only
270
        $return = [];
15✔
271

272
        foreach ($this->attributes as $key => $value) {
15✔
273
            // Special handling for arrays of entities in recursive mode
274
            // Skip hasChanged() and do per-entity comparison directly
275
            if ($recursive && is_array($value) && $this->containsOnlyEntities($value)) {
15✔
276
                $originalValue = $this->original[$key] ?? null;
4✔
277

278
                if (! is_string($originalValue)) {
4✔
279
                    // No original or invalid format, export all entities
NEW
280
                    $converted = [];
×
281

NEW
282
                    foreach ($value as $idx => $item) {
×
NEW
283
                        $converted[$idx] = $item->toRawArray(false, true);
×
284
                    }
NEW
285
                    $return[$key] = $converted;
×
286

NEW
287
                    continue;
×
288
                }
289

290
                // Decode original array structure for per-entity comparison
291
                $originalArray = json_decode($originalValue, true);
4✔
292
                $converted     = [];
4✔
293

294
                foreach ($value as $idx => $item) {
4✔
295
                    // Compare current entity against its original state
296
                    $currentNormalized  = $this->normalizeValue($item);
4✔
297
                    $originalNormalized = $originalArray[$idx] ?? null;
4✔
298

299
                    // Only include if changed, new, or can't determine
300
                    if ($originalNormalized === null || $currentNormalized !== $originalNormalized) {
4✔
301
                        $converted[$idx] = $item->toRawArray(false, true);
3✔
302
                    }
303
                }
304

305
                // Only include this property if at least one entity changed
306
                if ($converted !== []) {
4✔
307
                    $return[$key] = $converted;
3✔
308
                }
309

310
                continue;
4✔
311
            }
312

313
            // For all other cases, use hasChanged()
314
            if (! $this->hasChanged($key)) {
15✔
315
                continue;
13✔
316
            }
317

318
            if ($recursive) {
11✔
319
                // Special handling for arrays (mixed or not all entities)
NEW
320
                if (is_array($value)) {
×
NEW
321
                    $converted = [];
×
322

NEW
323
                    foreach ($value as $idx => $item) {
×
NEW
324
                        $converted[$idx] = $item instanceof self ? $item->toRawArray(false, true) : $convert($item);
×
325
                    }
NEW
326
                    $return[$key] = $converted;
×
327

NEW
328
                    continue;
×
329
                }
330

331
                // default recursive conversion
NEW
332
                $return[$key] = $convert($value);
×
333

NEW
334
                continue;
×
335
            }
336

337
            // non-recursive changed value
338
            $return[$key] = $value;
11✔
339
        }
340

341
        return $return;
15✔
342
    }
343

344
    /**
345
     * Ensures our "original" values match the current values.
346
     *
347
     * Objects and arrays are normalized and JSON-encoded for reliable change detection,
348
     * while scalars are stored as-is for performance.
349
     *
350
     * @return $this
351
     */
352
    public function syncOriginal()
353
    {
354
        $this->original     = [];
168✔
355
        $this->_onlyScalars = true;
168✔
356

357
        foreach ($this->attributes as $key => $value) {
168✔
358
            if (is_object($value) || is_array($value)) {
143✔
359
                $this->original[$key] = json_encode($this->normalizeValue($value), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
36✔
360
                $this->_onlyScalars   = false;
36✔
361
            } else {
362
                $this->original[$key] = $value;
139✔
363
            }
364
        }
365

366
        return $this;
168✔
367
    }
368

369
    /**
370
     * Checks a property to see if it has changed since the entity
371
     * was created. Or, without a parameter, checks if any
372
     * properties have changed.
373
     */
374
    public function hasChanged(?string $key = null): bool
375
    {
376
        // If no parameter was given then check all attributes
377
        if ($key === null) {
51✔
378
            if ($this->_onlyScalars) {
6✔
379
                return $this->original !== $this->attributes;
5✔
380
            }
381

382
            foreach (array_keys($this->attributes) as $attributeKey) {
1✔
383
                if ($this->hasChanged($attributeKey)) {
1✔
384
                    return true;
1✔
385
                }
386
            }
387

388
            return false;
1✔
389
        }
390

391
        $dbColumn = $this->mapProperty($key);
47✔
392

393
        // Key doesn't exist in either
394
        if (! array_key_exists($dbColumn, $this->original) && ! array_key_exists($dbColumn, $this->attributes)) {
47✔
395
            return false;
1✔
396
        }
397

398
        // It's a new element
399
        if (! array_key_exists($dbColumn, $this->original) && array_key_exists($dbColumn, $this->attributes)) {
46✔
400
            return true;
4✔
401
        }
402

403
        // It was removed
404
        if (array_key_exists($dbColumn, $this->original) && ! array_key_exists($dbColumn, $this->attributes)) {
44✔
405
            return true;
1✔
406
        }
407

408
        $originalValue = $this->original[$dbColumn];
43✔
409
        $currentValue  = $this->attributes[$dbColumn];
43✔
410

411
        // If original is a string, it was JSON-encoded (object or array)
412
        if (is_string($originalValue) && (is_object($currentValue) || is_array($currentValue))) {
43✔
413
            return $originalValue !== json_encode($this->normalizeValue($currentValue), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
23✔
414
        }
415

416
        // For scalars, use direct comparison
417
        return $originalValue !== $currentValue;
23✔
418
    }
419

420
    /**
421
     * Checks if an array contains only Entity instances.
422
     * This allows optimization for per-entity change tracking.
423
     *
424
     * @param array<int|string, mixed> $data
425
     */
426
    private function containsOnlyEntities(array $data): bool
427
    {
428
        if ($data === []) {
4✔
NEW
429
            return false;
×
430
        }
431

432
        foreach ($data as $item) {
4✔
433
            if (! $item instanceof self) {
4✔
NEW
434
                return false;
×
435
            }
436
        }
437

438
        return true;
4✔
439
    }
440

441
    /**
442
     * Recursively normalize a value for comparison.
443
     * Converts objects and arrays to a JSON-encodable format.
444
     */
445
    private function normalizeValue(mixed $data): mixed
446
    {
447
        if (is_array($data)) {
36✔
448
            $normalized = [];
29✔
449

450
            foreach ($data as $key => $value) {
29✔
451
                $normalized[$key] = $this->normalizeValue($value);
27✔
452
            }
453

454
            return $normalized;
29✔
455
        }
456

457
        if (is_object($data)) {
35✔
458
            // Check for Entity instance (use raw values, recursive)
459
            if ($data instanceof self) {
30✔
460
                $objectData = $data->toRawArray(false, true);
6✔
461
            } elseif ($data instanceof JsonSerializable) {
24✔
462
                $objectData = $data->jsonSerialize();
1✔
463
            } elseif (method_exists($data, 'toArray')) {
23✔
464
                $objectData = $data->toArray();
1✔
465
            } elseif ($data instanceof Traversable) {
22✔
466
                $objectData = iterator_to_array($data);
1✔
467
            } elseif ($data instanceof DateTimeInterface) {
22✔
468
                return [
8✔
469
                    '__class'    => $data::class,
8✔
470
                    '__datetime' => $data->format(DATE_RFC3339_EXTENDED),
8✔
471
                ];
8✔
472
            } elseif ($data instanceof UnitEnum) {
14✔
473
                return [
5✔
474
                    '__class' => $data::class,
5✔
475
                    '__enum'  => $data instanceof BackedEnum ? $data->value : $data->name,
5✔
476
                ];
5✔
477
            } else {
478
                $objectData = get_object_vars($data);
9✔
479

480
                // Fallback for value objects with __toString()
481
                // when properties are not accessible
482
                if ($objectData === [] && method_exists($data, '__toString')) {
9✔
483
                    return [
1✔
484
                        '__class'  => $data::class,
1✔
485
                        '__string' => (string) $data,
1✔
486
                    ];
1✔
487
                }
488
            }
489

490
            return [
16✔
491
                '__class' => $data::class,
16✔
492
                '__data'  => $this->normalizeValue($objectData),
16✔
493
            ];
16✔
494
        }
495

496
        // Return scalars and null as-is
497
        return $data;
27✔
498
    }
499

500
    /**
501
     * Set raw data array without any mutations.
502
     *
503
     * @param array<string, mixed> $data
504
     *
505
     * @return $this
506
     */
507
    public function injectRawData(array $data)
508
    {
509
        $this->attributes = $data;
26✔
510

511
        $this->syncOriginal();
26✔
512

513
        return $this;
26✔
514
    }
515

516
    /**
517
     * Checks the datamap to see if this property name is being mapped,
518
     * and returns the DB column name, if any, or the original property name.
519
     *
520
     * @return string Database column name.
521
     */
522
    protected function mapProperty(string $key)
523
    {
524
        if ($this->datamap === []) {
155✔
525
            return $key;
113✔
526
        }
527

528
        if (array_key_exists($key, $this->datamap) && $this->datamap[$key] !== '') {
42✔
529
            return $this->datamap[$key];
17✔
530
        }
531

532
        return $key;
35✔
533
    }
534

535
    /**
536
     * Converts the given string|timestamp|DateTimeInterface instance
537
     * into the "CodeIgniter\I18n\Time" object.
538
     *
539
     * @param DateTimeInterface|float|int|string $value
540
     *
541
     * @return Time
542
     *
543
     * @throws Exception
544
     */
545
    protected function mutateDate($value)
546
    {
547
        return DatetimeCast::get($value);
32✔
548
    }
549

550
    /**
551
     * Provides the ability to cast an item as a specific data type.
552
     * Add ? at the beginning of the type (i.e. ?string) to get `null`
553
     * instead of casting $value when $value is null.
554
     *
555
     * @param bool|float|int|string|null $value     Attribute value
556
     * @param string                     $attribute Attribute name
557
     * @param string                     $method    Allowed to "get" and "set"
558
     *
559
     * @return array<int|string, mixed>|bool|float|int|object|string|null
560
     *
561
     * @throws CastException
562
     */
563
    protected function castAs($value, string $attribute, string $method = 'get')
564
    {
565
        if ($this->dataCaster() instanceof DataCaster) {
149✔
566
            return $this->dataCaster
53✔
567
                // @TODO if $casts is readonly, we don't need the setTypes() method.
53✔
568
                ->setTypes($this->casts)
53✔
569
                ->castAs($value, $attribute, $method);
53✔
570
        }
571

572
        return $value;
96✔
573
    }
574

575
    /**
576
     * Returns a DataCaster instance when casts are defined.
577
     * If no casts are configured, no DataCaster is created and null is returned.
578
     */
579
    protected function dataCaster(): ?DataCaster
580
    {
581
        if ($this->casts === []) {
168✔
582
            $this->dataCaster = null;
119✔
583

584
            return null;
119✔
585
        }
586

587
        if (! $this->dataCaster instanceof DataCaster) {
53✔
588
            $this->dataCaster = new DataCaster(
53✔
589
                array_merge($this->defaultCastHandlers, $this->castHandlers),
53✔
590
                null,
53✔
591
                null,
53✔
592
                false,
53✔
593
            );
53✔
594
        }
595

596
        return $this->dataCaster;
53✔
597
    }
598

599
    /**
600
     * Support for json_encode().
601
     *
602
     * @return array<string, mixed>
603
     */
604
    public function jsonSerialize(): array
605
    {
606
        return $this->toArray();
1✔
607
    }
608

609
    /**
610
     * Change the value of the private $_cast property.
611
     *
612
     * @return bool|Entity
613
     */
614
    public function cast(?bool $cast = null)
615
    {
616
        if ($cast === null) {
14✔
617
            return $this->_cast;
11✔
618
        }
619

620
        $this->_cast = $cast;
13✔
621

622
        return $this;
13✔
623
    }
624

625
    /**
626
     * Magic method to all protected/private class properties to be
627
     * easily set, either through a direct access or a
628
     * `setCamelCasedProperty()` method.
629
     *
630
     * Examples:
631
     *  $this->my_property = $p;
632
     *  $this->setMyProperty() = $p;
633
     *
634
     * @param array<int|string, mixed>|bool|float|int|object|string|null $value
635
     *
636
     * @return void
637
     *
638
     * @throws Exception
639
     */
640
    public function __set(string $key, $value = null)
641
    {
642
        $dbColumn = $this->mapProperty($key);
127✔
643

644
        // Check if the field should be mutated into a date
645
        if (in_array($dbColumn, $this->dates, true)) {
127✔
646
            $value = $this->mutateDate($value);
17✔
647
        }
648

649
        $value = $this->castAs($value, $dbColumn, 'set');
127✔
650

651
        // if a setter method exists for this key, use that method to
652
        // insert this value. should be outside $isNullable check,
653
        // so maybe wants to do sth with null value automatically
654
        $method = 'set' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $dbColumn)));
119✔
655

656
        // If a "`_set` + $key" method exists, it is a setter.
657
        if (method_exists($this, '_' . $method)) {
119✔
658
            $this->{'_' . $method}($value);
1✔
659

660
            return;
1✔
661
        }
662

663
        // If a "`set` + $key" method exists, it is also a setter.
664
        if (method_exists($this, $method)) {
118✔
665
            $this->{$method}($value);
12✔
666

667
            return;
12✔
668
        }
669

670
        // Otherwise, just the value. This allows for creation of new
671
        // class properties that are undefined, though they cannot be
672
        // saved. Useful for grabbing values through joins, assigning
673
        // relationships, etc.
674
        $this->attributes[$dbColumn] = $value;
109✔
675
    }
676

677
    /**
678
     * Magic method to allow retrieval of protected and private class properties
679
     * either by their name, or through a `getCamelCasedProperty()` method.
680
     *
681
     * Examples:
682
     *  $p = $this->my_property
683
     *  $p = $this->getMyProperty()
684
     *
685
     * @return array<int|string, mixed>|bool|float|int|object|string|null
686
     *
687
     * @throws Exception
688
     */
689
    public function __get(string $key)
690
    {
691
        $dbColumn = $this->mapProperty($key);
91✔
692

693
        $result = null;
91✔
694

695
        // Convert to CamelCase for the method
696
        $method = 'get' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $dbColumn)));
91✔
697

698
        // if a getter method exists for this key,
699
        // use that method to insert this value.
700
        if (method_exists($this, '_' . $method)) {
91✔
701
            // If a "`_get` + $key" method exists, it is a getter.
702
            $result = $this->{'_' . $method}();
1✔
703
        } elseif (method_exists($this, $method)) {
90✔
704
            // If a "`get` + $key" method exists, it is also a getter.
705
            $result = $this->{$method}();
9✔
706
        }
707

708
        // Otherwise return the protected property
709
        // if it exists.
710
        elseif (array_key_exists($dbColumn, $this->attributes)) {
87✔
711
            $result = $this->attributes[$dbColumn];
85✔
712
        }
713

714
        // Do we need to mutate this into a date?
715
        if (in_array($dbColumn, $this->dates, true)) {
91✔
716
            $result = $this->mutateDate($result);
18✔
717
        }
718
        // Or cast it as something?
719
        elseif ($this->_cast) {
85✔
720
            $result = $this->castAs($result, $dbColumn);
85✔
721
        }
722

723
        return $result;
86✔
724
    }
725

726
    /**
727
     * Returns true if a property exists names $key, or a getter method
728
     * exists named like for __get().
729
     */
730
    public function __isset(string $key): bool
731
    {
732
        if ($this->isMappedDbColumn($key)) {
6✔
733
            return false;
2✔
734
        }
735

736
        $dbColumn = $this->mapProperty($key);
6✔
737

738
        $method = 'get' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $dbColumn)));
6✔
739

740
        if (method_exists($this, $method)) {
6✔
741
            return true;
1✔
742
        }
743

744
        return isset($this->attributes[$dbColumn]);
6✔
745
    }
746

747
    /**
748
     * Unsets an attribute property.
749
     */
750
    public function __unset(string $key): void
751
    {
752
        if ($this->isMappedDbColumn($key)) {
4✔
753
            return;
1✔
754
        }
755

756
        $dbColumn = $this->mapProperty($key);
4✔
757

758
        unset($this->attributes[$dbColumn]);
4✔
759
    }
760

761
    /**
762
     * Whether this key is mapped db column name?
763
     */
764
    protected function isMappedDbColumn(string $key): bool
765
    {
766
        $dbColumn = $this->mapProperty($key);
8✔
767

768
        // The $key is a property name which has mapped db column name
769
        if ($key !== $dbColumn) {
8✔
770
            return false;
5✔
771
        }
772

773
        return $this->hasMappedProperty($key);
6✔
774
    }
775

776
    /**
777
     * Whether this key has mapped property?
778
     */
779
    protected function hasMappedProperty(string $key): bool
780
    {
781
        $property = array_search($key, $this->datamap, true);
6✔
782

783
        return $property !== false;
6✔
784
    }
785
}
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

© 2026 Coveralls, Inc