-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStrictEntity.php
More file actions
60 lines (46 loc) · 1.49 KB
/
StrictEntity.php
File metadata and controls
60 lines (46 loc) · 1.49 KB
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
<?php
declare(strict_types=1);
namespace Tests\Support\Entities;
use CodeIgniter\Entity\Entity;
use LogicException;
abstract class StrictEntity extends Entity
{
protected $dates = [];
public function __construct(?array $data = null)
{
parent::__construct(is_array($data) ? $this->filterAttributes($data) : []);
}
public function fill(?array $data = null)
{
return parent::fill(is_array($data) ? $this->filterAttributes($data) : []);
}
public function injectRawData(array $data)
{
return parent::injectRawData(array_merge($this->attributes, $this->filterAttributes($data)));
}
public function __set(string $key, $value = null)
{
$attribute = $this->mapProperty($key);
if (! array_key_exists($attribute, $this->attributes)) {
throw new LogicException(sprintf('Attribute "%s" is not defined.', $attribute));
}
parent::__set($key, $value);
}
public function __get(string $key)
{
$attribute = $this->mapProperty($key);
if (! array_key_exists($attribute, $this->attributes)) {
throw new LogicException(sprintf('Attribute "%s" is not defined.', $attribute));
}
return parent::__get($key);
}
/**
* @param array<string, mixed> $attributes
*
* @return array<string, mixed>
*/
protected function filterAttributes(array $attributes): array
{
return array_intersect_key($attributes, $this->attributes);
}
}