1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelDoctrine\Fluent\Extensions\Gedmo; |
4
|
|
|
|
5
|
|
|
use Gedmo\Exception\InvalidMappingException; |
6
|
|
|
use Gedmo\Tree\Entity\Repository\NestedTreeRepository; |
7
|
|
|
use LaravelDoctrine\Fluent\Buildable; |
8
|
|
|
use LaravelDoctrine\Fluent\Builders\Delay; |
9
|
|
|
use LaravelDoctrine\Fluent\Extensions\Extension; |
10
|
|
|
|
11
|
|
|
class NestedSet extends TreeStrategy implements Buildable, Extension, Delay |
12
|
|
|
{ |
13
|
|
|
const MACRO_METHOD = 'nestedSet'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $left; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $right; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $root; |
29
|
|
|
|
30
|
|
|
public static function enable() |
31
|
|
|
{ |
32
|
|
|
parent::enable(); |
33
|
|
|
|
34
|
|
|
TreeLeft::enable(); |
35
|
|
|
TreeRight::enable(); |
36
|
|
|
TreeSelfReference::enableRoot(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $field |
41
|
|
|
* @param string $type |
42
|
|
|
* @param callable|null $callback |
43
|
|
|
* |
44
|
|
|
* @throws InvalidMappingException |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
public function left($field = 'left', $type = 'integer', callable $callback = null) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->validateNumericField($type, $field); |
50
|
|
|
|
51
|
|
|
$this->mapField($type, $field, $callback); |
52
|
|
|
|
53
|
|
|
$this->left = $field; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $field |
60
|
|
|
* @param string $type |
61
|
|
|
* @param callable|null $callback |
62
|
|
|
* |
63
|
|
|
* @throws InvalidMappingException |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
View Code Duplication |
public function right($field = 'right', $type = 'integer', callable $callback = null) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
$this->validateNumericField($type, $field); |
69
|
|
|
|
70
|
|
|
$this->mapField($type, $field, $callback); |
71
|
|
|
|
72
|
|
|
$this->right = $field; |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $field |
79
|
|
|
* @param callable|null $callback |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function root($field = 'root', callable $callback = null) |
84
|
|
|
{ |
85
|
|
|
$this->addSelfReferencingRelation($field, $callback); |
86
|
|
|
|
87
|
|
|
$this->root = $field; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Execute the build process |
94
|
|
|
*/ |
95
|
|
|
public function build() |
96
|
|
|
{ |
97
|
|
|
$this->builder->entity()->setRepositoryClass(NestedTreeRepository::class); |
98
|
|
|
|
99
|
|
|
parent::build(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Add default values to all required fields. |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
protected function defaults() |
108
|
|
|
{ |
109
|
|
|
parent::defaults(); |
110
|
|
|
|
111
|
|
|
if ($this->isMissingLeft()) { |
112
|
|
|
$this->left(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ($this->isMissingRight()) { |
116
|
|
|
$this->right(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function getValues() |
121
|
|
|
{ |
122
|
|
|
return array_merge(parent::getValues(), [ |
123
|
|
|
'strategy' => 'nested', |
124
|
|
|
'left' => $this->left, |
125
|
|
|
'right' => $this->right, |
126
|
|
|
'root' => $this->root, |
127
|
|
|
]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @return bool |
132
|
|
|
*/ |
133
|
|
|
private function isMissingLeft() |
134
|
|
|
{ |
135
|
|
|
return !$this->alreadyConfigured('left') && !$this->left; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
private function isMissingRight() |
142
|
|
|
{ |
143
|
|
|
return !$this->alreadyConfigured('right') && !$this->right; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.