|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/mangan |
|
7
|
|
|
* @licence AGPL or Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft |
|
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
|
11
|
|
|
* @link https://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Traits\Criteria; |
|
15
|
|
|
|
|
16
|
|
|
use Maslosoft\Mangan\Interfaces\Criteria\SelectableInterface; |
|
17
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* SelectableTrait |
|
21
|
|
|
* @see CriteriaInterface |
|
22
|
|
|
* @see SelectableInterface |
|
23
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
24
|
|
|
*/ |
|
25
|
|
|
trait SelectableTrait |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
private $_select = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* List of fields to get from DB |
|
32
|
|
|
* Multiple calls to this method will merge all given fields |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $fields list of fields to select |
|
35
|
|
|
* @return CriteriaInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
public function select(array $fields = null) |
|
38
|
|
|
{ |
|
39
|
|
|
if ($fields !== null) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->setSelect(array_merge($this->_select, $fields)); |
|
42
|
|
|
} |
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Return selected fields |
|
48
|
|
|
* @return bool[] Fields used for select |
|
49
|
|
|
* @since v1.3.1 |
|
50
|
|
|
*/ |
|
51
|
102 |
|
public function getSelect() |
|
52
|
|
|
{ |
|
53
|
102 |
|
return $this->_select; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Set fields to select. |
|
58
|
|
|
* Pass array with field names as keys and true as value, ie: |
|
59
|
|
|
* ```php |
|
60
|
|
|
* $criteria->setSelect(['_id' => true, 'title' => true]); |
|
61
|
|
|
* ``` |
|
62
|
|
|
* |
|
63
|
|
|
* NOTE: This resets entire select |
|
64
|
|
|
* |
|
65
|
|
|
* @param bool[] $select Fields to select |
|
66
|
|
|
* @return CriteriaInterface |
|
67
|
|
|
*/ |
|
68
|
4 |
|
public function setSelect(array $select) |
|
69
|
|
|
{ |
|
70
|
4 |
|
$this->_select = []; |
|
71
|
|
|
// Convert the select array to field=>true/false format |
|
72
|
4 |
|
foreach ($select as $key => $value) |
|
73
|
|
|
{ |
|
74
|
4 |
|
if (is_int($key)) |
|
75
|
4 |
|
{ |
|
76
|
|
|
$this->_select[$value] = true; |
|
77
|
|
|
} |
|
78
|
|
|
else |
|
79
|
|
|
{ |
|
80
|
4 |
|
$this->_select[$key] = $value; |
|
81
|
|
|
} |
|
82
|
4 |
|
} |
|
83
|
4 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|