Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace LdapRecord\Models\Attributes; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | use LdapRecord\Utilities; |
| 7 | |
| 8 | class 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 | * @see 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 | * @see 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 | return hex2bin($this->getHex()); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get the hexadecimal representation of the GUID string. |
| 112 | * |
| 113 | * @return string |
| 114 | */ |
| 115 | public function getHex() |
| 116 | { |
| 117 | $data = ''; |
| 118 | |
| 119 | $guid = str_replace('-', '', $this->value); |
| 120 | |
| 121 | foreach ($this->octetSections as $section) { |
| 122 | $data .= $this->parseSection($guid, $section, $octet = true); |
| 123 | } |
| 124 | |
| 125 | return $data; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Returns the string variant of a binary GUID. |
| 130 | * |
| 131 | * @param string $binary |
| 132 | * |
| 133 | * @return string|null |
| 134 | */ |
| 135 | protected function binaryGuidToString($binary) |
| 136 | { |
| 137 | return Utilities::binaryGuidToString($binary); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Return the specified section of the hexadecimal string. |
| 142 | * |
| 143 | * @author Chad Sikorra <Chad.Sikorra@gmail.com> |
| 144 | * |
| 145 | * @see https://github.com/ldaptools/ldaptools |
| 146 | * |
| 147 | * @param string $hex The full hex string. |
| 148 | * @param array $sections An array of start and length (unless octet is true, then length is always 2). |
| 149 | * @param bool $octet Whether this is for octet string form. |
| 150 | * |
| 151 | * @return string The concatenated sections in upper-case. |
| 152 | */ |
| 153 | protected function parseSection($hex, array $sections, $octet = false) |
| 154 | { |
| 155 | $parsedString = ''; |
| 156 | |
| 157 | foreach ($sections as $section) { |
| 158 | $start = $octet ? $section : $section[0]; |
| 159 | |
| 160 | $length = $octet ? 2 : $section[1]; |
| 161 | |
| 162 | $parsedString .= substr($hex, $start, $length); |
| 163 | } |
| 164 | |
| 165 | return $parsedString; |
| 166 | } |
| 167 | } |