blob: 6c6a600766935b5bfd8558cf988942f548801deb [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace Adldap\Models\Attributes;
4
5use Adldap\Utilities;
6use InvalidArgumentException;
7
8class Guid
9{
10 /**
11 * The string GUID value.
12 *
13 * @var string
14 */
15 protected $value;
16
17 /**
18 * The guid structure in order by section to parse using substr().
19 *
20 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
21 *
22 * @link https://github.com/ldaptools/ldaptools
23 *
24 * @var array
25 */
26 protected $guidSections = [
27 [[-26, 2], [-28, 2], [-30, 2], [-32, 2]],
28 [[-22, 2], [-24, 2]],
29 [[-18, 2], [-20, 2]],
30 [[-16, 4]],
31 [[-12, 12]],
32 ];
33
34 /**
35 * The hexadecimal octet order based on string position.
36 *
37 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
38 *
39 * @link https://github.com/ldaptools/ldaptools
40 *
41 * @var array
42 */
43 protected $octetSections = [
44 [6, 4, 2, 0],
45 [10, 8],
46 [14, 12],
47 [16, 18, 20, 22, 24, 26, 28, 30],
48 ];
49
50 /**
51 * Determines if the specified GUID is valid.
52 *
53 * @param string $guid
54 *
55 * @return bool
56 */
57 public static function isValid($guid)
58 {
59 return Utilities::isValidGuid($guid);
60 }
61
62 /**
63 * Constructor.
64 *
65 * @param mixed $value
66 *
67 * @throws InvalidArgumentException
68 */
69 public function __construct($value)
70 {
71 if (static::isValid($value)) {
72 $this->value = $value;
73 } elseif ($value = $this->binaryGuidToString($value)) {
74 $this->value = $value;
75 } else {
76 throw new InvalidArgumentException('Invalid Binary / String GUID.');
77 }
78 }
79
80 /**
81 * Returns the string value of the GUID.
82 *
83 * @return string
84 */
85 public function __toString()
86 {
87 return $this->getValue();
88 }
89
90 /**
91 * Returns the string value of the SID.
92 *
93 * @return string
94 */
95 public function getValue()
96 {
97 return $this->value;
98 }
99
100 /**
101 * Get the binary representation of the GUID string.
102 *
103 * @return string
104 */
105 public function getBinary()
106 {
107 $data = '';
108
109 $guid = str_replace('-', '', $this->value);
110
111 foreach ($this->octetSections as $section) {
112 $data .= $this->parseSection($guid, $section, true);
113 }
114
115 return hex2bin($data);
116 }
117
118 /**
119 * Returns the string variant of a binary GUID.
120 *
121 * @param string $binary
122 *
123 * @return string|null
124 */
125 protected function binaryGuidToString($binary)
126 {
127 return Utilities::binaryGuidToString($binary);
128 }
129
130 /**
131 * Return the specified section of the hexadecimal string.
132 *
133 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
134 *
135 * @link https://github.com/ldaptools/ldaptools
136 *
137 * @param string $hex The full hex string.
138 * @param array $sections An array of start and length (unless octet is true, then length is always 2).
139 * @param bool $octet Whether this is for octet string form.
140 *
141 * @return string The concatenated sections in upper-case.
142 */
143 protected function parseSection($hex, array $sections, $octet = false)
144 {
145 $parsedString = '';
146
147 foreach ($sections as $section) {
148 $start = $octet ? $section : $section[0];
149
150 $length = $octet ? 2 : $section[1];
151
152 $parsedString .= substr($hex, $start, $length);
153 }
154
155 return $parsedString;
156 }
157}