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