Passed
Push — master ( 9b3ef7...f502a6 )
by Aimeos
02:50
created

SQL::getOperators()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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-2022
7
 * @package Base
8
 * @subpackage Common
9
 */
10
11
12
namespace Aimeos\Base\Criteria\Expression\Sort;
13
14
15
/**
16
 * SQL implementation for sorting objects.
17
 *
18
 * @package Base
19
 * @subpackage Common
20
 */
21
class SQL extends Base
22
{
23
	private static $operators = array( '+' => 'ASC', '-' => 'DESC' );
24
	private $conn;
25
26
27
	/**
28
	 * Initializes the object.
29
	 *
30
	 * @param \Aimeos\Base\DB\Connection\Iface $conn Database connection object
31
	 * @param string $operator Sorting operator ("+": ascending, "-": descending)
32
	 * @param string $name Name of the variable or column to sort
33
	 */
34
	public function __construct( \Aimeos\Base\DB\Connection\Iface $conn, string $operator, string $name )
35
	{
36
		if( !isset( self::$operators[$operator] ) ) {
37
			throw new \Aimeos\Base\Common\Exception( sprintf( 'Invalid operator "%1$s"', $operator ) );
0 ignored issues
show
Bug introduced by
The type Aimeos\Base\Common\Exception 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...
38
		}
39
40
		parent::__construct( $operator, $name );
41
		$this->conn = $conn;
42
	}
43
44
45
	/**
46
	 * Returns the available operators for the expression.
47
	 *
48
	 * @return array List of available operators
49
	 */
50
	public static function getOperators() : array
51
	{
52
		return array_keys( self::$operators );
53
	}
54
55
56
	/**
57
	 * Generates a string from the expression objects.
58
	 *
59
	 * @param array $types Associative list of variable or column names as keys and their corresponding types
60
	 * @param array $translations Associative list of variable or column names that should be translated
61
	 * @param \Aimeos\Base\Criteria\Plugin\Iface[] $plugins Associative list of item names as keys and plugin objects as values
62
	 * @param array $funcs Associative list of item names and functions modifying the conditions
63
	 * @return mixed Expression that evaluates to a boolean result
64
	 */
65
	public function toSource( array $types, array $translations = [], array $plugins = [], array $funcs = [] )
66
	{
67
		$this->setPlugins( $plugins );
68
69
		$name = $this->getName();
70
		$transname = $this->translateName( $name, $translations, $funcs );
71
72
		if( !$transname ) {
73
			throw new \Aimeos\Base\Common\Exception( sprintf( 'Invalid sorting "%1$s"', $this->getName() ) );
74
		}
75
76
		if( !isset( $types[$name] ) ) {
77
			throw new \Aimeos\Base\Common\Exception( sprintf( 'Invalid name "%1$s"', $name ) );
78
		}
79
80
		return $transname . ' ' . self::$operators[$this->getOperator()];
0 ignored issues
show
Bug introduced by
Are you sure $transname of type array|mixed can be used in concatenation? ( Ignorable by Annotation )

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

80
		return /** @scrutinizer ignore-type */ $transname . ' ' . self::$operators[$this->getOperator()];
Loading history...
81
	}
82
83
84
	/**
85
	 * Escapes the value so it can be inserted into a SQL statement
86
	 *
87
	 * @param string $operator Operator used for the expression
88
	 * @param string $type Type constant
89
	 * @param mixed $value Value that the variable or column should be compared to
90
	 * @return string Escaped value
91
	 */
92
	protected function escape( string $operator, string $type, $value ) : string
93
	{
94
		$value = $this->translateValue( $this->getName(), $value );
95
96
		switch( $type )
97
		{
98
			case \Aimeos\Base\DB\Statement\Base::PARAM_BOOL:
99
				$value = (bool) $value; break;
100
			case \Aimeos\Base\DB\Statement\Base::PARAM_INT:
101
				$value = (int) $value; break;
102
			case \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT:
103
				$value = (float) $value; break;
104
			case \Aimeos\Base\DB\Statement\Base::PARAM_STR:
105
				if( $operator == '~=' )
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
106
				{
107
					$value = '\'%' . $this->conn->escape( $value ) . '%\'';
108
					break;
109
				}
110
			default: // all other operators: escape in default case
111
				$value = '\'' . $this->conn->escape( $value ) . '\'';
112
		}
113
114
		return (string) $value;
115
	}
116
117
118
	/**
119
	 * Returns the internal type of the function parameter.
120
	 *
121
	 * @param mixed &$item Reference to parameter value (will be updated if necessary)
122
	 * @return string Internal parameter type
123
	 * @throws \Aimeos\Base\Common\Exception If an error occurs
124
	 */
125
	protected function getParamType( &$item ) : string
126
	{
127
		if( is_null( $item ) ) {
128
			return \Aimeos\Base\DB\Statement\Base::PARAM_NULL;
129
		} elseif( is_float( $item ) ) {
130
			return \Aimeos\Base\DB\Statement\Base::PARAM_FLOAT;
131
		} elseif( is_int( $item ) ) {
132
			return \Aimeos\Base\DB\Statement\Base::PARAM_INT;
133
		}
134
135
		return \Aimeos\Base\DB\Statement\Base::PARAM_STR;
136
	}
137
}
138