xref: /plugin/pureldap/vendor/freedsx/ldap/src/FreeDSx/Ldap/Entry/Changes.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_search;
19*dad993c5SAndreas Gohruse function count;
20*dad993c5SAndreas Gohr
210b3fd2d3SAndreas Gohr/**
220b3fd2d3SAndreas Gohr * Represents a set of change objects.
230b3fd2d3SAndreas Gohr *
240b3fd2d3SAndreas Gohr * @author Chad Sikorra <Chad.Sikorra@gmail.com>
250b3fd2d3SAndreas Gohr */
26*dad993c5SAndreas Gohrclass Changes implements Countable, IteratorAggregate
270b3fd2d3SAndreas Gohr{
280b3fd2d3SAndreas Gohr    /**
290b3fd2d3SAndreas Gohr     * @var Change[]
30*dad993c5SAndreas Gohr     * @psalm-var list<Change>
310b3fd2d3SAndreas Gohr     */
320b3fd2d3SAndreas Gohr    protected $changes = [];
330b3fd2d3SAndreas Gohr
340b3fd2d3SAndreas Gohr    /**
350b3fd2d3SAndreas Gohr     * @param Change ...$changes
360b3fd2d3SAndreas Gohr     */
370b3fd2d3SAndreas Gohr    public function __construct(Change ...$changes)
380b3fd2d3SAndreas Gohr    {
390b3fd2d3SAndreas Gohr        $this->changes = $changes;
400b3fd2d3SAndreas Gohr    }
410b3fd2d3SAndreas Gohr
420b3fd2d3SAndreas Gohr    /**
430b3fd2d3SAndreas Gohr     * Add a change to the list.
440b3fd2d3SAndreas Gohr     *
450b3fd2d3SAndreas Gohr     * @param Change ...$changes
460b3fd2d3SAndreas Gohr     * @return $this
470b3fd2d3SAndreas Gohr     */
480b3fd2d3SAndreas Gohr    public function add(Change ...$changes)
490b3fd2d3SAndreas Gohr    {
500b3fd2d3SAndreas Gohr        foreach ($changes as $change) {
510b3fd2d3SAndreas Gohr            $this->changes[] = $change;
520b3fd2d3SAndreas Gohr        }
530b3fd2d3SAndreas Gohr
540b3fd2d3SAndreas Gohr        return $this;
550b3fd2d3SAndreas Gohr    }
560b3fd2d3SAndreas Gohr
570b3fd2d3SAndreas Gohr    /**
580b3fd2d3SAndreas Gohr     * Check if the change is in the change list.
590b3fd2d3SAndreas Gohr     *
600b3fd2d3SAndreas Gohr     * @param Change $change
610b3fd2d3SAndreas Gohr     * @return bool
620b3fd2d3SAndreas Gohr     */
630b3fd2d3SAndreas Gohr    public function has(Change $change)
640b3fd2d3SAndreas Gohr    {
65*dad993c5SAndreas Gohr        return array_search($change, $this->changes, true) !== false;
660b3fd2d3SAndreas Gohr    }
670b3fd2d3SAndreas Gohr
680b3fd2d3SAndreas Gohr    /**
690b3fd2d3SAndreas Gohr     * Remove a change from the list.
700b3fd2d3SAndreas Gohr     *
710b3fd2d3SAndreas Gohr     * @param Change ...$changes
720b3fd2d3SAndreas Gohr     * @return $this
730b3fd2d3SAndreas Gohr     */
740b3fd2d3SAndreas Gohr    public function remove(Change ...$changes)
750b3fd2d3SAndreas Gohr    {
760b3fd2d3SAndreas Gohr        foreach ($changes as $change) {
77*dad993c5SAndreas Gohr            if (($index = array_search($change, $this->changes, true)) !== false) {
780b3fd2d3SAndreas Gohr                unset($this->changes[$index]);
790b3fd2d3SAndreas Gohr            }
800b3fd2d3SAndreas Gohr        }
810b3fd2d3SAndreas Gohr
820b3fd2d3SAndreas Gohr        return $this;
830b3fd2d3SAndreas Gohr    }
840b3fd2d3SAndreas Gohr
850b3fd2d3SAndreas Gohr    /**
860b3fd2d3SAndreas Gohr     * Removes all changes from the list.
870b3fd2d3SAndreas Gohr     *
880b3fd2d3SAndreas Gohr     * @return $this
890b3fd2d3SAndreas Gohr     */
900b3fd2d3SAndreas Gohr    public function reset()
910b3fd2d3SAndreas Gohr    {
920b3fd2d3SAndreas Gohr        $this->changes = [];
930b3fd2d3SAndreas Gohr
940b3fd2d3SAndreas Gohr        return $this;
950b3fd2d3SAndreas Gohr    }
960b3fd2d3SAndreas Gohr
970b3fd2d3SAndreas Gohr    /**
980b3fd2d3SAndreas Gohr     * Set the change list to just these changes.
990b3fd2d3SAndreas Gohr     *
1000b3fd2d3SAndreas Gohr     * @param Change ...$changes
1010b3fd2d3SAndreas Gohr     * @return $this
1020b3fd2d3SAndreas Gohr     */
1030b3fd2d3SAndreas Gohr    public function set(Change ...$changes)
1040b3fd2d3SAndreas Gohr    {
1050b3fd2d3SAndreas Gohr        $this->changes = $changes;
1060b3fd2d3SAndreas Gohr
1070b3fd2d3SAndreas Gohr        return $this;
1080b3fd2d3SAndreas Gohr    }
1090b3fd2d3SAndreas Gohr
1100b3fd2d3SAndreas Gohr    /**
1110b3fd2d3SAndreas Gohr     * @return Change[]
1120b3fd2d3SAndreas Gohr     */
1130b3fd2d3SAndreas Gohr    public function toArray(): array
1140b3fd2d3SAndreas Gohr    {
1150b3fd2d3SAndreas Gohr        return $this->changes;
1160b3fd2d3SAndreas Gohr    }
1170b3fd2d3SAndreas Gohr
1180b3fd2d3SAndreas Gohr    /**
119*dad993c5SAndreas Gohr     * @inheritDoc
120*dad993c5SAndreas Gohr     * @psalm-return 0|positive-int
1210b3fd2d3SAndreas Gohr     */
122*dad993c5SAndreas Gohr    public function count(): int
1230b3fd2d3SAndreas Gohr    {
124*dad993c5SAndreas Gohr        return count($this->changes);
1250b3fd2d3SAndreas Gohr    }
1260b3fd2d3SAndreas Gohr
1270b3fd2d3SAndreas Gohr    /**
128*dad993c5SAndreas Gohr     * @inheritDoc
129*dad993c5SAndreas Gohr     * @psalm-return \ArrayIterator<int, Change>
1300b3fd2d3SAndreas Gohr     */
131*dad993c5SAndreas Gohr    public function getIterator(): Traversable
1320b3fd2d3SAndreas Gohr    {
133*dad993c5SAndreas Gohr        return new ArrayIterator($this->changes);
1340b3fd2d3SAndreas Gohr    }
1350b3fd2d3SAndreas Gohr}
136