1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Management; |
4
|
|
|
|
5
|
|
|
class Registration |
6
|
|
|
{ |
7
|
|
|
private $key; |
8
|
|
|
private $managerClass; |
9
|
|
|
private $modelClass; |
10
|
|
|
private $tags; |
11
|
|
|
|
12
|
433 |
|
public function __construct(string $managerClass, string $modelClass, array $tags = []) |
13
|
|
|
{ |
14
|
433 |
|
$this->validate($managerClass, $modelClass); |
15
|
|
|
|
16
|
433 |
|
$this->key = $modelClass::managedModelKey(); |
17
|
433 |
|
$this->managerClass = $managerClass; |
18
|
433 |
|
$this->modelClass = $modelClass; |
19
|
433 |
|
$this->tags = $tags; |
20
|
433 |
|
} |
21
|
|
|
|
22
|
|
|
public static function fromArray(array $registration) |
23
|
|
|
{ |
24
|
|
|
return new static(...array_values($registration)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Return the key of the first entry. |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
433 |
|
public function key(): string |
33
|
|
|
{ |
34
|
433 |
|
return $this->key; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Return the manager class |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function class(): string |
43
|
|
|
{ |
44
|
|
|
return $this->managerClass; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return the model class |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
129 |
|
public function model(): string |
53
|
|
|
{ |
54
|
129 |
|
return $this->modelClass; |
55
|
|
|
} |
56
|
|
|
|
57
|
16 |
|
public function tags(): array |
58
|
|
|
{ |
59
|
16 |
|
return $this->tags; |
60
|
|
|
} |
61
|
|
|
|
62
|
124 |
|
public function has(string $key, $value): bool |
63
|
|
|
{ |
64
|
124 |
|
if ($key == 'tags') { |
65
|
17 |
|
if (is_array($value)) { |
66
|
17 |
|
return (count(array_intersect($this->tags, $value)) > 0); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return in_array($value, $this->tags); |
70
|
|
|
} |
71
|
|
|
|
72
|
124 |
|
return $this->$key == $value; |
73
|
|
|
} |
74
|
|
|
|
75
|
433 |
|
private function validate($managerClass, $modelClass) |
76
|
|
|
{ |
77
|
433 |
|
if (!class_exists($managerClass)) { |
78
|
2 |
|
throw new \InvalidArgumentException('Manager class ['.$managerClass.'] is an invalid class reference. Please make sure the class exists.'); |
79
|
|
|
} |
80
|
|
|
|
81
|
433 |
|
if (!class_exists($modelClass)) { |
82
|
1 |
|
throw new \InvalidArgumentException('Model class ['.$modelClass.'] is an invalid model reference. Please make sure the class exists.'); |
83
|
|
|
} |
84
|
|
|
|
85
|
433 |
|
$manager = new \ReflectionClass($managerClass); |
86
|
433 |
|
if (! $manager->implementsInterface(Manager::class)) { |
87
|
|
|
throw new \InvalidArgumentException('Class ['.$managerClass.'] is expected to implement the ['.Manager::class.'] contract.'); |
88
|
|
|
} |
89
|
|
|
|
90
|
433 |
|
$model = new \ReflectionClass($modelClass); |
91
|
433 |
|
if (! $model->implementsInterface(ManagedModel::class)) { |
92
|
|
|
throw new \InvalidArgumentException('Class ['.$modelClass.'] is expected to implement the ['.ManagedModel::class.'] contract.'); |
93
|
|
|
} |
94
|
433 |
|
} |
95
|
|
|
} |
96
|
|
|
|