1<?php
2/**
3 * This file is part of the FreeDSx LDAP 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\Protocol;
12
13use FreeDSx\Asn1\Encoder\BerEncoder;
14use FreeDSx\Asn1\Type\AbstractType;
15
16/**
17 * Applies some LDAP specific rules, and mappings, to the BER encoder, specified in RFC 4511.
18 *
19 *    - Only the definite form of length encoding is used.
20 *    - OCTET STRING values are encoded in the primitive form only.
21 *    - If the value of a BOOLEAN type is true, the encoding of the value octet is set to hex "FF".
22 *    - If a value of a type is its default value, it is absent.
23 *
24 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
25 */
26class LdapEncoder extends BerEncoder
27{
28    /**
29     * {@inheritdoc}
30     */
31    public function __construct()
32    {
33        parent::__construct([
34            'primitive_only' => [
35                AbstractType::TAG_TYPE_OCTET_STRING,
36            ],
37        ]);
38        $this->setTagMap(AbstractType::TAG_CLASS_APPLICATION, [
39            0 => AbstractType::TAG_TYPE_SEQUENCE,
40            1 => AbstractType::TAG_TYPE_SEQUENCE,
41            2 => AbstractType::TAG_TYPE_NULL,
42            3 => AbstractType::TAG_TYPE_SEQUENCE,
43            4 => AbstractType::TAG_TYPE_SEQUENCE,
44            5 => AbstractType::TAG_TYPE_SEQUENCE,
45            6 => AbstractType::TAG_TYPE_SEQUENCE,
46            7 => AbstractType::TAG_TYPE_SEQUENCE,
47            8 => AbstractType::TAG_TYPE_SEQUENCE,
48            9 => AbstractType::TAG_TYPE_SEQUENCE,
49            10 => AbstractType::TAG_TYPE_OCTET_STRING,
50            11 => AbstractType::TAG_TYPE_SEQUENCE,
51            12 => AbstractType::TAG_TYPE_SEQUENCE,
52            13 => AbstractType::TAG_TYPE_SEQUENCE,
53            14 => AbstractType::TAG_TYPE_SEQUENCE,
54            15 => AbstractType::TAG_TYPE_SEQUENCE,
55            16 => AbstractType::TAG_TYPE_INTEGER,
56            19 => AbstractType::TAG_TYPE_SEQUENCE,
57            23 => AbstractType::TAG_TYPE_SEQUENCE,
58            24 => AbstractType::TAG_TYPE_SEQUENCE,
59            25 => AbstractType::TAG_TYPE_SEQUENCE,
60        ]);
61    }
62}
63