3 namespace Adldap\Models\Attributes
;
6 use InvalidArgumentException
;
11 * The string GUID value.
18 * The guid structure in order by section to parse using substr().
20 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
21 * @link https://github.com/ldaptools/ldaptools
25 protected $guidSections = [
26 [[-26, 2], [-28, 2], [-30, 2], [-32, 2]],
34 * The hexadecimal octet order based on string position.
36 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
37 * @link https://github.com/ldaptools/ldaptools
41 protected $octetSections = [
45 [16, 18, 20, 22, 24, 26, 28, 30]
49 * Determines if the specified GUID is valid.
55 public static function isValid($guid)
57 return Utilities
::isValidGuid($guid);
65 * @throws InvalidArgumentException
67 public function __construct($value)
69 if (static::isValid($value)) {
70 $this->value
= $value;
71 } else if ($value = $this->binaryGuidToString($value)) {
72 $this->value
= $value;
74 throw new InvalidArgumentException("Invalid Binary / String GUID.");
79 * Returns the string value of the GUID.
83 public function __toString()
85 return $this->getValue();
89 * Returns the string value of the SID.
93 public function getValue()
99 * Get the binary representation of the GUID string.
103 public function getBinary()
107 $guid = str_replace('-', '', $this->value
);
109 foreach ($this->octetSections
as $section) {
110 $data .= $this->parseSection($guid, $section, true);
113 return hex2bin($data);
117 * Returns the string variant of a binary GUID.
119 * @param string $binary
121 * @return string|null
123 protected function binaryGuidToString($binary)
125 return Utilities
::binaryGuidToString($binary);
129 * Return the specified section of the hexadecimal string.
131 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
132 * @link https://github.com/ldaptools/ldaptools
134 * @param string $hex The full hex string.
135 * @param array $sections An array of start and length (unless octet is true, then length is always 2).
136 * @param bool $octet Whether this is for octet string form.
138 * @return string The concatenated sections in upper-case.
140 protected function parseSection($hex, array $sections, $octet = false)
144 foreach ($sections as $section) {
145 $start = $octet ?
$section : $section[0];
147 $length = $octet ?
2 : $section[1];
149 $parsedString .= substr($hex, $start, $length);
152 return $parsedString;