1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* components |
4
|
|
|
* |
5
|
|
|
* @author Wolfy-J |
6
|
|
|
*/ |
7
|
|
|
namespace Spiral\ODM; |
8
|
|
|
|
9
|
|
|
use Spiral\Core\Component; |
10
|
|
|
use Spiral\Core\Exceptions\ScopeException; |
11
|
|
|
use Spiral\Core\Traits\SaturateTrait; |
12
|
|
|
use Spiral\Models\SchematicEntity; |
13
|
|
|
use Spiral\ODM\Entities\DocumentInstantiator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Primary class for spiral ODM, provides ability to pack it's own updates in a form of atomic |
17
|
|
|
* updates. |
18
|
|
|
* |
19
|
|
|
* You can use same properties to configure entity as in DataEntity + schema property. |
20
|
|
|
* |
21
|
|
|
* Example: |
22
|
|
|
* |
23
|
|
|
* class Test extends DocumentEntity |
24
|
|
|
* { |
25
|
|
|
* const SCHEMA = [ |
26
|
|
|
* 'name' => 'string' |
27
|
|
|
* ]; |
28
|
|
|
* } |
29
|
|
|
* |
30
|
|
|
* Configuration properties: |
31
|
|
|
* - schema |
32
|
|
|
* - defaults |
33
|
|
|
* - secured (* by default) |
34
|
|
|
* - fillable |
35
|
|
|
*/ |
36
|
|
|
abstract class DocumentEntity extends SchematicEntity implements CompositableInterface |
37
|
|
|
{ |
38
|
|
|
use SaturateTrait; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set of schema sections needed to describe entity behaviour. |
42
|
|
|
*/ |
43
|
|
|
const SH_INSTANTIATION = 0; |
44
|
|
|
const SH_DEFAULTS = 1; |
45
|
|
|
const SH_AGGREGATIONS = 6; |
46
|
|
|
const SH_COMPOSITIONS = 7; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Class responsible for instance construction. |
50
|
|
|
*/ |
51
|
|
|
const INSTANTIATOR = DocumentInstantiator::class; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Document fields, accessors and relations. ODM will generate setters and getters for some |
55
|
|
|
* fields based on their types. |
56
|
|
|
* |
57
|
|
|
* Example, fields: |
58
|
|
|
* const SCHEMA = [ |
59
|
|
|
* '_id' => 'MongoId', //Primary key field |
60
|
|
|
* 'value' => 'string', //Default string field |
61
|
|
|
* 'values' => ['string'] //ScalarArray accessor will be applied for fields like that |
62
|
|
|
* ]; |
63
|
|
|
* |
64
|
|
|
* Compositions: |
65
|
|
|
* const SCHEMA = [ |
66
|
|
|
* ..., |
67
|
|
|
* 'child' => Child::class, //One document are composited, for example user Profile |
68
|
|
|
* 'many' => [Child::class] //Compositor accessor will be applied, allows to |
69
|
|
|
* //composite many document instances |
70
|
|
|
* ]; |
71
|
|
|
* |
72
|
|
|
* Documents can extend each other, in this case schema will also be inherited. |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
const SCHEMA = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Default field values. |
80
|
|
|
* |
81
|
|
|
* @var array |
82
|
|
|
*/ |
83
|
|
|
const DEFAULTS = []; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Model behaviour configurations. |
87
|
|
|
*/ |
88
|
|
|
const SECURED = '*'; |
89
|
|
|
const HIDDEN = []; |
90
|
|
|
const FILLABLE = []; |
91
|
|
|
const SETTERS = []; |
92
|
|
|
const GETTERS = []; |
93
|
|
|
const ACCESSORS = []; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Parent ODM instance, responsible for aggregations and lazy loading operations. |
97
|
|
|
* |
98
|
|
|
* @invisible |
99
|
|
|
* @var ODMInterface |
100
|
|
|
*/ |
101
|
|
|
protected $odm; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Document behaviour schema. |
105
|
|
|
* |
106
|
|
|
* @var array |
107
|
|
|
*/ |
108
|
|
|
private $schema = []; |
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
* |
113
|
|
|
* @param ODMInterface $odm To lazy create nested document ang aggregations. |
114
|
|
|
* |
115
|
|
|
* @throws ScopeException When no ODM instance can be resolved. |
116
|
|
|
*/ |
117
|
|
|
public function __construct($fields, array $schema = null, ODMInterface $odm = null) |
118
|
|
|
{ |
119
|
|
|
//We can use global container as fallback if no default values were provided |
120
|
|
|
$this->odm = $this->saturate($odm, ODMInterface::class); |
121
|
|
|
|
122
|
|
|
//Use supplied schema or fetch one from ODM |
123
|
|
|
$this->schema = !empty($schema) ? $schema : $this->odm->schema( |
124
|
|
|
static::class, |
125
|
|
|
ODMInterface::D_SCHEMA |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$fields = is_array($fields) ? $fields : []; |
129
|
|
|
if (!empty($this->schema[self::SH_DEFAULTS])) { |
130
|
|
|
//Merging with default values |
131
|
|
|
$fields = array_replace_recursive($this->schema[self::SH_DEFAULTS], $fields); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
parent::__construct($fields, $this->schema); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function withODM() |
138
|
|
|
{ |
139
|
|
|
//????? |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function hasUpdates(): bool |
146
|
|
|
{ |
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function buildAtomics(string $container = ''): array |
154
|
|
|
{ |
155
|
|
|
// TODO: Implement buildAtomics() method. |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function flushUpdates() |
162
|
|
|
{ |
163
|
|
|
// TODO: Implement flushUpdates() method. |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
public function __debugInfo() |
170
|
|
|
{ |
171
|
|
|
return [ |
172
|
|
|
'fields' => $this->getFields(), |
173
|
|
|
'atomics' => $this->hasUpdates() ? $this->buildAtomics() : [] |
174
|
|
|
]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
protected function iocContainer() |
181
|
|
|
{ |
182
|
|
|
if (!empty($this->odm) || $this->odm instanceof Component) { |
183
|
|
|
//Forwarding IoC scope to parent ODM instance |
184
|
|
|
return $this->odm->iocContainer(); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return parent::iocContainer(); |
188
|
|
|
} |
189
|
|
|
} |