/library/Zend/Ldap/Filter.php

https://github.com/bruisedlee/zf2 · PHP · 256 lines · 80 code · 18 blank · 158 comment · 2 complexity · 7e979b90394710e19b42cdabf15c3900 MD5 · raw file

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