xref: /plugin/pureldap/vendor/freedsx/ldap/src/FreeDSx/Ldap/Entry/Entries.php (revision dad993c57a70866aa1db59c43f043769c2eb7ed0)
10b3fd2d3SAndreas Gohr<?php
2*dad993c5SAndreas Gohr
30b3fd2d3SAndreas Gohr/**
40b3fd2d3SAndreas Gohr * This file is part of the FreeDSx LDAP package.
50b3fd2d3SAndreas Gohr *
60b3fd2d3SAndreas Gohr * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
70b3fd2d3SAndreas Gohr *
80b3fd2d3SAndreas Gohr * For the full copyright and license information, please view the LICENSE
90b3fd2d3SAndreas Gohr * file that was distributed with this source code.
100b3fd2d3SAndreas Gohr */
110b3fd2d3SAndreas Gohr
120b3fd2d3SAndreas Gohrnamespace FreeDSx\Ldap\Entry;
130b3fd2d3SAndreas Gohr
14*dad993c5SAndreas Gohruse ArrayIterator;
15*dad993c5SAndreas Gohruse Countable;
16*dad993c5SAndreas Gohruse IteratorAggregate;
17*dad993c5SAndreas Gohruse Traversable;
18*dad993c5SAndreas Gohruse function array_merge;
19*dad993c5SAndreas Gohruse function array_search;
20*dad993c5SAndreas Gohruse function count;
21*dad993c5SAndreas Gohruse function end;
22*dad993c5SAndreas Gohruse function reset;
230b3fd2d3SAndreas Gohr
240b3fd2d3SAndreas Gohr/**
250b3fd2d3SAndreas Gohr * Represents a collection of entry objects.
260b3fd2d3SAndreas Gohr *
270b3fd2d3SAndreas Gohr * @author Chad Sikorra <Chad.Sikorra@gmail.com>
280b3fd2d3SAndreas Gohr */
29*dad993c5SAndreas Gohrclass Entries implements Countable, IteratorAggregate
300b3fd2d3SAndreas Gohr{
310b3fd2d3SAndreas Gohr    /**
320b3fd2d3SAndreas Gohr     * @var Entry[]
330b3fd2d3SAndreas Gohr     */
340b3fd2d3SAndreas Gohr    protected $entries = [];
350b3fd2d3SAndreas Gohr
360b3fd2d3SAndreas Gohr    /**
370b3fd2d3SAndreas Gohr     * @param Entry ...$entries
380b3fd2d3SAndreas Gohr     */
390b3fd2d3SAndreas Gohr    public function __construct(Entry ...$entries)
400b3fd2d3SAndreas Gohr    {
410b3fd2d3SAndreas Gohr        $this->entries = $entries;
420b3fd2d3SAndreas Gohr    }
430b3fd2d3SAndreas Gohr
440b3fd2d3SAndreas Gohr    /**
450b3fd2d3SAndreas Gohr     * @param Entry ...$entries
460b3fd2d3SAndreas Gohr     * @return $this
470b3fd2d3SAndreas Gohr     */
480b3fd2d3SAndreas Gohr    public function add(Entry ...$entries)
490b3fd2d3SAndreas Gohr    {
50*dad993c5SAndreas Gohr        $this->entries = array_merge($this->entries, $entries);
510b3fd2d3SAndreas Gohr
520b3fd2d3SAndreas Gohr        return $this;
530b3fd2d3SAndreas Gohr    }
540b3fd2d3SAndreas Gohr
550b3fd2d3SAndreas Gohr    /**
560b3fd2d3SAndreas Gohr     * @param Entry ...$entries
570b3fd2d3SAndreas Gohr     * @return $this
580b3fd2d3SAndreas Gohr     */
590b3fd2d3SAndreas Gohr    public function remove(Entry ...$entries)
600b3fd2d3SAndreas Gohr    {
610b3fd2d3SAndreas Gohr        foreach ($entries as $entry) {
62*dad993c5SAndreas Gohr            if (($index = array_search($entry, $this->entries, true)) !== false) {
630b3fd2d3SAndreas Gohr                unset($this->entries[$index]);
640b3fd2d3SAndreas Gohr            }
650b3fd2d3SAndreas Gohr        }
660b3fd2d3SAndreas Gohr
670b3fd2d3SAndreas Gohr        return $this;
680b3fd2d3SAndreas Gohr    }
690b3fd2d3SAndreas Gohr
700b3fd2d3SAndreas Gohr    /**
710b3fd2d3SAndreas Gohr     * Check whether or not an entry (either an Entry object or string DN) exists within the entries.
720b3fd2d3SAndreas Gohr     *
730b3fd2d3SAndreas Gohr     * @param Entry|Dn|string $entry
740b3fd2d3SAndreas Gohr     * @return bool
750b3fd2d3SAndreas Gohr     */
760b3fd2d3SAndreas Gohr    public function has($entry): bool
770b3fd2d3SAndreas Gohr    {
780b3fd2d3SAndreas Gohr        if ($entry instanceof Entry) {
79*dad993c5SAndreas Gohr            return (array_search($entry, $this->entries, true) !== false);
800b3fd2d3SAndreas Gohr        }
810b3fd2d3SAndreas Gohr
820b3fd2d3SAndreas Gohr        foreach ($this->entries as $entryObj) {
830b3fd2d3SAndreas Gohr            if ((string) $entry === $entryObj->getDn()->toString()) {
840b3fd2d3SAndreas Gohr                return true;
850b3fd2d3SAndreas Gohr            }
860b3fd2d3SAndreas Gohr        }
870b3fd2d3SAndreas Gohr
880b3fd2d3SAndreas Gohr        return false;
890b3fd2d3SAndreas Gohr    }
900b3fd2d3SAndreas Gohr
910b3fd2d3SAndreas Gohr    /**
920b3fd2d3SAndreas Gohr     * Get an entry from the collection by its DN.
930b3fd2d3SAndreas Gohr     *
940b3fd2d3SAndreas Gohr     * @param string $dn
950b3fd2d3SAndreas Gohr     * @return Entry|null
960b3fd2d3SAndreas Gohr     */
970b3fd2d3SAndreas Gohr    public function get(string $dn): ?Entry
980b3fd2d3SAndreas Gohr    {
990b3fd2d3SAndreas Gohr        foreach ($this->entries as $entry) {
1000b3fd2d3SAndreas Gohr            if ($entry->getDn()->toString() === $dn) {
1010b3fd2d3SAndreas Gohr                return $entry;
1020b3fd2d3SAndreas Gohr            }
1030b3fd2d3SAndreas Gohr        }
1040b3fd2d3SAndreas Gohr
1050b3fd2d3SAndreas Gohr        return null;
1060b3fd2d3SAndreas Gohr    }
1070b3fd2d3SAndreas Gohr
1080b3fd2d3SAndreas Gohr    /**
1090b3fd2d3SAndreas Gohr     * Get the first entry object, if one exists.
1100b3fd2d3SAndreas Gohr     *
1110b3fd2d3SAndreas Gohr     * @return Entry|null
1120b3fd2d3SAndreas Gohr     */
1130b3fd2d3SAndreas Gohr    public function first(): ?Entry
1140b3fd2d3SAndreas Gohr    {
115*dad993c5SAndreas Gohr        $entry = reset($this->entries);
1160b3fd2d3SAndreas Gohr
1170b3fd2d3SAndreas Gohr        return $entry === false ? null : $entry;
1180b3fd2d3SAndreas Gohr    }
1190b3fd2d3SAndreas Gohr
1200b3fd2d3SAndreas Gohr    /**
1210b3fd2d3SAndreas Gohr     * Get the last entry object, if one exists.
1220b3fd2d3SAndreas Gohr     *
1230b3fd2d3SAndreas Gohr     * @return Entry|null
1240b3fd2d3SAndreas Gohr     */
1250b3fd2d3SAndreas Gohr    public function last(): ?Entry
1260b3fd2d3SAndreas Gohr    {
127*dad993c5SAndreas Gohr        $entry = end($this->entries);
128*dad993c5SAndreas Gohr        reset($this->entries);
1290b3fd2d3SAndreas Gohr
1300b3fd2d3SAndreas Gohr        return $entry === false ? null : $entry;
1310b3fd2d3SAndreas Gohr    }
1320b3fd2d3SAndreas Gohr
1330b3fd2d3SAndreas Gohr    /**
1340b3fd2d3SAndreas Gohr     * @return Entry[]
1350b3fd2d3SAndreas Gohr     */
1360b3fd2d3SAndreas Gohr    public function toArray(): array
1370b3fd2d3SAndreas Gohr    {
1380b3fd2d3SAndreas Gohr        return $this->entries;
1390b3fd2d3SAndreas Gohr    }
1400b3fd2d3SAndreas Gohr
1410b3fd2d3SAndreas Gohr    /**
142*dad993c5SAndreas Gohr     * @inheritDoc
1430b3fd2d3SAndreas Gohr     */
144*dad993c5SAndreas Gohr    public function getIterator(): Traversable
1450b3fd2d3SAndreas Gohr    {
146*dad993c5SAndreas Gohr        return new ArrayIterator($this->entries);
1470b3fd2d3SAndreas Gohr    }
1480b3fd2d3SAndreas Gohr
1490b3fd2d3SAndreas Gohr    /**
150*dad993c5SAndreas Gohr     * @inheritDoc
1510b3fd2d3SAndreas Gohr     */
152*dad993c5SAndreas Gohr    public function count(): int
1530b3fd2d3SAndreas Gohr    {
154*dad993c5SAndreas Gohr        return count($this->entries);
1550b3fd2d3SAndreas Gohr    }
1560b3fd2d3SAndreas Gohr}
157