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\Sorting;
12
13/**
14 * Represents a server side sorting request SortKey.
15 *
16 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
17 */
18class SortKey
19{
20    /**
21     * @var string
22     */
23    protected $attribute;
24
25    /**
26     * @var null|string
27     */
28    protected $orderingRule;
29
30    /**
31     * @var bool
32     */
33    protected $useReverseOrder;
34
35    /**
36     * @param string $attribute
37     * @param bool $useReverseOrder
38     * @param null|string $orderingRule
39     */
40    public function __construct(string $attribute, bool $useReverseOrder = false, ?string $orderingRule = null)
41    {
42        $this->attribute = $attribute;
43        $this->orderingRule = $orderingRule;
44        $this->useReverseOrder = $useReverseOrder;
45    }
46
47    /**
48     * @return string
49     */
50    public function getAttribute(): string
51    {
52        return $this->attribute;
53    }
54
55    /**
56     * @param string $attribute
57     * @return $this
58     */
59    public function setAttribute(string $attribute)
60    {
61        $this->attribute = $attribute;
62
63        return $this;
64    }
65
66    /**
67     * @return string
68     */
69    public function getOrderingRule(): ?string
70    {
71        return $this->orderingRule;
72    }
73
74    /**
75     * @param string $orderingRule
76     * @return $this
77     */
78    public function setOrderingRule(?string $orderingRule)
79    {
80        $this->orderingRule = $orderingRule;
81
82        return $this;
83    }
84
85    /**
86     * @return bool
87     */
88    public function getUseReverseOrder(): bool
89    {
90        return $this->useReverseOrder;
91    }
92
93    /**
94     * @param bool $useReverseOrder
95     * @return $this
96     */
97    public function setUseReverseOrder(bool $useReverseOrder)
98    {
99        $this->useReverseOrder = $useReverseOrder;
100
101        return $this;
102    }
103
104    /**
105     * Create an ascending sort key.
106     *
107     * @param string $attribute
108     * @param null|string $matchRule
109     * @return SortKey
110     */
111    public static function ascending(string $attribute, ?string $matchRule = null)
112    {
113        return new self($attribute, false, $matchRule);
114    }
115
116    /**
117     * Create a descending sort key.
118     *
119     * @param string $attribute
120     * @param null|string $matchRule
121     * @return SortKey
122     */
123    public static function descending(string $attribute, ?string $matchRule = null)
124    {
125        return new self($attribute, true, $matchRule);
126    }
127}
128