Completed
Push — master ( 02237c...39a962 )
by Aimeos
13:42
created

Base::toString()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 8
nop 3
dl 0
loc 24
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2016
7
 * @package MW
8
 * @subpackage Common
9
 */
10
11
12
namespace Aimeos\MW\Criteria\Expression\Compare;
13
14
15
/**
16
 * Abstract class with common methods for comparing objects.
17
 *
18
 * @package MW
19
 * @subpackage Common
20
 */
21
abstract class Base
22
	extends \Aimeos\MW\Criteria\Expression\Base
0 ignored issues
show
Coding Style introduced by
Expected 0 spaces between "Base" and comma; 1 found
Loading history...
23
	implements \Aimeos\MW\Criteria\Expression\Compare\Iface
24
{
25
	private $operator;
26
	private $name;
27
	private $value;
28
29
30
	/**
31
	 * Initializes the object.
32
	 *
33
	 * @param string $operator Operator used for the expression
34
	 * @param string $name Name of variable or column that should be compared.
35
	 * @param string|array $value Value that the variable or column should be compared to
36
	 */
37
	public function __construct( $operator, $name, $value )
38
	{
39
		$this->operator = $operator;
40
		$this->name = $name;
41
		$this->value = $value;
42
	}
43
44
45
	/**
46
	 * Returns the operator used for the expression.
47
	 *
48
	 * @return string Operator used for the expression
49
	 */
50
	public function getOperator()
51
	{
52
		return $this->operator;
53
	}
54
55
56
	/**
57
	 * Returns the left side of the compare expression.
58
	 *
59
	 * @return string Name of variable or column that should be compared
60
	 */
61
	public function getName()
62
	{
63
		return $this->name;
64
	}
65
66
67
	/**
68
	 * Returns the right side of the compare expression.
69
	 *
70
	 * @return string|array Value that the variable or column should be compared to
71
	 */
72
	public function getValue()
73
	{
74
		return $this->value;
75
	}
76
77
78
	/**
79
	 * Generates a string from the expression objects.
80
	 *
81
	 * @param array $types Associative list of variable or column names as keys and their corresponding types
82
	 * @param array $translations Associative list of variable or column names that should be translated
83
	 * @param array $plugins Associative list of item names and plugins implementing \Aimeos\MW\Criteria\Plugin\Iface
84
	 * @return string Expression that evaluates to a boolean result
85
	 */
86
	public function toString( array $types, array $translations = array(), array $plugins = array() )
87
	{
88
		$this->setPlugins( $plugins );
89
90
		$name = $this->name;
91
92
		if( ( $transname = $this->translateName( $name, $translations ) ) === '' ) {
93
			$transname = $name;
94
		}
95
96
		if( !isset( $types[$name] ) ) {
97
			throw new \Aimeos\MW\Common\Exception( sprintf( 'Invalid name "%1$s"', $name ) );
98
		}
99
100
		if( $this->value === null && ( $this->operator === '==' || $this->operator === '!=' ) ) {
101
			return $this->createNullTerm( $transname );
102
		}
103
104
		if( is_array( $this->value ) ) {
105
			return $this->createListTerm( $transname, $types[$name] );
106
		}
107
108
		return $this->createTerm( $transname, $types[$name], $this->value );
109
	}
110
111
112
	/**
113
	 * Creates a term string from the given parameters.
114
	 *
115
	 * @param string $name Translated name of variable or column that should be compared
116
	 * @param integer $type Type constant
117
	 * @param string $value Value that the variable or column should be compared to
118
	 * @return string Created term string (name operator value)
119
	 */
120
	abstract protected function createTerm( $name, $type, $value );
121
122
123
	/**
124
	 * Creates a term which contains a null value.
125
	 *
126
	 * @param string $name Translated name of the variable or column
127
	 * @return string String that can be inserted into a SQL statement
128
	 */
129
	abstract protected function createNullTerm( $name );
130
131
132
	/**
133
	 * Creates a term from a list of values.
134
	 *
135
	 * @param string $name Translated name of the variable or column
136
	 * @param integer $type Type constant
137
	 * @return string String that can be inserted into a SQL statement
138
	 */
139
	abstract protected function createListTerm( $name, $type );
140
}
141