composer package updates
[openemr.git] / vendor / adldap2 / adldap2 / src / Models / Attributes / DistinguishedName.php
blobe7b0e0e0d4d7a710b2e33b62e4c37486bd68a624
1 <?php
3 namespace Adldap\Models\Attributes;
5 use Adldap\Utilities;
6 use Adldap\Schemas\ActiveDirectory;
7 use Adldap\Schemas\SchemaInterface;
9 class DistinguishedName
11 /**
12 * The common names in the DN.
14 * @var array
16 public $commonNames = [];
18 /**
19 * The uid's in the DN.
21 * @var array
23 public $userIds = [];
25 /**
26 * The organizational units in the DN.
28 * @var array
30 public $organizationUnits = [];
32 /**
33 * The domain components in the DN.
35 * @var array
37 public $domainComponents = [];
39 /**
40 * The organization names in the DN.
42 * @var array
44 public $organizationNames = [];
46 /**
47 * The current LDAP schema.
49 * @var SchemaInterface
51 protected $schema;
53 /**
54 * The RDN attribute types.
56 * @var array
58 protected $types = [
59 'o',
60 'dc',
61 'ou',
62 'uid',
63 'cn',
66 /**
67 * Constructor.
69 * @param mixed $baseDn
70 * @param SchemaInterface $schema
72 public function __construct($baseDn = null, SchemaInterface $schema = null)
74 $this->setBase($baseDn)
75 ->setSchema($schema);
78 /**
79 * Returns the complete distinguished name.
81 * @return string
83 public function __toString()
85 return $this->get();
88 /**
89 * Returns the complete distinguished name.
91 * @return string
93 public function get()
95 return $this->assemble();
98 /**
99 * Adds a domain component.
101 * @param string $dc
103 * @return DistinguishedName
105 public function addDc($dc)
107 $this->domainComponents[] = $dc;
109 return $this;
113 * Removes a domain component.
115 * @param string $dc
117 * @return DistinguishedName
119 public function removeDc($dc)
121 $this->domainComponents = array_diff($this->domainComponents, [$dc]);
123 return $this;
127 * Adds an organization name.
129 * @param string $o
131 * @return $this
133 public function addO($o)
135 $this->organizationNames[] = $o;
137 return $this;
141 * Removes an organization name.
143 * @param string $o
145 * @return DistinguishedName
147 public function removeO($o)
149 $this->organizationNames = array_diff($this->organizationNames, [$o]);
151 return $this;
155 * Add a user identifier.
157 * @param string $uid
159 * @return DistinguishedName
161 public function addUid($uid)
163 $this->userIds[] = $uid;
165 return $this;
169 * Removes a user identifier.
171 * @param string $uid
173 * @return DistinguishedName
175 public function removeUid($uid)
177 $this->userIds = array_diff($this->userIds, [$uid]);
179 return $this;
183 * Adds a common name.
185 * @param string $cn
187 * @return DistinguishedName
189 public function addCn($cn)
191 $this->commonNames[] = $cn;
193 return $this;
197 * Removes a common name.
199 * @param string $cn
201 * @return DistinguishedName
203 public function removeCn($cn)
205 $this->commonNames = array_diff($this->commonNames, [$cn]);
207 return $this;
211 * Adds an organizational unit.
213 * @param string $ou
215 * @return DistinguishedName
217 public function addOu($ou)
219 $this->organizationUnits[] = $ou;
221 return $this;
225 * Removes an organizational unit.
227 * @param string $ou
229 * @return DistinguishedName
231 public function removeOu($ou)
233 $this->organizationUnits = array_diff($this->organizationUnits, [$ou]);
235 return $this;
239 * Sets the base RDN of the distinguished name.
241 * @param string|DistinguishedName $base
243 * @return DistinguishedName
245 public function setBase($base)
247 // Typecast base to string in case we've been given
248 // an instance of the distinguished name object.
249 $base = (string) $base;
251 // If the base DN isn't null we'll try to explode it.
252 $base = Utilities::explodeDn($base, false) ?: [];
254 // Remove the count key from the exploded distinguished name.
255 unset($base['count']);
257 foreach ($base as $key => $rdn) {
258 // We'll break the RDN into pieces
259 $pieces = explode('=', $rdn) ?: [];
261 // If there's exactly 2 pieces, then we can work with it.
262 if (count($pieces) === 2) {
263 $attribute = ucfirst($pieces[0]);
265 $method = 'add'.$attribute;
267 if (method_exists($this, $method)) {
268 // We see what type of RDN it is and add each accordingly.
269 call_user_func_array([$this, $method], [$pieces[1]]);
274 return $this;
278 * Sets the schema for the distinguished name.
280 * @param SchemaInterface|null $schema
282 * @return DistinguishedName
284 public function setSchema(SchemaInterface $schema = null)
286 $this->schema = $schema ?: new ActiveDirectory();
288 return $this;
292 * Assembles all of the RDNs and returns the result.
294 * @return string
296 public function assemble()
298 return implode(',', array_filter([
299 $this->assembleCns(),
300 $this->assembleUids(),
301 $this->assembleOus(),
302 $this->assembleDcs(),
303 $this->assembleOs(),
304 ]));
308 * Assembles the common names in the distinguished name.
310 * @return string
312 public function assembleCns()
314 return $this->assembleRdns($this->schema->commonName(), $this->commonNames);
318 * Assembles the user ID's in the distinguished name.
320 * @return string
322 public function assembleUids()
324 return $this->assembleRdns($this->schema->userId(), $this->userIds);
328 * Assembles the organizational units in the distinguished Name.
330 * @return string
332 public function assembleOus()
334 return $this->assembleRdns($this->schema->organizationalUnitShort(), $this->organizationUnits);
338 * Assembles the domain components in the distinguished Name.
340 * @return string
342 public function assembleDcs()
344 return $this->assembleRdns($this->schema->domainComponent(), $this->domainComponents);
348 * Assembles the organization names in the distinguished name.
350 * @return string
352 public function assembleOs()
354 return $this->assembleRdns($this->schema->organizationName(), $this->organizationNames);
358 * Assembles an RDN with the specified attribute and value.
360 * @param string $attribute
361 * @param array $values
363 * @return string
365 protected function assembleRdns($attribute, array $values = [])
367 return implode(',', array_map(function ($value) use ($attribute) {
368 return sprintf('%s=%s', $attribute, Utilities::escape($value, '', 2));
369 }, $values));