|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Storage package |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Michal Wachowski <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Moss\Storage\Model; |
|
13
|
|
|
|
|
14
|
|
|
use Moss\Storage\NormalizeNamespaceTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Registry containing models |
|
18
|
|
|
* |
|
19
|
|
|
* @author Michal Wachowski <[email protected]> |
|
20
|
|
|
* @package Moss\Storage |
|
21
|
|
|
*/ |
|
22
|
|
|
class ModelBag |
|
23
|
|
|
{ |
|
24
|
|
|
use NormalizeNamespaceTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ModelInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $collection = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ModelInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $byAlias = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var ModelInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $byEntity = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Construct |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $collection |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct($collection = []) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->all($collection); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Retrieves offset value |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $alias |
|
55
|
|
|
* |
|
56
|
|
|
* @return ModelInterface |
|
57
|
|
|
* @throws ModelException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function get($alias) |
|
60
|
|
|
{ |
|
61
|
|
|
$alias = $this->normalizeNamespace($alias); |
|
62
|
|
|
|
|
63
|
|
|
if (isset($this->byAlias[$alias])) { |
|
64
|
|
|
return $this->byAlias[$alias]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (isset($this->byEntity[$alias])) { |
|
68
|
|
|
return $this->byEntity[$alias]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
throw new ModelException(sprintf('Model for entity "%s" does not exists', $alias)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Sets value to offset |
|
76
|
|
|
* |
|
77
|
|
|
* @param ModelInterface $model |
|
78
|
|
|
* @param string $alias |
|
79
|
|
|
* |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
|
|
public function set(ModelInterface $model, $alias = null) |
|
83
|
|
|
{ |
|
84
|
|
|
$hash = spl_object_hash($model); |
|
85
|
|
|
|
|
86
|
|
|
$this->collection[$hash] = &$model; |
|
87
|
|
|
|
|
88
|
|
|
if ($alias !== null) { |
|
89
|
|
|
$this->byAlias[$model->alias($alias)] = &$this->collection[$hash]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->byEntity[$this->normalizeNamespace($model->entity())] = &$this->collection[$hash]; |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Returns true if offset exists in bag |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $alias |
|
101
|
|
|
* |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function has($alias) |
|
105
|
|
|
{ |
|
106
|
|
|
$alias = $this->normalizeNamespace($alias); |
|
107
|
|
|
|
|
108
|
|
|
if (isset($this->byAlias[$alias]) || isset($this->byEntity[$alias])) { |
|
109
|
|
|
return true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return false; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns all options |
|
117
|
|
|
* If array passed, becomes bag content |
|
118
|
|
|
* |
|
119
|
|
|
* @param array $array overwrites values |
|
120
|
|
|
* |
|
121
|
|
|
* @return ModelInterface[] |
|
122
|
|
|
*/ |
|
123
|
|
|
public function all($array = []) |
|
124
|
|
|
{ |
|
125
|
|
|
if (!empty($array)) { |
|
126
|
|
|
foreach ($array as $key => $model) { |
|
127
|
|
|
$this->set($model, is_numeric($key) ? null : $key); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->byAlias; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|