1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq; |
4
|
|
|
|
5
|
|
|
use Pinq\Expressions as O; |
6
|
|
|
use Pinq\Iterators\IIteratorScheme; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* The standard repository class, fully implements the repository API |
10
|
|
|
* |
11
|
|
|
* @author Elliot Levin <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class Repository extends Queryable implements IRepository, Interfaces\IOrderedRepository |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The repository provider for the current instance |
17
|
|
|
* |
18
|
|
|
* @var Providers\IRepositoryProvider |
19
|
|
|
*/ |
20
|
|
|
protected $repositoryProvider; |
21
|
|
|
|
22
|
|
|
public function __construct( |
23
|
|
|
Providers\IRepositoryProvider $provider, |
24
|
|
|
Queries\ISourceInfo $sourceInfo, |
25
|
|
|
O\TraversalExpression $queryExpression = null, |
26
|
|
|
IIteratorScheme $scheme = null |
27
|
|
|
) { |
28
|
|
|
parent::__construct($provider, $sourceInfo, $queryExpression, $scheme); |
29
|
|
|
|
30
|
|
|
$this->repositoryProvider = $provider; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Executes the supplied operation query expression on the underlying repository provider. |
35
|
|
|
* |
36
|
|
|
* @param O\Expression $expression |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
private function executeQuery(O\Expression $expression) |
41
|
|
|
{ |
42
|
|
|
$this->repositoryProvider->execute($expression); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
* @return IRepository |
48
|
|
|
*/ |
49
|
|
|
protected function newMethodSegment($name, array $arguments = []) |
50
|
|
|
{ |
51
|
|
|
return $this->repositoryProvider->createRepository($this->newMethod($name, $arguments)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function join($values) |
55
|
|
|
{ |
56
|
|
|
return new Connectors\JoiningRepository( |
57
|
|
|
$this->repositoryProvider, |
58
|
|
|
$this->newMethod(__FUNCTION__, [$values])); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function groupJoin($values) |
62
|
|
|
{ |
63
|
|
|
return new Connectors\JoiningRepository( |
64
|
|
|
$this->repositoryProvider, |
65
|
|
|
$this->newMethod(__FUNCTION__, [$values])); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function addRange($values) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
if (!Utilities::isIterable($values)) { |
71
|
|
|
throw PinqException::invalidIterable(__METHOD__, $values); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->executeQuery($this->newMethod(__FUNCTION__, [$values])); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function apply(callable $function) |
78
|
|
|
{ |
79
|
|
|
$this->executeQuery($this->newMethod(__FUNCTION__, [$function])); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function remove($value) |
83
|
|
|
{ |
84
|
|
|
$this->executeQuery($this->newMethod(__FUNCTION__, [$value])); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
public function removeRange($values) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
if (!Utilities::isIterable($values)) { |
90
|
|
|
throw PinqException::invalidIterable(__METHOD__, $values); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->executeQuery($this->newMethod(__FUNCTION__, [$values])); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function removeWhere(callable $predicate) |
97
|
|
|
{ |
98
|
|
|
$this->executeQuery($this->newMethod(__FUNCTION__, [$predicate])); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function clear() |
102
|
|
|
{ |
103
|
|
|
$this->executeQuery($this->newMethod(__FUNCTION__)); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function offsetSet($index, $value) |
107
|
|
|
{ |
108
|
|
|
$this->executeQuery($this->newMethod(__FUNCTION__, [$index, $value])); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function offsetUnset($index) |
112
|
|
|
{ |
113
|
|
|
$this->executeQuery($this->newMethod(__FUNCTION__, [$index])); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.