1: <?php declare(strict_types=1);
2:
3: namespace Salient\Core\Concern;
4:
5: use Salient\Contract\Core\Extensible;
6: use Salient\Core\Introspector as IS;
7:
8: /**
9: * Implements Extensible to store arbitrary property values
10: *
11: * @see Extensible
12: */
13: trait ExtensibleTrait
14: {
15: /**
16: * Normalised property name => value
17: *
18: * @var array<string,mixed>
19: */
20: private $MetaProperties = [];
21:
22: /**
23: * Normalised property name => first name passed to setMetaProperty
24: *
25: * @var array<string,string>
26: */
27: private $MetaPropertyNames = [];
28:
29: final public function clearMetaProperties()
30: {
31: $this->MetaProperties = [];
32: $this->MetaPropertyNames = [];
33:
34: return $this;
35: }
36:
37: /**
38: * @param mixed $value
39: */
40: final public function setMetaProperty(string $name, $value): void
41: {
42: $normalised = IS::get(static::class)->maybeNormalise($name);
43: $this->MetaProperties[$normalised] = $value;
44: if (!array_key_exists($normalised, $this->MetaPropertyNames)) {
45: $this->MetaPropertyNames[$normalised] = $name;
46: }
47: }
48:
49: /**
50: * @return mixed
51: */
52: final public function getMetaProperty(string $name)
53: {
54: return $this->MetaProperties[IS::get(static::class)->maybeNormalise($name)] ?? null;
55: }
56:
57: final public function isMetaPropertySet(string $name): bool
58: {
59: return isset($this->MetaProperties[IS::get(static::class)->maybeNormalise($name)]);
60: }
61:
62: final public function unsetMetaProperty(string $name): void
63: {
64: unset($this->MetaProperties[IS::get(static::class)->maybeNormalise($name)]);
65: }
66:
67: final public function getMetaProperties(): array
68: {
69: return array_combine(
70: array_map(
71: fn(string $name): string => $this->MetaPropertyNames[$name],
72: array_keys($this->MetaProperties)
73: ),
74: $this->MetaProperties
75: );
76: }
77: }
78: