Passed
Push — master ( 9f1c00...0115e8 )
by Aimeos
05:45
created

Methods::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2023
6
 * @package MShop
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\MShop\Common\Manager;
12
13
14
/**
15
 * Method trait for managers
16
 *
17
 * @package MShop
18
 * @subpackage Common
19
 */
20
trait Methods
21
{
22
	private ?\Aimeos\MShop\Common\Manager\Iface $object = null;
23
	private array $filterFcn = [];
24
	private string $domain;
25
	private string $subpath;
26
27
28
	/**
29
	 * Adds a filter callback for an item type
30
	 *
31
	 * @param string $iface Interface name of the item to apply the filter to
32
	 * @param \Closure $fcn Anonymous function receiving the item to check as first parameter
33
	 */
34
	public function addFilter( string $iface, \Closure $fcn )
35
	{
36
		if( !isset( $this->filterFcn[$iface] ) ) {
37
			$this->filterFcn[$iface] = [];
38
		}
39
40
		$this->filterFcn[$iface][] = $fcn;
41
	}
42
43
44
	/**
45
	 * Returns the class names of the manager and used decorators.
46
	 *
47
	 * @return array List of class names
48
	 */
49
	public function classes() : array
50
	{
51
		return [get_class( $this )];
52
	}
53
54
55
	/**
56
	 * Removes old entries from the storage
57
	 *
58
	 * @param iterable $siteids List of IDs for sites whose entries should be deleted
59
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
60
	 */
61
	public function clear( iterable $siteids ) : \Aimeos\MShop\Common\Manager\Iface
62
	{
63
		return $this;
64
	}
65
66
67
	/**
68
	 * Creates a new empty item instance
69
	 *
70
	 * @param array $values Values the item should be initialized with
71
	 * @return \Aimeos\MShop\Attribute\Item\Iface New attribute item object
72
	 */
73
	public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface
74
	{
75
		return new \Aimeos\MShop\Common\Item\Base( $this->prefix(), $values );
76
	}
77
78
79
	/**
80
	 * Creates a new cursor based on the filter criteria
81
	 *
82
	 * @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc.
83
	 * @return \Aimeos\MShop\Common\Cursor\Iface Cursor object
84
	 */
85
	public function cursor( \Aimeos\Base\Criteria\Iface $filter ) : \Aimeos\MShop\Common\Cursor\Iface
86
	{
87
		return new \Aimeos\MShop\Common\Cursor\Standard( $filter );
88
	}
89
90
91
	/**
92
	 * Deletes one or more items.
93
	 *
94
	 * @param \Aimeos\MShop\Common\Item\Iface|\Aimeos\Map|array|string $items Item object, ID or a list of them
95
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
96
	 */
97
	public function delete( $items ) : \Aimeos\MShop\Common\Manager\Iface
98
	{
99
		return $this;
100
	}
101
102
103
	/**
104
	 * Creates a filter object.
105
	 *
106
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
107
	 * @param bool $site TRUE for adding site criteria to limit items by the site of related items
108
	 * @return \Aimeos\Base\Criteria\Iface Returns the filter object
109
	 */
110
	public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface
111
	{
112
		throw new \LogicException( 'Not implemented' );
113
	}
114
115
116
	/**
117
	 * Returns the item specified by its ID
118
	 *
119
	 * @param string $id Id of item
120
	 * @param string[] $ref List of domains to fetch list items and referenced items for
121
	 * @param bool|null $default Add default criteria or NULL for relaxed default criteria
122
	 * @return \Aimeos\MShop\Common\Item\Iface Item object
123
	 */
124
	public function get( string $id, array $ref = [], ?bool $default = false ) : \Aimeos\MShop\Common\Item\Iface
125
	{
126
		throw new \LogicException( 'Not implemented' );
127
	}
128
129
130
	/**
131
	 * Returns the additional column/search definitions
132
	 *
133
	 * @return array Associative list of column names as keys and items implementing \Aimeos\Base\Criteria\Attribute\Iface
134
	 */
135
	public function getSaveAttributes() : array
136
	{
137
		return [];
138
	}
139
140
141
	/**
142
	 * Returns the attributes that can be used for searching.
143
	 *
144
	 * @param bool $withsub Return also attributes of sub-managers if true
145
	 * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of attribute items
146
	 */
147
	public function getSearchAttributes( bool $withsub = true ) : array
148
	{
149
		return [];
150
	}
151
152
153
	/**
154
	 * Returns a new manager for attribute extensions
155
	 *
156
	 * @param string $manager Name of the sub manager type in lower case
157
	 * @param string|null $name Name of the implementation, will be from configuration (or Default) if null
158
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager for different extensions, e.g Type, List's etc.
159
	 */
160
	public function getSubManager( string $manager, string $name = null ) : \Aimeos\MShop\Common\Manager\Iface
161
	{
162
		throw new \LogicException( 'Not implemented' );
163
	}
164
165
166
	/**
167
	 * Iterates over all matched items and returns the found ones
168
	 *
169
	 * @param \Aimeos\MShop\Common\Cursor\Iface $cursor Cursor object with filter, domains and cursor
170
	 * @param string[] $ref List of domains whose items should be fetched too
171
	 * @return \Aimeos\Map|null List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys
172
	 */
173
	public function iterate( \Aimeos\MShop\Common\Cursor\Iface $cursor, array $ref = [] ) : ?\Aimeos\Map
174
	{
175
		return null;
176
	}
177
178
179
	/**
180
	 * Adds or updates an item object or a list of them.
181
	 *
182
	 * @param \Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface $items Item or list of items whose data should be saved
183
	 * @param bool $fetch True if the new ID should be returned in the item
184
	 * @return \Aimeos\MShop\Common\Item\Iface[]|\Aimeos\MShop\Common\Item\Iface Saved item or items
185
	 */
186
	public function save( $items, bool $fetch = true )
187
	{
188
		return $items;
189
	}
190
191
192
	/**
193
	 * Searches for all items matching the given critera.
194
	 *
195
	 * @param \Aimeos\Base\Criteria\Iface $filter Criteria object with conditions, sortations, etc.
196
	 * @param string[] $ref List of domains to fetch list items and referenced items for
197
	 * @param int &$total Number of items that are available in total
198
	 * @return \Aimeos\Map List of items implementing \Aimeos\MShop\Common\Item\Iface with ids as keys
199
	 */
200
	public function search( \Aimeos\Base\Criteria\Iface $filter, array $ref = [], int &$total = null ) : \Aimeos\Map
201
	{
202
		return map();
203
	}
204
205
206
	/**
207
	 * Injects the reference of the outmost object
208
	 *
209
	 * @param \Aimeos\MShop\Common\Manager\Iface $object Reference to the outmost manager or decorator
210
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
211
	 */
212
	public function setObject( \Aimeos\MShop\Common\Manager\Iface $object ) : \Aimeos\MShop\Common\Manager\Iface
213
	{
214
		$this->object = $object;
215
		return $this;
216
	}
217
218
219
	/**
220
	 * Starts a database transaction on the connection identified by the given name
221
	 *
222
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
223
	 */
224
	public function begin() : \Aimeos\MShop\Common\Manager\Iface
225
	{
226
		return $this;
227
	}
228
229
230
	/**
231
	 * Commits the running database transaction on the connection identified by the given name
232
	 *
233
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
234
	 */
235
	public function commit() : \Aimeos\MShop\Common\Manager\Iface
236
	{
237
		return $this;
238
	}
239
240
241
	/**
242
	 * Rolls back the running database transaction on the connection identified by the given name
243
	 *
244
	 * @return \Aimeos\MShop\Common\Manager\Iface Manager object for chaining method calls
245
	 */
246
	public function rollback() : \Aimeos\MShop\Common\Manager\Iface
247
	{
248
		return $this;
249
	}
250
251
252
	/**
253
	 * Applies the filters for the item type to the item
254
	 *
255
	 * @param object $item Item to apply the filter to
256
	 * @return object|null Object if the item should be used, null if not
257
	 */
258
	protected function applyFilter( $item )
259
	{
260
		foreach( $this->filterFcn as $iface => $fcnList )
261
		{
262
			if( is_object( $item ) && $item instanceof $iface )
263
			{
264
				foreach( $fcnList as $fcn )
265
				{
266
					if( $fcn( $item ) === null ) {
267
						return null;
268
					}
269
				}
270
			}
271
		}
272
273
		return $item;
274
	}
275
276
277
	/**
278
	 * Creates the criteria attribute items from the list of entries
279
	 *
280
	 * @param array $list Associative array of code as key and array with properties as values
281
	 * @return \Aimeos\Base\Criteria\Attribute\Standard[] List of criteria attribute items
282
	 */
283
	protected function createAttributes( array $list ) : array
284
	{
285
		$attr = [];
286
287
		foreach( $list as $key => $fields )
288
		{
289
			$fields['code'] = $fields['code'] ?? $key;
290
			$fields['internalcode'] = $fields['internalcode'] ?? $key;
291
			$attr[$key] = new \Aimeos\Base\Criteria\Attribute\Standard( $fields );
292
		}
293
294
		return $attr;
295
	}
296
297
298
	/**
299
	 * Returns the prefix for the item properties and search keys.
300
	 *
301
	 * @return string Prefix for the item properties and search keys
302
	 */
303
	protected function prefix() : string
304
	{
305
		return '';
306
	}
307
308
309
	/**
310
	 * Returns the attribute helper functions for searching defined by the manager.
311
	 *
312
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
313
	 * @return array Associative array of attribute code and helper function
314
	 */
315
	protected function getSearchFunctions( array $attributes ) : array
316
	{
317
		$list = [];
318
319
		foreach( $attributes as $key => $item ) {
320
			$list[$item->getCode()] = $item->getFunction();
321
		}
322
323
		return $list;
324
	}
325
326
327
	/**
328
	 * Returns the attribute translations for searching defined by the manager.
329
	 *
330
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
331
	 * @return array Associative array of attribute code and internal attribute code
332
	 */
333
	protected function getSearchTranslations( array $attributes ) : array
334
	{
335
		$list = [];
336
337
		foreach( $attributes as $key => $item ) {
338
			$list[$item->getCode()] = $item->getInternalCode();
339
		}
340
341
		return $list;
342
	}
343
344
345
	/**
346
	 * Returns the attribute types for searching defined by the manager.
347
	 *
348
	 * @param \Aimeos\Base\Criteria\Attribute\Iface[] $attributes List of search attribute items
349
	 * @return array Associative array of attribute code and internal attribute type
350
	 */
351
	protected function getSearchTypes( array $attributes ) : array
352
	{
353
		$list = [];
354
355
		foreach( $attributes as $key => $item ) {
356
			$list[$item->getCode()] = $item->getInternalType();
357
		}
358
359
		return $list;
360
	}
361
362
363
	/**
364
	 * Returns the outmost decorator of the decorator stack
365
	 *
366
	 * @return \Aimeos\MShop\Common\Manager\Iface Outmost decorator object
367
	 */
368
	protected function object() : \Aimeos\MShop\Common\Manager\Iface
369
	{
370
		return $this->object ?? $this;
371
	}
372
}
373