-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
DistinguishedNameBuilder.php
251 lines (221 loc) · 5.58 KB
/
DistinguishedNameBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
namespace LdapRecord\Models\Attributes;
use LdapRecord\EscapesValues;
use LdapRecord\Support\Arr;
class DistinguishedNameBuilder
{
use EscapesValues;
/**
* The components of the DN.
*
* @var array
*/
protected $components = [];
/**
* Whether to output the DN in reverse.
*
* @var bool
*/
protected $reverse = false;
/**
* Constructor.
*
* @param string|null $value
*/
public function __construct($dn = null)
{
$this->components = array_map(function ($rdn) {
return DistinguishedName::explodeRdn($rdn);
}, DistinguishedName::make($dn)->components());
}
/**
* Forward missing method calls onto the Distinguished Name object.
*
* @param string $method
* @param array $args
*
* @return mixed
*/
public function __call($method, $args)
{
return $this->get()->{$method}(...$args);
}
/**
* Get the distinguished name value.
*
* @return string
*/
public function __toString()
{
return (string) $this->get();
}
/**
* Prepend an RDN onto the DN.
*
* @param string|array $attribute
* @param string|null $value
*
* @return $this
*/
public function prepend($attribute, $value = null)
{
array_unshift(
$this->components,
...$this->componentize($attribute, $value)
);
return $this;
}
/**
* Append an RDN onto the DN.
*
* @param string|array $attribute
* @param string|null $value
*
* @return $this
*/
public function append($attribute, $value = null)
{
array_push(
$this->components,
...$this->componentize($attribute, $value)
);
return $this;
}
/**
* Componentize the attribute and value.
*
* @param string|array $attribute
* @param string|null $value
*
* @return array
*/
protected function componentize($attribute, $value = null)
{
// Here we will make the assumption that an array of
// RDN's have been given if the value is null, and
// attempt to break them into their components.
if (is_null($value)) {
$attributes = Arr::wrap($attribute);
$components = array_map([$this, 'makeComponentizedArray'], $attributes);
} else {
$components = [[$attribute, $value]];
}
return array_map(function ($component) {
[$attribute, $value] = $component;
return $this->makeAppendableComponent($attribute, $value);
}, $components);
}
/**
* Make a componentized array by exploding the value if it's a string.
*
* @param string $value
*
* @return array
*/
protected function makeComponentizedArray($value)
{
return is_array($value) ? $value : DistinguishedName::explodeRdn($value);
}
/**
* Make an appendable component array from the attribute and value.
*
* @param string|array $attribute
* @param string|null $value
*
* @return array
*/
protected function makeAppendableComponent($attribute, $value = null)
{
return [trim($attribute), $this->escape(trim($value))->dn()];
}
/**
* Pop an RDN off of the end of the DN.
*
* @param int $amount
* @param array $removed
*
* @return $this
*/
public function pop($amount = 1, &$removed = [])
{
$removed = array_map(function ($component) {
return DistinguishedName::makeRdn($component);
}, array_splice($this->components, -$amount, $amount));
return $this;
}
/**
* Shift an RDN off of the beginning of the DN.
*
* @param int $amount
* @param array $removed
*
* @return $this
*/
public function shift($amount = 1, &$removed = [])
{
$removed = array_map(function ($component) {
return DistinguishedName::makeRdn($component);
}, array_splice($this->components, 0, $amount));
return $this;
}
/**
* Whether to output the DN in reverse.
*
* @return $this
*/
public function reverse()
{
$this->reverse = true;
return $this;
}
/**
* Get the components of the DN.
*
* @param null|string $type
*
* @return array
*/
public function components($type = null)
{
return is_null($type)
? $this->components
: $this->componentsOfType($type);
}
/**
* Get the components of a particular type.
*
* @param string $type
*
* @return array
*/
protected function componentsOfType($type)
{
$components = array_filter($this->components, function ($component) use ($type) {
return ([$name] = $component) && strtolower($name) === strtolower($type);
});
return array_values($components);
}
/**
* Get the fully qualified DN.
*
* @return DistinguishedName
*/
public function get()
{
return new DistinguishedName($this->build());
}
/**
* Build the distinguished name from the components.
*
* @return string
*/
protected function build()
{
$components = $this->reverse
? array_reverse($this->components)
: $this->components;
return implode(',', array_map(function ($component) {
return DistinguishedName::makeRdn($component);
}, $components));
}
}