Conditions   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 106
ccs 0
cts 42
cp 0
rs 10

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __get() 0 5 1
A __set() 0 6 1
A eq() 0 5 1
A gt() 0 5 1
A gte() 0 5 1
A lt() 0 5 1
A lte() 0 5 1
A ne() 0 5 1
A in() 0 4 1
A notIn() 0 4 1
A addOr() 0 4 1
A addAnd() 0 4 1
A addNot() 0 4 1
A addNor() 0 4 1
A get() 0 4 1
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\Criteria;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\Criteria;
18
19
/**
20
 * Conditions builder helper class. This exposes fluid interface for building complex queries.
21
 *
22
 * Building complex queries can lead to (semi) manually constructing deeply nested array of conditions,
23
 * which is both unreadable and unreliable, espacially when user with `$or`, `$and`, `$not` etc.
24
 *
25
 * This class is meant to overcome this issue, by providing fluid, cascading interface.
26
 *
27
 * Example for simple query, let's find `visits` larger than 30 and lesser than 100:
28
 * 
29
 * ```php
30
 * $conditions = new Conditions($model)
31
 * $conditions->visits->gt(30)->lt(100);
32
 * ```
33
 *
34
 * However real improvement comes when used with more complex query. In this example we
35
 * search for `visits` greater than 10 and lesser than 200 or greater than 100 and
36
 * lesser than 200, where `active` is true:
37
 *
38
 * ```php
39
 * $c1 = new Conditions($model);
40
 * $c1->visits->gt(10)->lt(20);
41
 *
42
 * $c2->visits->gt(100)->lt(200);
43
 *
44
 * $condtions = new Conditions($model);
45
 * $conditions->active = true;
46
 *
47
 * $conditions->or($c1, $c2);
48
 * ```
49
 *
50
 * In above example mongodb conditions array is quite verbose and depth, however
51
 * with `Conditions` class it is fairly clean and easy to create it.
52
 *
53
 * When conditions setup is finished, this should be passed to `Criteria` by `setCriteria`:
54
 *
55
 * ```php
56
 * $criteria = new Criteria();
57
 * $criteria->setconditions($conditions);
58
 * ```
59
 *
60
 * Or alternatively it can be passed directly into constructor:
61
 * ```
62
 * $criteria = new Criteria($conditions);
63
 * ```
64
 *
65
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
66
 */
67
class Conditions
68
{
69
70
	const FieldName = 1;
71
	const Operator = 2;
72
	const Value = 3;
73
74
	private $model = null;
75
	private $criteria = null;
76
	private $field = '';
77
78
	public function __construct(AnnotatedInterface $model)
79
	{
80
		$this->criteria = new Criteria(null, $model);
81
	}
82
83
	public function __get($name)
84
	{
85
		$this->field = $name;
86
		return $this;
87
	}
88
89
	public function __set($name, $value)
90
	{
91
		$this->field = $name;
92
		$this->eq($value);
93
		return $this;
94
	}
95
96
	public function eq($value)
97
	{
98
		$this->criteria->addCond($this->field, 'eq', $value);
99
		return $this;
100
	}
101
102
	public function gt($value)
103
	{
104
		$this->criteria->addCond($this->field, 'gt', $value);
105
		return $this;
106
	}
107
108
	public function gte($value)
109
	{
110
		$this->criteria->addCond($this->field, 'gte', $value);
111
		return $this;
112
	}
113
114
	public function lt($value)
115
	{
116
		$this->criteria->addCond($this->field, 'lt', $value);
117
		return $this;
118
	}
119
120
	public function lte($value)
121
	{
122
		$this->criteria->addCond($this->field, 'lte', $value);
123
		return $this;
124
	}
125
126
	public function ne($value)
127
	{
128
		$this->criteria->addCond($this->field, 'ne', $value);
129
		return $this;
130
	}
131
132
	public function in(array $values)
0 ignored issues
show
Unused Code introduced by
The parameter $values is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
	{
134
		return $this;
135
	}
136
137
	public function notIn(array $values)
0 ignored issues
show
Unused Code introduced by
The parameter $values is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
138
	{
139
		return $this;
140
	}
141
142
	/**
143
	 *
144
	 * @param Conditions|Conditions[] $conditions
145
	 * @return Conditions
146
	 */
147
	public function addOr($conditions)
0 ignored issues
show
Unused Code introduced by
The parameter $conditions is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
148
	{
149
		return $this;
150
	}
151
152
	public function addAnd($value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
153
	{
154
		return $this;
155
	}
156
157
	public function addNot($value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
158
	{
159
		return $this;
160
	}
161
162
	public function addNor($value)
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
163
	{
164
		return $this;
165
	}
166
167
	public function get()
168
	{
169
		return $this->criteria->getConditions();
170
	}
171
172
}
173