1<?php
2/**
3 * This file is part of the FreeDSx LDAP package.
4 *
5 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace FreeDSx\Ldap\Control\Ad;
12
13use FreeDSx\Asn1\Asn1;
14use FreeDSx\Asn1\Type\AbstractType;
15use FreeDSx\Asn1\Type\IntegerType;
16use FreeDSx\Asn1\Type\SequenceType;
17use FreeDSx\Ldap\Control\Control;
18use FreeDSx\Ldap\Exception\ProtocolException;
19
20/**
21 * Implements the AD PolicyHints control.
22 *
23 *  PolicyHintsRequestValue ::= SEQUENCE {
24 *      Flags    INTEGER
25 *  }
26 *
27 * @see https://msdn.microsoft.com/en-us/library/hh128228.aspx
28 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
29 */
30class PolicyHintsControl extends Control
31{
32    /**
33     * @var bool
34     */
35    protected $isEnabled;
36
37    /**
38     * @param bool $isEnabled
39     */
40    public function __construct(bool $isEnabled = true)
41    {
42        $this->isEnabled = $isEnabled;
43        parent::__construct(self::OID_POLICY_HINTS, true);
44    }
45
46    /**
47     * @param bool $isEnabled
48     * @return $this
49     */
50    public function setIsEnabled(bool $isEnabled)
51    {
52        $this->isEnabled = $isEnabled;
53
54        return $this;
55    }
56
57    /**
58     * @return bool
59     */
60    public function getIsEnabled(): bool
61    {
62        return $this->isEnabled;
63    }
64
65    /**
66     * {@inheritdoc}
67     */
68    public function toAsn1(): AbstractType
69    {
70        $this->controlValue = Asn1::sequence(Asn1::integer($this->isEnabled ? 1 : 0));
71
72        return parent::toAsn1();
73    }
74
75    /**
76     * {@inheritdoc}
77     */
78    public static function fromAsn1(AbstractType $type)
79    {
80        $request = self::decodeEncodedValue($type);
81        if (!$request instanceof SequenceType) {
82            throw new ProtocolException('A PolicyHints control value must be a sequence type.');
83        }
84        $isEnabled = $request->getChild(0);
85        if (!$isEnabled instanceof IntegerType) {
86            throw new ProtocolException('A PolicyHints control value sequence 0 must be an integer type.');
87        }
88        $control = new self($isEnabled->getValue());
89
90        return self::mergeControlData($control, $type);
91    }
92}
93