From f5508c36850b7a6edf1d1a5b9c6a7a0c74ee3677 Mon Sep 17 00:00:00 2001 From: Henk Poley Date: Fri, 12 Mar 2021 18:19:59 +0100 Subject: [PATCH] Missing LDAP-style case insensitive search in Entries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Not sure if it should be named like this, but I was missing LDAP-style case insensitive search in Entries. Maybe use mb_strtolower ? Or an LDAP spec strtolower() implementation.. ? I'm not even sure if I would merge this as is 😅 --- src/FreeDSx/Ldap/Entry/Entries.php | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/FreeDSx/Ldap/Entry/Entries.php b/src/FreeDSx/Ldap/Entry/Entries.php index 5721afd7..b3ef8810 100644 --- a/src/FreeDSx/Ldap/Entry/Entries.php +++ b/src/FreeDSx/Ldap/Entry/Entries.php @@ -79,6 +79,28 @@ public function has($entry): bool return false; } + + /** + * Check whether or not an entry (either an Entry object or string DN) exists within the entries. + * + * @param Entry|Dn|string $entry + * @return bool + */ + public function hasCaseInsensitive($entry): bool + { + if ($entry instanceof Entry) { + return (\array_search($entry, $this->entries, true) !== false); + } + + foreach ($this->entries as $entryObj) { + if ((string) strtolower($entry) === strtolower($entryObj->getDn()->toString())) { + return true; + } + } + + return false; + } + /** * Get an entry from the collection by its DN. * @@ -95,6 +117,23 @@ public function get(string $dn): ?Entry return null; } + + /** + * Get an entry from the collection by its DN. + * + * @param string $dn + * @return Entry|null + */ + public function getCaseInsensitive(string $dn): ?Entry + { + foreach ($this->entries as $entry) { + if (strtolower($entry->getDn()->toString()) === strtolower($dn)) { + return $entry; + } + } + + return null; + } /** * Get the first entry object, if one exists.