Base::toSource()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 29
rs 8.8333
cc 7
nc 6
nop 4
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2015-2025
7
 * @package Base
8
 * @subpackage Common
9
 */
10
11
12
namespace Aimeos\Base\Criteria\Expression\Compare;
13
14
15
/**
16
 * Abstract class with common methods for comparing objects.
17
 *
18
 * @package Base
19
 * @subpackage Common
20
 */
21
abstract class Base implements Iface
22
{
23
	use \Aimeos\Base\Criteria\Expression\Traits;
24
25
	private string $operator;
26
	private string $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|\Aimeos\Map $value Value that the variable or column should be compared to
0 ignored issues
show
Bug introduced by
The type Aimeos\Map was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
	 */
37
	public function __construct( string $operator, string $name, $value )
38
	{
39
		$this->value = is_array( $value ) ? $value : ( is_iterable( $value ) ? iterator_to_array( $value, true ) : $value );
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type string; however, parameter $iterator of iterator_to_array() does only seem to accept Traversable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
		$this->value = is_array( $value ) ? $value : ( is_iterable( $value ) ? iterator_to_array( /** @scrutinizer ignore-type */ $value, true ) : $value );
Loading history...
40
		$this->operator = $operator;
41
		$this->name = $name;
42
	}
43
44
45
	/**
46
	 * Returns an array representation of the expression that can be parsed again
47
	 *
48
	 * @return array Multi-dimensional expression structure
49
	 */
50
	public function __toArray() : array
51
	{
52
		return [$this->operator => [$this->name => $this->value]];
53
	}
54
55
56
	/**
57
	 * Returns the operator used for the expression.
58
	 *
59
	 * @return string Operator used for the expression
60
	 */
61
	public function getOperator() : string
62
	{
63
		return $this->operator;
64
	}
65
66
67
	/**
68
	 * Returns the left side of the compare expression.
69
	 *
70
	 * @return string Name of variable or column that should be compared
71
	 */
72
	public function getName() : string
73
	{
74
		return $this->name;
75
	}
76
77
78
	/**
79
	 * Returns the right side of the compare expression.
80
	 *
81
	 * @return string|array Value that the variable or column should be compared to
82
	 */
83
	public function getValue()
84
	{
85
		return $this->value;
86
	}
87
88
89
	/**
90
	 * Generates a string from the expression objects.
91
	 *
92
	 * @param array $types Associative list of variable or column names as keys and their corresponding types
93
	 * @param array $translations Associative list of variable or column names that should be translated
94
	 * @param \Aimeos\Base\Criteria\Plugin\Iface[] $plugins Associative list of item names as keys and plugin objects as values
95
	 * @param array $funcs Associative list of item names and functions modifying the conditions
96
	 * @return mixed Expression that evaluates to a boolean result
97
	 */
98
	public function toSource( array $types, array $translations = [], array $plugins = [], array $funcs = [] )
99
	{
100
		$this->setPlugins( $plugins );
101
102
		$name = $this->name;
103
104
		if( !( $transname = $this->translateName( $name, $translations, $funcs ) ) ) {
105
			return;
106
		}
107
108
		if( !isset( $types[$name] ) ) {
109
			throw new \Aimeos\Base\Exception( sprintf( 'Invalid name "%1$s"', $name ) );
110
		}
111
112
		$transvalue = $this->translateValue( $name, $this->value, $types[$name] );
113
114
		if( $transvalue === null && in_array( $this->getOperator(), ['==', '!='] ) ) {
115
			return $this->createNullTerm( $transname, $types[$name] );
116
		}
117
118
		if( is_array( $transname ) ) {
119
			return $transname;
120
		}
121
122
		if( is_array( $transvalue ) ) {
123
			return $this->createListTerm( $transname, $types[$name] );
124
		}
125
126
		return $this->createTerm( $transname, $types[$name], $this->value );
127
	}
128
129
130
	/**
131
	 * Creates a term string from the given parameters.
132
	 *
133
	 * @param string|array $name Translated name(s) of the variable or column
134
	 * @param string $type Type constant
135
	 * @param mixed $value Value that the variable or column should be compared to
136
	 * @return mixed Created term
137
	 */
138
	abstract protected function createTerm( $name, string $type, $value );
139
140
141
	/**
142
	 * Creates a term which contains a null value.
143
	 *
144
	 * @param string|array $name Translated name(s) of the variable or column
145
	 * @param string $type Code of the internal value type
146
	 * @return mixed Created null term
147
	 */
148
	abstract protected function createNullTerm( $name, string $type );
149
150
151
	/**
152
	 * Creates a term from a list of values.
153
	 *
154
	 * @param string|array $name Translated name(s) of the variable or column
155
	 * @param string $type Type constant
156
	 * @return mixed Created list term
157
	 */
158
	abstract protected function createListTerm( $name, string $type );
159
}
160