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\Control\Ad;
13
14use FreeDSx\Asn1\Asn1;
15use FreeDSx\Asn1\Exception\EncoderException;
16use FreeDSx\Asn1\Exception\PartialPduException;
17use FreeDSx\Asn1\Type\AbstractType;
18use FreeDSx\Asn1\Type\IntegerType;
19use FreeDSx\Asn1\Type\SequenceType;
20use FreeDSx\Ldap\Control\Control;
21use FreeDSx\Ldap\Exception\ProtocolException;
22
23/**
24 * Implements the AD PolicyHints control.
25 *
26 *  PolicyHintsRequestValue ::= SEQUENCE {
27 *      Flags    INTEGER
28 *  }
29 *
30 * @see https://msdn.microsoft.com/en-us/library/hh128228.aspx
31 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
32 */
33class PolicyHintsControl extends Control
34{
35    /**
36     * @var bool
37     */
38    protected $isEnabled;
39
40    /**
41     * @param bool $isEnabled
42     */
43    public function __construct(bool $isEnabled = true)
44    {
45        $this->isEnabled = $isEnabled;
46        parent::__construct(self::OID_POLICY_HINTS, true);
47    }
48
49    /**
50     * @param bool $isEnabled
51     * @return $this
52     */
53    public function setIsEnabled(bool $isEnabled)
54    {
55        $this->isEnabled = $isEnabled;
56
57        return $this;
58    }
59
60    /**
61     * @return bool
62     */
63    public function getIsEnabled(): bool
64    {
65        return $this->isEnabled;
66    }
67
68    /**
69     * {@inheritdoc}
70     */
71    public function toAsn1(): AbstractType
72    {
73        $this->controlValue = Asn1::sequence(Asn1::integer($this->isEnabled ? 1 : 0));
74
75        return parent::toAsn1();
76    }
77
78    /**
79     * {@inheritDoc}
80     * @return Control
81     * @throws EncoderException
82     * @throws PartialPduException
83     */
84    public static function fromAsn1(AbstractType $type)
85    {
86        $request = self::decodeEncodedValue($type);
87        if (!$request instanceof SequenceType) {
88            throw new ProtocolException('A PolicyHints control value must be a sequence type.');
89        }
90        $isEnabled = $request->getChild(0);
91        if (!$isEnabled instanceof IntegerType) {
92            throw new ProtocolException('A PolicyHints control value sequence 0 must be an integer type.');
93        }
94        $control = new self($isEnabled->getValue());
95
96        return self::mergeControlData($control, $type);
97    }
98}
99