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\Extensions\ExtensibleClassMetadata; |
9
|
|
|
use LaravelDoctrine\Fluent\Extensions\Extension; |
10
|
|
|
|
11
|
|
|
class NestedSet extends TreeStrategy implements Buildable, Extension |
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
|
|
|
/** |
31
|
|
|
* Enable extension |
32
|
|
|
*/ |
33
|
|
|
public static function enable() |
34
|
|
|
{ |
35
|
|
|
TreeLeft::enable(); |
36
|
|
|
TreeRight::enable(); |
37
|
|
|
TreeLevel::enable(); |
38
|
|
|
TreeRoot::enable(); |
39
|
|
|
TreeParent::enable(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $field |
44
|
|
|
* @param string $type |
45
|
|
|
* @param callable|null $callback |
46
|
|
|
* |
47
|
|
|
* @throws InvalidMappingException |
48
|
|
|
* @return $this |
49
|
|
|
*/ |
50
|
31 |
View Code Duplication |
public function left($field = 'left', $type = 'integer', callable $callback = null) |
|
|
|
|
51
|
|
|
{ |
52
|
31 |
|
$this->validateNumericField($type, $field); |
53
|
31 |
|
|
54
|
|
|
$this->mapField($type, $field, $callback); |
55
|
|
|
|
56
|
|
|
$this->left = $field; |
57
|
|
|
|
58
|
83 |
|
return $this; |
59
|
|
|
} |
60
|
83 |
|
|
61
|
83 |
|
/** |
62
|
83 |
|
* @param string $field |
63
|
83 |
|
* @param string $type |
64
|
83 |
|
* @param callable|null $callback |
65
|
83 |
|
* |
66
|
|
|
* @throws InvalidMappingException |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function right($field = 'right', $type = 'integer', callable $callback = null) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->validateNumericField($type, $field); |
72
|
|
|
|
73
|
|
|
$this->mapField($type, $field, $callback); |
74
|
|
|
|
75
|
24 |
|
$this->right = $field; |
76
|
|
|
|
77
|
24 |
|
return $this; |
78
|
|
|
} |
79
|
23 |
|
|
80
|
|
|
/** |
81
|
23 |
|
* @param string $field |
82
|
|
|
* @param callable|null $callback |
83
|
23 |
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function root($field = 'root', callable $callback = null) |
87
|
|
|
{ |
88
|
|
|
$this->addSelfReferencingRelation($field, $callback); |
89
|
|
|
|
90
|
|
|
$this->root = $field; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
24 |
|
|
95
|
|
|
/** |
96
|
24 |
|
* Execute the build process |
97
|
|
|
*/ |
98
|
23 |
|
public function build() |
99
|
|
|
{ |
100
|
23 |
|
$this->addDefaults(); |
101
|
|
|
|
102
|
23 |
|
$this->builder->entity()->setRepositoryClass(NestedTreeRepository::class); |
103
|
|
|
|
104
|
|
|
/** @var ExtensibleClassMetadata $classMetadata */ |
105
|
|
|
$classMetadata = $this->builder->getBuilder()->getClassMetadata(); |
106
|
|
|
$classMetadata->mergeExtension($this->getExtensionName(), $this->getValues()); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Add default values to all required fields. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
6 |
|
*/ |
114
|
|
|
protected function addDefaults() |
115
|
6 |
|
{ |
116
|
|
|
if (!$this->parent) { |
117
|
5 |
|
$this->parent(); |
118
|
|
|
} |
119
|
5 |
|
|
120
|
|
|
if (!$this->left) { |
121
|
5 |
|
$this->left(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (!$this->right) { |
125
|
|
|
$this->right(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function getValues() |
130
|
3 |
|
{ |
131
|
|
|
return array_merge(parent::getValues(), [ |
132
|
3 |
|
'strategy' => 'nested', |
133
|
|
|
'left' => $this->left, |
134
|
3 |
|
'right' => $this->right, |
135
|
|
|
'root' => $this->root, |
136
|
3 |
|
]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
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.