composer package updates
[openemr.git] / vendor / adldap2 / adldap2 / src / Models / Attributes / Guid.php
blob0571b0daa14216bdb9e2e83d1b0657620bf2fe4b
1 <?php
3 namespace Adldap\Models\Attributes;
5 use Adldap\Utilities;
6 use InvalidArgumentException;
8 class Guid
10 /**
11 * The string GUID value.
13 * @var string
15 protected $value;
17 /**
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
23 * @var array
25 protected $guidSections = [
26 [[-26, 2], [-28, 2], [-30, 2], [-32, 2]],
27 [[-22, 2], [-24, 2]],
28 [[-18, 2], [-20, 2]],
29 [[-16, 4]],
30 [[-12, 12]],
33 /**
34 * The hexadecimal octet order based on string position.
36 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
37 * @link https://github.com/ldaptools/ldaptools
39 * @var array
41 protected $octetSections = [
42 [6, 4, 2 ,0],
43 [10, 8],
44 [14, 12],
45 [16, 18, 20, 22, 24, 26, 28, 30]
48 /**
49 * Determines if the specified GUID is valid.
51 * @param string $guid
53 * @return bool
55 public static function isValid($guid)
57 return Utilities::isValidGuid($guid);
60 /**
61 * Constructor.
63 * @param mixed $value
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;
73 } else {
74 throw new InvalidArgumentException("Invalid Binary / String GUID.");
78 /**
79 * Returns the string value of the GUID.
81 * @return string
83 public function __toString()
85 return $this->getValue();
88 /**
89 * Returns the string value of the SID.
91 * @return string
93 public function getValue()
95 return $this->value;
98 /**
99 * Get the binary representation of the GUID string.
101 * @return string
103 public function getBinary()
105 $data = '';
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)
142 $parsedString = '';
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;