1<?php
2
3/**
4 * This file is part of the FreeDSx LDAP package.
5 *
6 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace FreeDSx\Ldap\Protocol;
13
14use Countable;
15use FreeDSx\Ldap\LdapUrl;
16use function count;
17use function strtolower;
18
19/**
20 * Keeps track of referrals while they are being chased.
21 *
22 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
23 */
24class ReferralContext implements Countable
25{
26    /**
27     * @var LdapUrl[]
28     */
29    protected $referrals = [];
30
31    /**
32     * @param LdapUrl ...$referrals
33     */
34    public function __construct(LdapUrl ...$referrals)
35    {
36        $this->referrals = $referrals;
37    }
38
39    /**
40     * @return LdapUrl[]
41     */
42    public function getReferrals(): array
43    {
44        return $this->referrals;
45    }
46
47    /**
48     * @param LdapUrl $referral
49     * @return $this
50     */
51    public function addReferral(LdapUrl $referral)
52    {
53        $this->referrals[] = $referral;
54
55        return $this;
56    }
57
58    /**
59     * @param LdapUrl $url
60     * @return bool
61     */
62    public function hasReferral(LdapUrl $url): bool
63    {
64        foreach ($this->referrals as $referral) {
65            if (strtolower($referral->toString()) === strtolower($url->toString())) {
66                return true;
67            }
68        }
69
70        return false;
71    }
72
73    /**
74     * @inheritDoc
75     * @psalm-return 0|positive-int
76     */
77    public function count(): int
78    {
79        return count($this->referrals);
80    }
81}
82