1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\Ldap;
11
12/**
13 * Zend\Ldap\Filter.
14 */
15class Filter extends Filter\StringFilter
16{
17    const TYPE_EQUALS         = '=';
18    const TYPE_GREATER        = '>';
19    const TYPE_GREATEROREQUAL = '>=';
20    const TYPE_LESS           = '<';
21    const TYPE_LESSOREQUAL    = '<=';
22    const TYPE_APPROX         = '~=';
23
24    /**
25     * Creates an 'equals' filter.
26     * (attr=value)
27     *
28     * @param  string $attr
29     * @param  string $value
30     * @return Filter
31     */
32    public static function equals($attr, $value)
33    {
34        return new static($attr, $value, self::TYPE_EQUALS, null, null);
35    }
36
37    /**
38     * Creates a 'begins with' filter.
39     * (attr=value*)
40     *
41     * @param  string $attr
42     * @param  string $value
43     * @return Filter
44     */
45    public static function begins($attr, $value)
46    {
47        return new static($attr, $value, self::TYPE_EQUALS, null, '*');
48    }
49
50    /**
51     * Creates an 'ends with' filter.
52     * (attr=*value)
53     *
54     * @param  string $attr
55     * @param  string $value
56     * @return Filter
57     */
58    public static function ends($attr, $value)
59    {
60        return new static($attr, $value, self::TYPE_EQUALS, '*', null);
61    }
62
63    /**
64     * Creates a 'contains' filter.
65     * (attr=*value*)
66     *
67     * @param  string $attr
68     * @param  string $value
69     * @return Filter
70     */
71    public static function contains($attr, $value)
72    {
73        return new static($attr, $value, self::TYPE_EQUALS, '*', '*');
74    }
75
76    /**
77     * Creates a 'greater' filter.
78     * (attr>value)
79     *
80     * @param  string $attr
81     * @param  string $value
82     * @return Filter
83     */
84    public static function greater($attr, $value)
85    {
86        return new static($attr, $value, self::TYPE_GREATER, null, null);
87    }
88
89    /**
90     * Creates a 'greater or equal' filter.
91     * (attr>=value)
92     *
93     * @param  string $attr
94     * @param  string $value
95     * @return Filter
96     */
97    public static function greaterOrEqual($attr, $value)
98    {
99        return new static($attr, $value, self::TYPE_GREATEROREQUAL, null, null);
100    }
101
102    /**
103     * Creates a 'less' filter.
104     * (attr<value)
105     *
106     * @param  string $attr
107     * @param  string $value
108     * @return Filter
109     */
110    public static function less($attr, $value)
111    {
112        return new static($attr, $value, self::TYPE_LESS, null, null);
113    }
114
115    /**
116     * Creates an 'less or equal' filter.
117     * (attr<=value)
118     *
119     * @param  string $attr
120     * @param  string $value
121     * @return Filter
122     */
123    public static function lessOrEqual($attr, $value)
124    {
125        return new static($attr, $value, self::TYPE_LESSOREQUAL, null, null);
126    }
127
128    /**
129     * Creates an 'approx' filter.
130     * (attr~=value)
131     *
132     * @param  string $attr
133     * @param  string $value
134     * @return Filter
135     */
136    public static function approx($attr, $value)
137    {
138        return new static($attr, $value, self::TYPE_APPROX, null, null);
139    }
140
141    /**
142     * Creates an 'any' filter.
143     * (attr=*)
144     *
145     * @param  string $attr
146     * @return Filter
147     */
148    public static function any($attr)
149    {
150        return new static($attr, '', self::TYPE_EQUALS, '*', null);
151    }
152
153    /**
154     * Creates a simple custom string filter.
155     *
156     * @param  string $filter
157     * @return Filter\StringFilter
158     */
159    public static function string($filter)
160    {
161        return new Filter\StringFilter($filter);
162    }
163
164    /**
165     * Creates a simple string filter to be used with a mask.
166     *
167     * @param string $mask
168     * @param string $value
169     * @return Filter\MaskFilter
170     */
171    public static function mask($mask, $value)
172    {
173        return new Filter\MaskFilter($mask, $value);
174    }
175
176    /**
177     * Creates an 'and' filter.
178     *
179     * @param  Filter\AbstractFilter $filter,...
180     * @return Filter\AndFilter
181     */
182    public static function andFilter($filter)
183    {
184        return new Filter\AndFilter(func_get_args());
185    }
186
187    /**
188     * Creates an 'or' filter.
189     *
190     * @param  Filter\AbstractFilter $filter,...
191     * @return Filter\OrFilter
192     */
193    public static function orFilter($filter)
194    {
195        return new Filter\OrFilter(func_get_args());
196    }
197
198    /**
199     * Create a filter string.
200     *
201     * @param  string $attr
202     * @param  string $value
203     * @param  string $filtertype
204     * @param  string $prepend
205     * @param  string $append
206     * @return string
207     */
208    private static function createFilterString($attr, $value, $filtertype, $prepend = null, $append = null)
209    {
210        $str = $attr . $filtertype;
211        if ($prepend !== null) {
212            $str .= $prepend;
213        }
214        $str .= static::escapeValue($value);
215        if ($append !== null) {
216            $str .= $append;
217        }
218        return $str;
219    }
220
221    /**
222     * Creates a new Zend\Ldap\Filter.
223     *
224     * @param string $attr
225     * @param string $value
226     * @param string $filtertype
227     * @param string $prepend
228     * @param string $append
229     */
230    public function __construct($attr, $value, $filtertype, $prepend = null, $append = null)
231    {
232        $filter = static::createFilterString($attr, $value, $filtertype, $prepend, $append);
233        parent::__construct($filter);
234    }
235}
236