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 * Used to represent the Extended DN control.
22 *
23 * @see https://msdn.microsoft.com/en-us/library/cc223349.aspx
24 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
25 */
26class ExtendedDnControl extends Control
27{
28    /**
29     * @var bool
30     */
31    protected $useHexFormat;
32
33    /**
34     * @param bool $useHexFormat
35     */
36    public function __construct(bool $useHexFormat = false)
37    {
38        $this->useHexFormat = $useHexFormat;
39        parent::__construct(self::OID_EXTENDED_DN);
40    }
41
42    /**
43     * @return bool
44     */
45    public function getUseHexFormat(): bool
46    {
47        return $this->useHexFormat;
48    }
49
50    /**
51     * @param bool $useHexFormat
52     * @return $this
53     */
54    public function setUseHexFormat(bool $useHexFormat)
55    {
56        $this->useHexFormat = $useHexFormat;
57
58        return $this;
59    }
60
61    /**
62     * {@inheritdoc}
63     */
64    public static function fromAsn1(AbstractType $type)
65    {
66        if (!$type instanceof SequenceType) {
67            throw new ProtocolException('The extended DN control is malformed.');
68        }
69        [0 => $oid, 1 => $criticality, 2 => $value] = self::parseAsn1ControlValues($type);
70
71        $useHexFormat = false;
72        if ($value !== null) {
73            $request = self::decodeEncodedValue($type);
74            if (!$request instanceof SequenceType) {
75                throw new ProtocolException('An ExtendedDn control value must be a sequence type.');
76            }
77            $useHexFormat = $request->getChild(0);
78            if (!$useHexFormat instanceof IntegerType) {
79                throw new ProtocolException('An ExtendedDn control value sequence 0 must be an integer type.');
80            }
81            $useHexFormat = ($useHexFormat->getValue() === 0);
82        }
83
84        $control = new self($useHexFormat);
85        $control->controlType = $oid;
86        $control->criticality = $criticality;
87
88        return $control;
89    }
90
91    /**
92     * {@inheritdoc}
93     */
94    public function toAsn1(): AbstractType
95    {
96        $useHexFormat = $this->useHexFormat ? 0 : 1;
97        $this->controlValue = Asn1::sequence(Asn1::integer($useHexFormat));
98
99        return parent::toAsn1();
100    }
101
102    /**
103     * @param AbstractType $type
104     * @throws ProtocolException
105     */
106    protected static function validate(AbstractType $type): void
107    {
108        if (!($type instanceof SequenceType && \count($type) === 1)) {
109            throw new ProtocolException('An ExtendedDn control value must be a sequence type with 1 child.');
110        }
111        if (!$type->getChild(0) instanceof IntegerType) {
112            throw new ProtocolException('An ExtendedDn control value sequence 0 must be an integer type.');
113        }
114    }
115}
116