|
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\DbRefDecorator; |
|
18
|
|
|
use Maslosoft\Mangan\Decorators\EmbedRefDecorator; |
|
19
|
|
|
use Maslosoft\Mangan\Meta\DbRefMeta; |
|
20
|
|
|
use Maslosoft\Mangan\Meta\ManganPropertyAnnotation; |
|
21
|
|
|
use UnexpectedValueException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* DB reference annotation |
|
25
|
|
|
* |
|
26
|
|
|
* Most simple usage: |
|
27
|
|
|
* ``` |
|
28
|
|
|
* @DbRef(Vendor\Package\ClassLiteral) |
|
29
|
|
|
* ``` |
|
30
|
|
|
* |
|
31
|
|
|
* Disable updates, long notation: |
|
32
|
|
|
* ``` |
|
33
|
|
|
* @DbRef(Vendor\Package\ClassLiteral, 'updatable' = false) |
|
34
|
|
|
* ``` |
|
35
|
|
|
* |
|
36
|
|
|
* Disable updates, short notation: |
|
37
|
|
|
* ``` |
|
38
|
|
|
* @DbRef(Vendor\Package\ClassLiteral, false) |
|
39
|
|
|
* ``` |
|
40
|
|
|
* |
|
41
|
|
|
* NOTE: Nullable requires field to be not `updatable` |
|
42
|
|
|
* Nullable, long notation: |
|
43
|
|
|
* ``` |
|
44
|
|
|
* @DbRef(Vendor\Package\ClassLiteral, 'updatable' = false) |
|
45
|
|
|
* ``` |
|
46
|
|
|
* |
|
47
|
|
|
* Nullable, short notation: |
|
48
|
|
|
* ``` |
|
49
|
|
|
* @DbRef(Vendor\Package\ClassLiteral, false, false) |
|
50
|
|
|
* ``` |
|
51
|
|
|
* |
|
52
|
|
|
* @template DbRef(${class}, ${updatable}) |
|
53
|
|
|
* |
|
54
|
|
|
* @Conflicts('Embedded') |
|
55
|
|
|
* @Conflicts('EmbeddedArray') |
|
56
|
|
|
* @Conflicts('DbRefArray') |
|
57
|
|
|
* @Conflicts('Related') |
|
58
|
|
|
* @Conflicts('RelatedArray') |
|
59
|
|
|
* |
|
60
|
|
|
* @Target('property') |
|
61
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
62
|
|
|
*/ |
|
63
|
|
|
class DbRefAnnotation extends ManganPropertyAnnotation |
|
64
|
|
|
{ |
|
65
|
|
|
|
|
66
|
|
|
public $class; |
|
67
|
|
|
public $updatable; |
|
68
|
|
|
public $nullable; |
|
69
|
|
|
public $value; |
|
70
|
|
|
|
|
71
|
8 |
|
public function init() |
|
72
|
|
|
{ |
|
73
|
8 |
|
$refMeta = $this->getDbRefMeta(); |
|
74
|
8 |
|
$refMeta->single = true; |
|
75
|
8 |
|
$refMeta->isArray = false; |
|
76
|
8 |
|
$this->getEntity()->dbRef = $refMeta; |
|
77
|
8 |
|
$this->getEntity()->propagateEvents = true; |
|
78
|
8 |
|
$this->getEntity()->owned = true; |
|
79
|
8 |
|
$this->getEntity()->decorators[] = DbRefDecorator::class; |
|
80
|
8 |
|
$this->getEntity()->decorators[] = EmbedRefDecorator::class; |
|
81
|
8 |
|
} |
|
82
|
|
|
|
|
83
|
14 |
|
protected function getDbRefMeta() |
|
84
|
|
|
{ |
|
85
|
14 |
|
$data = ParamsExpander::expand($this, ['class', 'updatable', 'nullable']); |
|
86
|
|
|
|
|
87
|
|
|
$params = [ |
|
88
|
14 |
|
$this->getMeta()->type()->name, |
|
89
|
14 |
|
$this->getEntity()->name |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
14 |
|
$msg = vsprintf("Parameter `updatable` must be of type `boolean` (or be omitted) on %s:%s", $params); |
|
93
|
14 |
|
$msg2 = vsprintf("Parameter `nullable` must be of type `boolean` (or be omitted) on %s:%s", $params); |
|
94
|
|
|
|
|
95
|
14 |
|
assert(empty($data['updatable']) || is_bool($data['updatable']), new UnexpectedValueException($msg)); |
|
96
|
14 |
|
assert(empty($data['nullable']) || is_bool($data['nullable']), new UnexpectedValueException($msg2)); |
|
97
|
|
|
|
|
98
|
14 |
|
$refMeta = new DbRefMeta($data); |
|
99
|
14 |
|
if (!$refMeta->class) |
|
100
|
|
|
{ |
|
101
|
1 |
|
$refMeta->class = $this->getMeta()->type()->name; |
|
102
|
|
|
} |
|
103
|
14 |
|
if($refMeta->updatable && $refMeta->nullable) |
|
104
|
|
|
{ |
|
105
|
|
|
$msg3 = vsprintf("Both parameter `nullable` and `updatable` cannot be set to `true` on %s:%s.", $params); |
|
106
|
|
|
$msg3 .= 'This feature is not implemented'; |
|
107
|
|
|
throw new UnexpectedValueException($msg3); |
|
108
|
|
|
} |
|
109
|
14 |
|
$this->getEntity()->updatable = $refMeta->updatable; |
|
110
|
14 |
|
$this->getEntity()->nullable = $refMeta->nullable; |
|
111
|
14 |
|
return $refMeta; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|