1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
4
|
|
|
|
5
|
|
|
use Gedmo\Exception\InvalidMappingException; |
6
|
|
|
use Gedmo\Tree\Mapping\Driver\Fluent as FluentDriver; |
7
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
8
|
|
|
use LaravelDoctrine\Fluent\Builders\Field; |
9
|
|
|
use LaravelDoctrine\Fluent\Extensions\ExtensibleClassMetadata; |
10
|
|
|
|
11
|
|
|
class TreePath implements Buildable |
12
|
|
|
{ |
13
|
|
|
const MACRO_METHOD = 'treePath'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ExtensibleClassMetadata |
17
|
|
|
*/ |
18
|
|
|
protected $classMetadata; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $fieldName; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $separator; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var mixed |
32
|
|
|
*/ |
33
|
|
|
protected $appendId = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var bool |
37
|
|
|
*/ |
38
|
|
|
protected $startsWithSeparator = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var bool |
42
|
|
|
*/ |
43
|
|
|
protected $endsWithSeparator = true; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ExtensibleClassMetadata $classMetadata |
47
|
|
|
* @param string $fieldName |
48
|
|
|
* @param string $separator |
49
|
|
|
*/ |
50
|
4 |
|
public function __construct(ExtensibleClassMetadata $classMetadata, $fieldName, $separator = '|') |
51
|
|
|
{ |
52
|
4 |
|
$this->classMetadata = $classMetadata; |
53
|
4 |
|
$this->fieldName = $fieldName; |
54
|
4 |
|
$this->separator = $separator; |
55
|
4 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Enable TreePath |
59
|
|
|
*/ |
60
|
|
|
public static function enable() |
61
|
|
|
{ |
62
|
2 |
|
Field::macro(self::MACRO_METHOD, function (Field $field, $separator = '|') { |
63
|
1 |
|
return new static($field->getClassMetadata(), $field->getName(), $separator); |
|
|
|
|
64
|
2 |
|
}); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Execute the build process |
69
|
|
|
*/ |
70
|
3 |
View Code Duplication |
public function build() |
|
|
|
|
71
|
|
|
{ |
72
|
3 |
|
if (strlen($this->separator) > 1) { |
73
|
1 |
|
throw new InvalidMappingException("Tree Path field - [{$this->fieldName}] Separator {$this->separator} is invalid. It must be only one character long."); |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
$this->classMetadata->appendExtension($this->getExtensionName(), [ |
77
|
2 |
|
'path' => $this->fieldName, |
78
|
2 |
|
'path_separator' => $this->separator, |
79
|
2 |
|
'path_append_id' => $this->appendId, |
80
|
2 |
|
'path_starts_with_separator' => $this->startsWithSeparator, |
81
|
2 |
|
'path_ends_with_separator' => $this->endsWithSeparator |
82
|
2 |
|
]); |
83
|
2 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return the name of the actual extension. |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
2 |
|
public function getExtensionName() |
91
|
|
|
{ |
92
|
2 |
|
return FluentDriver::EXTENSION_NAME; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $separator |
97
|
|
|
* @return TreePath |
98
|
|
|
*/ |
99
|
2 |
|
public function separator($separator) |
100
|
|
|
{ |
101
|
2 |
|
$this->separator = $separator; |
102
|
|
|
|
103
|
2 |
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param mixed $appendId |
108
|
|
|
* @return TreePath |
109
|
|
|
*/ |
110
|
1 |
|
public function appendId($appendId) |
111
|
|
|
{ |
112
|
1 |
|
$this->appendId = $appendId; |
113
|
|
|
|
114
|
1 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param bool $startsWithSeparator |
119
|
|
|
* @return TreePath |
120
|
|
|
*/ |
121
|
1 |
|
public function startsWithSeparator($startsWithSeparator = true) |
122
|
|
|
{ |
123
|
1 |
|
$this->startsWithSeparator = $startsWithSeparator; |
124
|
|
|
|
125
|
1 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param bool $endsWithSeparator |
130
|
|
|
* @return TreePath |
131
|
|
|
*/ |
132
|
1 |
|
public function endsWithSeparator($endsWithSeparator = true) |
133
|
|
|
{ |
134
|
1 |
|
$this->endsWithSeparator = $endsWithSeparator; |
135
|
|
|
|
136
|
1 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.