1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace BristolSU\ControlDB\AdditionalProperties; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Handles storing the additional properties models can define using a local array |
7
|
|
|
*/ |
|
|
|
|
8
|
|
|
class AdditionalPropertySingletonStore implements AdditionalPropertyStore |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Properties that can be used |
13
|
|
|
* |
14
|
|
|
* Stores with the model as the key, then an array of property keys |
15
|
|
|
* |
16
|
|
|
* @var array [ 'ModelClassName' => ['student_id', 'faculty'] |
17
|
|
|
*/ |
18
|
|
|
private $properties = []; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Add a property to a model. |
22
|
|
|
* |
23
|
|
|
* Properties must be initialised before they can be used. |
24
|
|
|
* |
25
|
|
|
* @param string $model Model class name to add the property to |
26
|
|
|
* @param string $key Key of the property |
|
|
|
|
27
|
|
|
* @return void |
|
|
|
|
28
|
|
|
*/ |
29
|
47 |
|
public function addProperty(string $model, string $key): void { |
|
|
|
|
30
|
47 |
|
if(!$this->hasProperties($model)) { |
|
|
|
|
31
|
47 |
|
$this->properties[$model] = []; |
32
|
|
|
} |
33
|
47 |
|
$this->properties[$model][] = $key; |
34
|
47 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Does the given model have any properties |
38
|
|
|
* |
39
|
|
|
* @param string $model Full class name of the model |
40
|
|
|
* @return bool If the model has properties |
|
|
|
|
41
|
|
|
*/ |
42
|
447 |
|
public function hasProperties(string $model): bool |
43
|
|
|
{ |
44
|
447 |
|
return array_key_exists($model, $this->properties); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get all properties a model has |
49
|
|
|
* |
50
|
|
|
* @param string $model Full class name of the model |
51
|
|
|
* @return array Array of property keys. |
|
|
|
|
52
|
|
|
*/ |
53
|
445 |
|
public function getProperties(string $model): array |
54
|
|
|
{ |
55
|
445 |
|
if($this->hasProperties($model)) { |
|
|
|
|
56
|
46 |
|
return $this->properties[$model]; |
57
|
|
|
} |
58
|
401 |
|
return []; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |