|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Adrian Dumitru |
|
5
|
|
|
* Date: 8/15/2017 |
|
6
|
|
|
* Time: 12:15 PM |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Qpdb\QueryBuilder\Traits; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use Qpdb\QueryBuilder\Dependencies\QueryException; |
|
13
|
|
|
use Qpdb\QueryBuilder\Dependencies\QueryHelper; |
|
14
|
|
|
use Qpdb\QueryBuilder\Dependencies\QueryStructure; |
|
15
|
|
|
|
|
16
|
|
|
trait SelectFields |
|
17
|
|
|
{ |
|
18
|
|
|
use Objects; |
|
19
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param string|array $fields |
|
23
|
|
|
* @return $this |
|
24
|
|
|
* @throws QueryException |
|
25
|
|
|
*/ |
|
26
|
|
|
public function fields( $fields ) |
|
27
|
|
|
{ |
|
28
|
|
|
|
|
29
|
|
|
switch ( gettype( $fields ) ) { |
|
30
|
|
|
case QueryStructure::ELEMENT_TYPE_ARRAY: |
|
31
|
|
|
|
|
32
|
|
|
$fields = $this->prepareArrayFields( $fields ); |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
if ( count( $fields ) ) |
|
35
|
|
|
$this->queryStructure->setElement( QueryStructure::FIELDS, implode( ', ', $fields ) ); |
|
36
|
|
|
else |
|
37
|
|
|
$this->queryStructure->setElement( QueryStructure::FIELDS, '*' ); |
|
38
|
|
|
break; |
|
39
|
|
|
|
|
40
|
|
|
case QueryStructure::ELEMENT_TYPE_STRING: |
|
41
|
|
|
|
|
42
|
|
|
$fields = trim( $fields ); |
|
|
|
|
|
|
43
|
|
|
if ( '' !== $fields ) { |
|
44
|
|
|
$fields = explode( ',', $fields ); |
|
45
|
|
|
$fields = $this->prepareArrayFields( $fields ); |
|
46
|
|
|
$this->queryStructure->setElement( QueryStructure::FIELDS, implode( ', ', $fields ) ); |
|
47
|
|
|
} |
|
48
|
|
|
else |
|
49
|
|
|
$this->queryStructure->setElement( QueryStructure::FIELDS, '*' ); |
|
50
|
|
|
break; |
|
51
|
|
|
|
|
52
|
|
|
default: |
|
53
|
|
|
throw new QueryException( 'Invalid fields parameter type', QueryException::QUERY_ERROR_WHERE_INVALID_PARAM_ARRAY ); |
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function fieldsByExpression( $expression ) |
|
61
|
|
|
{ |
|
62
|
|
|
$expression = trim( $expression ); |
|
63
|
|
|
$this->queryStructure->setElement( QueryStructure::FIELDS, $expression ); |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param array $fieldsArray |
|
72
|
|
|
* @return array |
|
73
|
|
|
* @throws QueryException |
|
74
|
|
|
*/ |
|
75
|
|
|
private function prepareArrayFields( $fieldsArray = array() ) |
|
76
|
|
|
{ |
|
77
|
|
|
$prepareArray = []; |
|
78
|
|
|
|
|
79
|
|
|
foreach ( $fieldsArray as $field ) { |
|
80
|
|
|
|
|
81
|
|
|
switch ( gettype( $field ) ) { |
|
82
|
|
|
case QueryStructure::ELEMENT_TYPE_STRING: |
|
83
|
|
|
$prepareArray[] = $this->queryStructure->prepare( $field ); |
|
84
|
|
|
break; |
|
85
|
|
|
case QueryStructure::ELEMENT_TYPE_ARRAY: |
|
86
|
|
|
$prepareArray[] = $this->getFieldByArray( $field ); |
|
87
|
|
|
break; |
|
88
|
|
|
default: |
|
89
|
|
|
throw new QueryException('Invalid field description'); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $prepareArray; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param array $fieldArray |
|
99
|
|
|
* @return string |
|
100
|
|
|
* @throws QueryException |
|
101
|
|
|
*/ |
|
102
|
|
|
private function getFieldByArray( array $fieldArray ) |
|
103
|
|
|
{ |
|
104
|
|
|
|
|
105
|
|
|
if ( !in_array( count( $fieldArray ), [ 1, 2 ] ) ) { |
|
106
|
|
|
throw new QueryException( 'Invalid descriptive array from field.' ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ( count( $fieldArray ) === 1 ) { |
|
110
|
|
|
return $this->queryStructure->prepare( trim( $fieldArray[ 0 ] ) ); |
|
111
|
|
|
} |
|
112
|
|
|
else { |
|
113
|
|
|
return $this->queryStructure->prepare( trim( $fieldArray[ 0 ] ) ) . ' ' . $this->queryStructure->prepare( trim( $fieldArray[ 1 ] ) ); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
} |