blob: 4ec46ea71cf890511db8bfcc671721b7e2554687 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace LdapRecord\Models\Attributes;
4
5use InvalidArgumentException;
6use LdapRecord\Utilities;
7
8class Sid
9{
10 /**
11 * The string SID value.
12 *
13 * @var string
14 */
15 protected $value;
16
17 /**
18 * Determines if the specified SID is valid.
19 *
20 * @param string $sid
21 *
22 * @return bool
23 */
24 public static function isValid($sid)
25 {
26 return Utilities::isValidSid($sid);
27 }
28
29 /**
30 * Constructor.
31 *
32 * @param mixed $value
33 *
34 * @throws InvalidArgumentException
35 */
36 public function __construct($value)
37 {
38 if (static::isValid($value)) {
39 $this->value = $value;
40 } elseif ($value = $this->binarySidToString($value)) {
41 $this->value = $value;
42 } else {
43 throw new InvalidArgumentException('Invalid Binary / String SID.');
44 }
45 }
46
47 /**
48 * Returns the string value of the SID.
49 *
50 * @return string
51 */
52 public function __toString()
53 {
54 return $this->getValue();
55 }
56
57 /**
58 * Returns the string value of the SID.
59 *
60 * @return string
61 */
62 public function getValue()
63 {
64 return $this->value;
65 }
66
67 /**
68 * Returns the binary variant of the SID.
69 *
70 * @return string
71 */
72 public function getBinary()
73 {
74 $sid = explode('-', ltrim($this->value, 'S-'));
75
76 $level = (int) array_shift($sid);
77
78 $authority = (int) array_shift($sid);
79
80 $subAuthorities = array_map('intval', $sid);
81
82 $params = array_merge(
83 ['C2xxNV*', $level, count($subAuthorities), $authority],
84 $subAuthorities
85 );
86
87 return call_user_func_array('pack', $params);
88 }
89
90 /**
91 * Returns the string variant of a binary SID.
92 *
93 * @param string $binary
94 *
95 * @return string|null
96 */
97 protected function binarySidToString($binary)
98 {
99 return Utilities::binarySidToString($binary);
100 }
101}