/
Abstract.php
153 lines (131 loc) · 4.22 KB
/
Abstract.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* @copyright Copyright (c) Metaways Infosystems GmbH, 2011
* @license LGPLv3, http://www.gnu.org/licenses/lgpl.html
* @package MW
* @subpackage Common
*/
/**
* Abstract search class
*
* @package MW
* @subpackage Common
*/
abstract class MW_Common_Criteria_Abstract implements MW_Common_Criteria_Interface
{
/**
* Creates a function signature for expressions.
*
* @param string $name Function name
* @param array $params Single- or multi-dimensional list of parameters of type boolean, integer, float and string
*/
public function createFunction( $name, array $params )
{
return MW_Common_Criteria_Expression_Abstract::createFunction( $name, $params );
}
/**
* Creates condition expressions from a multi-dimensional associative array.
*
* The simplest form of a valid associative array is a single comparison:
* $array = array(
* '==' => array( 'name' => 'value' ),
* );
*
* Combining several conditions can look like:
* $array = array(
* '&&' => array(
* 0 => array(
* '==' => array( 'name' => 'value' ),
* 1 => array(
* '==' => array( 'name2' => 'value2' ),
* ),
* ),
* );
*
* Nested combine operators are also possible.
*
* @param array $array Multi-dimensional associative array containing the expression arrays
* @return MW_Common_Criteria_Expression_Interface Condition expressions (maybe nested)
* @throws MW_Common_Exception If given array is invalid
*/
public function toConditions( array $array )
{
if( count( $array ) === 0 ) {
return $this->compare( '==', '1', '1' );
}
if( ( list( $op, $value ) = each( $array ) ) === false ) {
throw new MW_Common_Exception( sprintf( 'Invalid condition array "%1$s"', json_encode( $array ) ) );
}
$operators = $this->getOperators();
if( in_array( $op, $operators['combine'] ) ) {
return $this->_createCombineExpression( $op, (array) $value );
}
else if( in_array( $op, $operators['compare'] ) ) {
return $this->_createCompareExpression( $op, (array) $value );
}
throw new MW_Common_Exception( sprintf( 'Invalid operator "%1$s"', $op ) );
}
/**
* Creates sortation expressions from an associative array.
*
* The array must be a single-dimensional array of name and operator pairs like
* $array = array(
* 'name' => '+',
* 'name2' => '-',
* );
*
* @param array $array Single-dimensional array of name and operator pairs
* @return array List of sort expressions implementing MW_Common_Criteria_Expression_Sort_Interface
*/
public function toSortations( array $array )
{
$results = array();
foreach( $array as $name => $op ) {
$results[] = $this->sort( $op, $name );
}
return $results;
}
/**
* Creates a "combine" expression.
*
* @param string $operator One of the "combine" operators
* @param array $list List of arrays with "combine" or "compare" representations
* @throws MW_Common_Exception If operator is invalid
*/
protected function _createCombineExpression( $operator, array $list )
{
$results = array();
$operators = $this->getOperators();
foreach( $list as $entry )
{
$entry = (array) $entry;
if( ( list( $op, $value ) = each( $entry ) ) === false ) {
throw new MW_Common_Exception( sprintf( 'Invalid combine condition array "%1$s"', json_encode( $entry ) ) );
}
if( in_array( $op, $operators['combine'] ) ) {
$results[] = $this->_createCombineExpression( $op, (array) $entry[$op] );
}
else if( in_array( $op, $operators['compare'] ) ) {
$results[] = $this->_createCompareExpression( $op, (array) $entry[$op] );
}
else {
throw new MW_Common_Exception( sprintf( 'Invalid operator "%1$s"', $op ) );
}
}
return $this->combine( $operator, $results );
}
/**
* Creates a "compare" expression.
*
* @param string $op One of the "compare" operators
* @param array $pair Associative list containing one name/value pair
* @throws MW_Common_Exception If no name/value pair is available
*/
protected function _createCompareExpression( $op, array $pair )
{
if( ( list( $name, $value ) = each( $pair ) ) === false ) {
throw new MW_Common_Exception( sprintf( 'Invalid compare condition array "%1$s"', json_encode( $pair ) ) );
}
return $this->compare( $op, $name, $value );
}
}