1: <?php declare(strict_types=1);
2:
3: namespace Salient\Core\Concern;
4:
5: use Salient\Contract\Core\Entity\Extensible;
6: use Salient\Core\Internal\ReadPropertyTrait;
7: use Salient\Core\Internal\WritePropertyTrait;
8: use Salient\Utility\Arr;
9:
10: /**
11: * Implements Extensible to store arbitrary property values
12: *
13: * @see Extensible
14: *
15: * @phpstan-require-implements Extensible
16: */
17: trait ExtensibleTrait
18: {
19: use ReadPropertyTrait;
20: use WritePropertyTrait;
21:
22: /**
23: * Normalised property name => value
24: *
25: * @var array<string,mixed>
26: */
27: protected $MetaProperties = [];
28:
29: /**
30: * Normalised property name => first name passed to __set
31: *
32: * @var array<string,string>
33: */
34: protected $MetaPropertyNames = [];
35:
36: /**
37: * @inheritDoc
38: */
39: public static function getDynamicPropertiesProperty(): string
40: {
41: return 'MetaProperties';
42: }
43:
44: /**
45: * @inheritDoc
46: */
47: public static function getDynamicPropertyNamesProperty(): string
48: {
49: return 'MetaPropertyNames';
50: }
51:
52: /**
53: * @inheritDoc
54: */
55: final public function setDynamicProperties(array $values): void
56: {
57: $this->MetaProperties = [];
58: $this->MetaPropertyNames = [];
59:
60: foreach ($values as $name => $value) {
61: $this->__set($name, $value);
62: }
63: }
64:
65: final public function getDynamicProperties(): array
66: {
67: return Arr::combine(
68: array_map(
69: fn(string $name): string => $this->MetaPropertyNames[$name],
70: array_keys($this->MetaProperties)
71: ),
72: $this->MetaProperties
73: );
74: }
75: }
76: