13
13
14
14
/**
15
15
* @author Charles Sarrazin <charles@sarraz.in>
16
+ * @author Karl Shea <karl@karlshea.com>
16
17
*/
17
18
class Entry
18
19
{
19
20
private $ dn ;
20
21
private $ attributes ;
22
+ private $ lowerMap ;
21
23
22
24
public function __construct (string $ dn , array $ attributes = [])
23
25
{
24
26
$ this ->dn = $ dn ;
25
- $ this ->attributes = $ attributes ;
27
+ $ this ->attributes = [];
28
+ $ this ->lowerMap = [];
29
+
30
+ foreach ($ attributes as $ key => $ attribute ) {
31
+ $ this ->setAttribute ($ key , $ attribute );
32
+ }
26
33
}
27
34
28
35
/**
@@ -38,13 +45,21 @@ public function getDn()
38
45
/**
39
46
* Returns whether an attribute exists.
40
47
*
41
- * @param string $name The name of the attribute
48
+ * @param string $name The name of the attribute
49
+ * @param bool $caseSensitive Whether the check should be case-sensitive
42
50
*
43
51
* @return bool
44
52
*/
45
- public function hasAttribute (string $ name )
53
+ public function hasAttribute (string $ name/* , bool $caseSensitive = true */ )
46
54
{
47
- return isset ($ this ->attributes [$ name ]);
55
+ $ caseSensitive = 2 > \func_num_args () || true === func_get_arg (1 );
56
+ $ attributeKey = $ this ->getAttributeKey ($ name , $ caseSensitive );
57
+
58
+ if (null === $ attributeKey ) {
59
+ return false ;
60
+ }
61
+
62
+ return isset ($ this ->attributes [$ attributeKey ]);
48
63
}
49
64
50
65
/**
@@ -53,13 +68,21 @@ public function hasAttribute(string $name)
53
68
* As LDAP can return multiple values for a single attribute,
54
69
* this value is returned as an array.
55
70
*
56
- * @param string $name The name of the attribute
71
+ * @param string $name The name of the attribute
72
+ * @param bool $caseSensitive Whether the attribute name is case-sensitive
57
73
*
58
74
* @return array|null
59
75
*/
60
- public function getAttribute (string $ name )
76
+ public function getAttribute (string $ name/* , bool $caseSensitive = true */ )
61
77
{
62
- return isset ($ this ->attributes [$ name ]) ? $ this ->attributes [$ name ] : null ;
78
+ $ caseSensitive = 2 > \func_num_args () || true === func_get_arg (1 );
79
+ $ attributeKey = $ this ->getAttributeKey ($ name , $ caseSensitive );
80
+
81
+ if (null === $ attributeKey ) {
82
+ return null ;
83
+ }
84
+
85
+ return $ this ->attributes [$ attributeKey ] ?? null ;
63
86
}
64
87
65
88
/**
@@ -78,6 +101,7 @@ public function getAttributes()
78
101
public function setAttribute (string $ name , array $ value )
79
102
{
80
103
$ this ->attributes [$ name ] = $ value ;
104
+ $ this ->lowerMap [strtolower ($ name )] = $ name ;
81
105
}
82
106
83
107
/**
@@ -86,5 +110,21 @@ public function setAttribute(string $name, array $value)
86
110
public function removeAttribute (string $ name )
87
111
{
88
112
unset($ this ->attributes [$ name ]);
113
+ unset($ this ->lowerMap [strtolower ($ name )]);
114
+ }
115
+
116
+ /**
117
+ * Get the attribute key.
118
+ *
119
+ * @param string $name The attribute name
120
+ * @param bool $caseSensitive Whether the attribute name is case-sensitive
121
+ */
122
+ private function getAttributeKey (string $ name , bool $ caseSensitive = true ): ?string
123
+ {
124
+ if ($ caseSensitive ) {
125
+ return $ name ;
126
+ }
127
+
128
+ return $ this ->lowerMap [strtolower ($ name )] ?? null ;
89
129
}
90
130
}
0 commit comments