-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Entry.php
123 lines (104 loc) · 2.88 KB
/
Entry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Ldap;
/**
* @author Charles Sarrazin <charles@sarraz.in>
* @author Karl Shea <karl@karlshea.com>
*/
class Entry
{
private string $dn;
/**
* @var array<string, array>
*/
private array $attributes = [];
/**
* @var array<string, string>
*/
private array $lowerMap = [];
/**
* @param array<string, array> $attributes
*/
public function __construct(string $dn, array $attributes = [])
{
$this->dn = $dn;
foreach ($attributes as $key => $attribute) {
$this->setAttribute($key, $attribute);
}
}
/**
* Returns the entry's DN.
*/
public function getDn(): string
{
return $this->dn;
}
/**
* Returns whether an attribute exists.
*
* @param string $name The name of the attribute
* @param bool $caseSensitive Whether the check should be case-sensitive
*/
public function hasAttribute(string $name, bool $caseSensitive = true): bool
{
$attributeKey = $this->getAttributeKey($name, $caseSensitive);
if (null === $attributeKey) {
return false;
}
return isset($this->attributes[$attributeKey]);
}
/**
* Returns a specific attribute's value.
*
* As LDAP can return multiple values for a single attribute,
* this value is returned as an array.
*
* @param string $name The name of the attribute
* @param bool $caseSensitive Whether the attribute name is case-sensitive
*/
public function getAttribute(string $name, bool $caseSensitive = true): ?array
{
$attributeKey = $this->getAttributeKey($name, $caseSensitive);
if (null === $attributeKey) {
return null;
}
return $this->attributes[$attributeKey] ?? null;
}
/**
* Returns the complete list of attributes.
*/
public function getAttributes(): array
{
return $this->attributes;
}
/**
* Sets a value for the given attribute.
*/
public function setAttribute(string $name, array $value): void
{
$this->attributes[$name] = $value;
$this->lowerMap[strtolower($name)] = $name;
}
/**
* Removes a given attribute.
*/
public function removeAttribute(string $name): void
{
unset($this->attributes[$name]);
unset($this->lowerMap[strtolower($name)]);
}
private function getAttributeKey(string $name, bool $caseSensitive = true): ?string
{
if ($caseSensitive) {
return $name;
}
return $this->lowerMap[strtolower($name)] ?? null;
}
}