1 <?php
2
3 /**
4 * AfRegexFilter class file.
5 *
6 * @author Keith Burton <kburton@kappasoft.net>
7 * @package advancedfilters.filters
8 */
9
10 /**
11 * This filter handles regular expression patterns.
12 *
13 * The filter syntax is entirely determined by the database being queried.
14 */
15 class AfRegexFilter extends AfBaseFilter
16 {
17 /**
18 * @var string the prefix string to identify regex filter expressions.
19 */
20 public $prefix = '/';
21
22 /**
23 * @var string the suffix string to identify regex filter expressions.
24 */
25 public $suffix = '/';
26
27 private $pattern;
28
29 /**
30 * Determines whether the provided expression can be processed by this
31 * filter class.
32 *
33 * @return boolean whether this class can process the expression.
34 */
35 public function acceptsFilterExpression()
36 {
37 return $this->parseExpression();
38 }
39
40 /**
41 * Builds a new CDbCriteria object based on the expression provided.
42 *
43 * @return CDbCriteria the new criteria object.
44 */
45 public function getCriteria()
46 {
47 $this->parseExpression();
48
49 $criteria = new CDbCriteria;
50
51 $this->getDbHelper()->addRegexCondition($criteria,
52 $this->columnExpression, $this->pattern, $this->invertLogic);
53
54 return $criteria;
55 }
56
57 private $_expressionAccepted;
58 private function parseExpression()
59 {
60 // Only need to parse the expression once
61 if ($this->_expressionAccepted !== null)
62 return $this->_expressionAccepted;
63
64 $this->_expressionAccepted = false;
65
66 // First strip the suffix and prefix and fail if they aren't found
67 $result = self::stripPrefixSuffixString($this->filterExpression,
68 $this->prefix, $this->suffix);
69
70 if ($result === false)
71 return $this->_expressionAccepted = false;
72
73 // Fail if the database doesn't recognise the pattern syntax
74 if (!$this->getDbHelper()->checkRegex(
75 $this->getDbConnection(), $result))
76 return $this->_expressionAccepted = false;
77
78 // Expression was accepted so save the pattern
79 $this->pattern = $result;
80
81 return $this->_expressionAccepted = true;
82 }
83 }
84