Passed
Push — master ( 04e202...9b7189 )
by Aimeos
05:11
created

Base::getSearchFunctions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 1
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-2023
7
 * @package MW
8
 * @subpackage Tree
9
 */
10
11
12
namespace Aimeos\MW\Tree\Manager;
13
14
15
/**
16
 * Abstract tree manager class with basic methods.
17
 *
18
 * @package MW
19
 * @subpackage Tree
20
 */
21
abstract class Base implements \Aimeos\MW\Tree\Manager\Iface
22
{
23
	/**
24
	 * Returns only the requested node
25
	 */
26
	const LEVEL_ONE = 1;
27
28
	/**
29
	 * Returns the requested node and its children
30
	 */
31
	const LEVEL_LIST = 2;
32
33
	/**
34
	 * Returns all subnodes including the requested one
35
	 */
36
	const LEVEL_TREE = 3;
37
38
39
	private $readOnly = false;
40
	/**
41
	 * Returns the attribute helper functions for searching defined by the manager.
42
	 *
43
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
44
	 * @return array Associative array of attribute code and helper function
45
	 */
46
	protected function getSearchFunctions( array $attributes ) : array
47
	{
48
		$list = [];
49
		$iface = \Aimeos\Base\Criteria\Attribute\Iface::class;
50
51
		foreach( $attributes as $key => $item )
52
		{
53
			if( $item instanceof $iface ) {
54
				$list[$item->getCode()] = $item->getFunction();
55
			} else if( isset( $item['code'] ) ) {
56
				$list[$item['code']] = $item['function'] ?? null;
57
			} else {
58
				throw new \Aimeos\MW\Common\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) );
0 ignored issues
show
Bug introduced by
The type Aimeos\MW\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...
59
			}
60
		}
61
62
		return $list;
63
	}
64
65
66
	/**
67
	 * Returns the attribute translations for searching defined by the manager.
68
	 *
69
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
70
	 * @return array Associative array of attribute code and internal attribute code
71
	 */
72
	protected function getSearchTranslations( array $attributes ) : array
73
	{
74
		$translations = [];
75
		$iface = \Aimeos\Base\Criteria\Attribute\Iface::class;
76
77
		foreach( $attributes as $key => $item )
78
		{
79
			if( $item instanceof $iface ) {
80
				$translations[$item->getCode()] = $item->getInternalCode();
81
			} else if( isset( $item['code'] ) ) {
82
				$translations[$item['code']] = $item['internalcode'];
83
			} else {
84
				throw new \Aimeos\MW\Common\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) );
85
			}
86
		}
87
88
		return $translations;
89
	}
90
91
92
	/**
93
	 * Returns the attribute types for searching defined by the manager.
94
	 *
95
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
96
	 * @return array Associative array of attribute code and internal attribute type
97
	 */
98
	protected function getSearchTypes( array $attributes ) : array
99
	{
100
		$types = [];
101
		$iface = \Aimeos\Base\Criteria\Attribute\Iface::class;
102
103
		foreach( $attributes as $key => $item )
104
		{
105
			if( $item instanceof $iface ) {
106
				$types[$item->getCode()] = $item->getInternalType();
107
			} else if( isset( $item['code'] ) ) {
108
				$types[$item['code']] = $item['internaltype'];
109
			} else {
110
				throw new \Aimeos\MW\Common\Exception( sprintf( 'Invalid attribute at position "%1$d"', $key ) );
111
			}
112
		}
113
114
		return $types;
115
	}
116
117
118
	/**
119
	 * Checks, whether a tree is read only.
120
	 *
121
	 * @return bool True if tree is read-only, false if not
122
	 */
123
	public function isReadOnly() : bool
124
	{
125
		return $this->readOnly;
126
	}
127
128
129
	/**
130
	 * Sets this manager to read only.
131
	 *
132
	 * @param bool $flag True if tree is read-only, false if not
133
	 */
134
	protected function setReadOnly( bool $flag = true ) : Iface
135
	{
136
		$this->readOnly = (bool) $flag;
137
		return $this;
138
	}
139
}
140