1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/mangan |
7
|
|
|
* @licence AGPL or Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
9
|
|
|
* @copyright Copyright (c) Maslosoft |
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
11
|
|
|
* @link https://maslosoft.com/mangan/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Annotations; |
15
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Helpers\ParamsExpander; |
17
|
|
|
use Maslosoft\Mangan\Decorators\EmbedRefDecorator; |
18
|
|
|
use Maslosoft\Mangan\Decorators\RelatedDecorator; |
19
|
|
|
use Maslosoft\Mangan\Interfaces\SortInterface; |
20
|
|
|
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation; |
21
|
|
|
use Maslosoft\Mangan\Meta\RelatedMeta; |
22
|
|
|
use UnexpectedValueException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* RelatedAnnotation |
26
|
|
|
* Shorthand notation: |
27
|
|
|
* |
28
|
|
|
* Related(Company\Project\Projects, join = {'_id' = 'entity_id'}, sort = {'_id' = 1}, true) |
29
|
|
|
* |
30
|
|
|
* Expanded notation: |
31
|
|
|
* |
32
|
|
|
* Related(class = Company\Project\Projects, join = {'_id' => 'entity_id'}, sort = {'_id' = 1}, updatable = true) |
33
|
|
|
* |
34
|
|
|
* |
35
|
|
|
* @Conflicts('Embedded') |
36
|
|
|
* @Conflicts('EmbeddedArray') |
37
|
|
|
* @Conflicts('DbRef') |
38
|
|
|
* @Conflicts('DbRefArray') |
39
|
|
|
* @Conflicts('RelatedArray') |
40
|
|
|
* |
41
|
|
|
* |
42
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
43
|
|
|
*/ |
44
|
|
|
class RelatedAnnotation extends ManganPropertyAnnotation |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
public $class; |
48
|
|
|
public $join; |
49
|
|
|
public $condition; |
50
|
|
|
public $updatable; |
51
|
|
|
public $value; |
52
|
|
|
|
53
|
2 |
|
public function init() |
54
|
|
|
{ |
55
|
2 |
|
$relMeta = $this->_getMeta(); |
56
|
2 |
|
$relMeta->single = true; |
57
|
2 |
|
$relMeta->isArray = false; |
58
|
2 |
|
$this->getEntity()->related = $relMeta; |
59
|
2 |
|
$this->getEntity()->propagateEvents = true; |
60
|
2 |
|
$this->getEntity()->owned = true; |
61
|
2 |
|
$this->getEntity()->decorators[] = RelatedDecorator::class; |
62
|
2 |
|
$this->getEntity()->decorators[] = EmbedRefDecorator::class; |
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* |
67
|
|
|
* @return RelatedMeta |
68
|
|
|
*/ |
69
|
4 |
|
protected function _getMeta() |
70
|
|
|
{ |
71
|
4 |
|
$data = ParamsExpander::expand($this, ['class', 'join', 'sort', 'updatable']); |
72
|
|
|
|
73
|
|
|
// Do not expand `condition` with params expander for backward compat. |
74
|
4 |
|
if(isset($this->value['condition'])) |
75
|
|
|
{ |
76
|
1 |
|
$data['condition'] = $this->value['condition']; |
77
|
|
|
} |
78
|
4 |
|
if (empty($this->getEntity()->related)) |
79
|
|
|
{ |
80
|
4 |
|
$relMeta = new RelatedMeta(); |
81
|
|
|
} |
82
|
|
|
else |
83
|
|
|
{ |
84
|
|
|
$relMeta = $this->getEntity()->related; |
85
|
|
|
} |
86
|
4 |
|
foreach ($data as $key => $val) |
87
|
|
|
{ |
88
|
4 |
|
$relMeta->$key = $val; |
89
|
|
|
} |
90
|
4 |
|
if (!$relMeta->class) |
91
|
|
|
{ |
92
|
1 |
|
$relMeta->class = $this->getMeta()->type()->name; |
93
|
|
|
} |
94
|
4 |
|
if (empty($relMeta->join) && empty($relMeta->condition)) |
95
|
|
|
{ |
96
|
|
|
throw new UnexpectedValueException(sprintf('Parameter `join` or `condition` is required for `%s`, model `%s`, field `%s`', static::class, $this->getMeta()->type()->name, $this->getEntity()->name)); |
97
|
|
|
} |
98
|
4 |
|
if (empty($relMeta->sort)) |
99
|
|
|
{ |
100
|
4 |
|
$relMeta->sort = [ |
101
|
|
|
'_id' => SortInterface::SortAsc |
102
|
|
|
]; |
103
|
|
|
} |
104
|
4 |
|
return $relMeta; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|