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\Extensions\ExtensibleClassMetadata; |
9
|
|
|
use LaravelDoctrine\Fluent\Extensions\Extension; |
10
|
|
|
use LaravelDoctrine\Fluent\Fluent; |
11
|
|
|
|
12
|
|
|
abstract class TreeStrategy implements Buildable, Extension |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $parent; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $level; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Fluent |
26
|
|
|
*/ |
27
|
|
|
protected $builder; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Fluent $builder |
31
|
|
|
*/ |
32
|
|
|
public function __construct(Fluent $builder) |
33
|
|
|
{ |
34
|
|
|
$this->builder = $builder; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $field |
39
|
|
|
* @param string $type |
40
|
|
|
* @param callable|null $callback |
41
|
|
|
* |
42
|
|
|
* @throws InvalidMappingException |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function level($field = 'level', $type = 'integer', callable $callback = null) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$this->validateNumericField($type, $field); |
48
|
|
|
|
49
|
|
|
$this->mapField($type, $field, $callback); |
50
|
|
|
|
51
|
|
|
$this->level = $field; |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $field |
58
|
|
|
* @param callable|null $callback |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function parent($field = 'parent', callable $callback = null) |
63
|
|
|
{ |
64
|
|
|
$this->addSelfReferencingRelation($field, $callback); |
65
|
|
|
|
66
|
|
|
$this->parent = $field; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Return the name of the actual extension. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
protected function getExtensionName() |
77
|
|
|
{ |
78
|
|
|
return FluentDriver::EXTENSION_NAME; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $type |
83
|
|
|
* @param string $field |
84
|
|
|
* @param callable|null $callback |
85
|
|
|
* @param bool|false $nullable |
86
|
|
|
*/ |
87
|
|
|
protected function mapField($type, $field, callable $callback = null, $nullable = false) |
88
|
|
|
{ |
89
|
|
|
$this->builder->field($type, $field, $callback)->nullable($nullable); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $type |
94
|
|
|
* @param string $field |
95
|
|
|
* |
96
|
|
|
* @throws InvalidMappingException |
97
|
|
|
*/ |
98
|
|
|
protected function validateNumericField($type, $field) |
99
|
|
|
{ |
100
|
|
|
if (!in_array($type, ['integer', 'bigint', 'smallint'])) { |
101
|
|
|
throw new InvalidMappingException("Invalid type [$type] for the [$field] field. Must be a (small, big) integer type."); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns the name of the mapped class. |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function myself() |
111
|
|
|
{ |
112
|
|
|
return $this->getClassMetadata()->name; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $field |
117
|
|
|
* @param callable|null $callback |
118
|
|
|
*/ |
119
|
|
|
protected function addSelfReferencingRelation($field, callable $callback = null) |
120
|
|
|
{ |
121
|
|
|
$this->builder->belongsTo($this->myself(), $field, $callback)->nullable(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return ExtensibleClassMetadata |
126
|
|
|
*/ |
127
|
|
|
protected function getClassMetadata() |
128
|
|
|
{ |
129
|
|
|
return $this->builder->getBuilder()->getClassMetadata(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
protected function getValues() |
136
|
|
|
{ |
137
|
|
|
$values = []; |
138
|
|
|
|
139
|
|
|
if ($this->parent) { |
140
|
|
|
$values['parent'] = $this->parent; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($this->level) { |
144
|
|
|
$values['level'] = $this->level; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $values; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.