1 | <?php |
||
24 | trait UniqueModelsAwareTrait |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * Annotated model |
||
29 | * @var AnnotatedInterface[] |
||
30 | */ |
||
31 | private $models = []; |
||
32 | |||
33 | /** |
||
34 | * Annotated models |
||
35 | * @var AnnotatedInterface[] |
||
36 | */ |
||
37 | 32 | public function getModels() |
|
41 | |||
42 | /** |
||
43 | * |
||
44 | * @param AnnotatedInterface[] $models |
||
45 | * @return $this |
||
46 | */ |
||
47 | 33 | public function setModels($models) |
|
48 | { |
||
49 | // Unique based on class name |
||
50 | // See: http://stackoverflow.com/a/2561283/5444623 |
||
51 | $map = function($value) |
||
52 | { |
||
53 | 33 | if (is_object($value)) |
|
54 | { |
||
55 | 33 | return get_class($value); |
|
56 | } |
||
57 | return $value; |
||
58 | 33 | }; |
|
59 | 33 | $unique = array_intersect_key($models, array_unique(array_map($map, $models))); |
|
60 | 33 | $this->models = array_values($unique); |
|
61 | 33 | return $this; |
|
62 | } |
||
63 | |||
64 | /** |
||
65 | * Add model to set but only if it's instance is not present in set. |
||
66 | * |
||
67 | * @param AnnotatedInterface $model |
||
68 | * @return boolean Whether model was added |
||
69 | */ |
||
70 | 32 | public function addModel(AnnotatedInterface $model) |
|
79 | |||
80 | /** |
||
81 | * Remove model from set. |
||
82 | * |
||
83 | * @param AnnotatedInterface $model |
||
84 | * @return boolean Whether model was removed |
||
85 | */ |
||
86 | public function removeModel(AnnotatedInterface $model) |
||
104 | |||
105 | 32 | public function hasModel(AnnotatedInterface $model) |
|
116 | |||
117 | } |
||
118 |