1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
4
|
|
|
|
5
|
|
|
use Gedmo\Tree\Mapping\Driver\Fluent as FluentDriver; |
6
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
7
|
|
|
use LaravelDoctrine\Fluent\Builders\Field; |
8
|
|
|
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
9
|
|
|
use LaravelDoctrine\Fluent\Relations\ManyToOne; |
10
|
|
|
|
11
|
|
|
class TreeSelfReference implements Buildable |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ExtensibleClassMetadata |
15
|
|
|
*/ |
16
|
|
|
protected $classMetadata; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $fieldName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $key; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ExtensibleClassMetadata $classMetadata |
30
|
|
|
* @param string $fieldName |
31
|
|
|
* @param string $key |
32
|
|
|
*/ |
33
|
13 |
|
public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName, $key) |
34
|
|
|
{ |
35
|
13 |
|
$this->classMetadata = $classMetadata; |
36
|
13 |
|
$this->fieldName = $fieldName; |
37
|
13 |
|
$this->key = $key; |
38
|
13 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Enable TreeRoot and TreeParent macros. |
42
|
|
|
* |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
8 |
|
public static function enable() |
46
|
|
|
{ |
47
|
8 |
|
static::enableRoot(); |
48
|
8 |
|
static::enableParent(); |
49
|
8 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Enable only the TreeRoot macro. |
53
|
|
|
* |
54
|
|
|
* @return void |
55
|
|
|
*/ |
56
|
92 |
|
public static function enableRoot() |
57
|
|
|
{ |
58
|
92 |
|
static::addMacro('treeRoot', 'root'); |
59
|
92 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Enable only the TreeParent macro. |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
93 |
|
public static function enableParent() |
67
|
|
|
{ |
68
|
93 |
|
static::addMacro('treeParent', 'parent'); |
69
|
93 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $method |
73
|
|
|
* @param string $key |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
93 |
|
protected static function addMacro($method, $key) |
78
|
|
|
{ |
79
|
|
|
Field::macro($method, function(Field $field) use ($key) { |
80
|
4 |
|
$field->nullable(); |
81
|
|
|
|
82
|
4 |
|
return new static($field->getClassMetadata(), $field->getName(), $key); |
|
|
|
|
83
|
93 |
|
}); |
84
|
|
|
|
85
|
93 |
|
ManyToOne::macro($method, function (ManyToOne $relation) use ($key) { |
86
|
7 |
|
$relation->nullable(); |
87
|
|
|
|
88
|
7 |
|
return new static($relation->getClassMetadata(), $relation->getRelation(), $key); |
|
|
|
|
89
|
93 |
|
}); |
90
|
93 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Execute the build process |
94
|
|
|
*/ |
95
|
9 |
|
public function build() |
96
|
|
|
{ |
97
|
9 |
|
$this->classMetadata->mergeExtension($this->getExtensionName(), [ |
98
|
9 |
|
$this->key => $this->fieldName, |
99
|
9 |
|
]); |
100
|
9 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Return the name of the actual extension. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
9 |
|
public function getExtensionName() |
108
|
|
|
{ |
109
|
9 |
|
return FluentDriver::EXTENSION_NAME; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.