1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rougin\Windstorm; |
4
|
|
|
|
5
|
|
|
use Rougin\Windstorm\QueryInterface; |
6
|
|
|
use Rougin\Windstorm\ResultInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Query Repository |
10
|
|
|
* |
11
|
|
|
* @package Windstorm |
12
|
|
|
* @author Rougin Gutib <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class QueryRepository |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var \Rougin\Windstorm\ResultInterface |
18
|
|
|
*/ |
19
|
|
|
protected $result; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var \Rougin\Windstorm\MappingInterface |
23
|
|
|
*/ |
24
|
|
|
protected $mapping; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \Rougin\Windstorm\QueryInterface |
28
|
|
|
*/ |
29
|
|
|
protected $original; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var \Rougin\Windstorm\QueryInterface |
33
|
|
|
*/ |
34
|
|
|
protected $query; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Initializes the repository instance. |
38
|
|
|
* |
39
|
|
|
* @param \Rougin\Windstorm\QueryInterface $query |
40
|
|
|
* @param \Rougin\Windstorm\ResultInterface $result |
41
|
|
|
*/ |
42
|
51 |
|
public function __construct(QueryInterface $query, ResultInterface $result) |
43
|
|
|
{ |
44
|
51 |
|
$this->result = $result; |
45
|
|
|
|
46
|
51 |
|
$this->query = $query; |
47
|
|
|
|
48
|
51 |
|
$this->original = $query; |
49
|
51 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns a number of affected rows. |
53
|
|
|
* |
54
|
|
|
* @return integer |
55
|
|
|
*/ |
56
|
21 |
|
public function affected() |
57
|
|
|
{ |
58
|
21 |
|
return $this->execute()->affected(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the first row from result. |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
6 |
|
public function first() |
67
|
|
|
{ |
68
|
6 |
|
$item = $this->execute()->first(); |
69
|
|
|
|
70
|
6 |
|
return $this->mapping->map($item); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns all items from the result. |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
21 |
|
public function items() |
79
|
|
|
{ |
80
|
21 |
|
$result = $this->execute()->items(); |
81
|
|
|
|
82
|
21 |
|
if ($this->mapping === null) |
83
|
14 |
|
{ |
84
|
6 |
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
15 |
|
foreach ($result as $key => $item) |
88
|
|
|
{ |
89
|
15 |
|
$result[$key] = $this->mapping->map($item); |
90
|
10 |
|
} |
91
|
|
|
|
92
|
15 |
|
return $result; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Sets the mapping instance. |
97
|
|
|
* |
98
|
|
|
* @param \Rougin\Windstorm\MappingInterface $mapping |
99
|
|
|
* @return self |
100
|
|
|
*/ |
101
|
36 |
|
public function mapping(MappingInterface $mapping) |
102
|
|
|
{ |
103
|
36 |
|
$this->mapping = $mapping; |
104
|
|
|
|
105
|
36 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Mutates the specified query instance. |
110
|
|
|
* |
111
|
|
|
* @param \Rougin\Windstorm\MutatorInterface $mutator |
112
|
|
|
* @return self |
113
|
|
|
*/ |
114
|
48 |
|
public function mutate(MutatorInterface $mutator) |
115
|
|
|
{ |
116
|
48 |
|
$this->query = $mutator->set($this->query); |
117
|
|
|
|
118
|
48 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Executes the result against query instance. |
123
|
|
|
* |
124
|
|
|
* @return \Rougin\Windstorm\ResultInterface |
125
|
|
|
*/ |
126
|
48 |
|
protected function execute() |
127
|
|
|
{ |
128
|
48 |
|
return $this->result->execute($this->query); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|