1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StoutLogic\AcfBuilder; |
4
|
|
|
|
5
|
|
|
class FieldManager |
6
|
|
|
{ |
7
|
|
|
private $fields; |
8
|
|
|
|
9
|
|
|
public function __construct($fields = []) |
10
|
|
|
{ |
11
|
|
|
$this->fields = $fields; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
public function getFields() |
15
|
|
|
{ |
16
|
|
|
return $this->fields; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function getCount() |
20
|
|
|
{ |
21
|
|
|
return count($this->getFields()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function pushField($field) |
25
|
|
|
{ |
26
|
|
|
$this->insertFields($field, $this->getCount()); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function popField() |
30
|
|
|
{ |
31
|
|
|
if ($this->getCount() > 0) { |
32
|
|
|
$fields = $this->removeFieldAtIndex($this->getCount() - 1); |
33
|
|
|
return $fields[0]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
throw new \OutOfRangeException("Can't call popField when the field count is 0"); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function insertFields($fields, $index) |
40
|
|
|
{ |
41
|
|
|
// If a singular field config, put into an array of fields |
42
|
|
|
if ($this->getFieldName($fields)) { |
43
|
|
|
$fields = [$fields]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
foreach($fields as $i => $field) { |
47
|
|
|
if ($this->validateField($field)) { |
48
|
|
|
array_splice($this->fields, $index + $i, 0, [$field]); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function removeFieldAtIndex($index) |
54
|
|
|
{ |
55
|
|
|
return array_splice($this->fields, $index, 1); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function removeField($name) |
59
|
|
|
{ |
60
|
|
|
$index = $this->getFieldIndex($name); |
61
|
|
|
$this->removeFieldAtIndex($index); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function replaceField($name, $field) |
65
|
|
|
{ |
66
|
|
|
$index = $this->getFieldIndex($name); |
67
|
|
|
$this->removeFieldAtIndex($index); |
68
|
|
|
$this->insertFields($field, $index); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function fieldNameExists($name) |
72
|
|
|
{ |
73
|
|
|
try { |
74
|
|
|
$this->getFieldIndex($name); |
75
|
|
|
} catch (FieldNotFoundException $e) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function getField($name) |
83
|
|
|
{ |
84
|
|
|
return $this->fields[$this->getFieldIndex($name)]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
public function getFieldName($field) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
if ($field instanceof NamedBuilder) { |
90
|
|
|
return $field->getName(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (is_array($field) && array_key_exists('name', $field)) { |
94
|
|
|
return $field['name']; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function modifyField($name, $modifications) |
101
|
|
|
{ |
102
|
|
|
$field = $this->getField($name); |
103
|
|
|
$field = array_merge($field, $modifications); |
104
|
|
|
$this->replaceField($name, $field); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function validateField($field) |
108
|
|
|
{ |
109
|
|
|
return $this->validateFieldName($field); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function validateFieldName($field) |
113
|
|
|
{ |
114
|
|
|
$fieldName = $this->getFieldName($field); |
115
|
|
|
if ($this->fieldNameExists($fieldName)) { |
116
|
|
|
throw new FieldNameCollisionException("Field Name: `{$fieldName}` already exists."); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return the index in the $this->fields array looked up by the field's name |
124
|
|
|
* @param string $name Field Name |
125
|
|
|
* |
126
|
|
|
* @throws FieldNotFoundException if the field name doesn't exist |
127
|
|
|
* |
128
|
|
|
* @return integer Field Index |
129
|
|
|
*/ |
130
|
|
|
public function getFieldIndex($name) |
131
|
|
|
{ |
132
|
|
|
foreach($this->getFields() as $index => $field) { |
133
|
|
|
if ($this->getFieldName($field) === $name) { |
134
|
|
|
return $index; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
throw new FieldNotFoundException("Field `{$name}` not found."); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
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.