Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 15fc0c8

Browse files
committed
Merge branch 'feature-foundation-filters' into develop
2 parents 05f9575 + bee0ed0 commit 15fc0c8

14 files changed

+1318
-130
lines changed

src/AccessibleMutatableTrait.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php namespace Quince\Pelastic;
2+
3+
use Quince\Pelastic\Exceptions\PelasticLogicException;
4+
5+
trait AccessibleMutatableTrait {
6+
7+
/**
8+
* @var array
9+
*/
10+
protected $optionAttribute = [];
11+
12+
/**
13+
* Gets the attribute from options array
14+
*
15+
* @param string $attributeName
16+
* @param bool $hardCheck
17+
* @param null $defaultValue
18+
* @return mixed
19+
* @throws PelasticLogicException
20+
*/
21+
public function getAttribute($attributeName, $hardCheck = false, $defaultValue = null)
22+
{
23+
$attributes = $this->getOptionAttribute();
24+
25+
if (isset($attributes[$attributeName])) {
26+
27+
return $attributes[$attributeName];
28+
29+
} elseif ($hardCheck) {
30+
31+
throw new PelasticLogicException("You should have set a value for {$attributeName}.");
32+
33+
}
34+
35+
return $defaultValue;
36+
}
37+
38+
/**
39+
* Set attribute on options array
40+
*
41+
* @param string $attributeName
42+
* @param mixed $value
43+
* @return $this
44+
*/
45+
public function setAttribute($attributeName, $value)
46+
{
47+
$this->optionAttribute[$attributeName] = $value;
48+
49+
return $this;
50+
}
51+
52+
/**
53+
* Whether a offset exists
54+
*
55+
* @param mixed $offset
56+
* @return boolean
57+
*/
58+
public function offsetExists($offset)
59+
{
60+
return isset($this->getOptionAttribute()[$offset]);
61+
}
62+
63+
/**
64+
* Offset to retrieve
65+
*
66+
* @param mixed $offset
67+
* @return mixed
68+
*/
69+
public function offsetGet($offset)
70+
{
71+
return $this->getAttribute($offset, false, null);
72+
}
73+
74+
/**
75+
* Offset to set
76+
*
77+
* @param mixed $offset
78+
* @param mixed $value
79+
* @return void
80+
*/
81+
public function offsetSet($offset, $value)
82+
{
83+
$this->setAttribute($offset, $value);
84+
}
85+
86+
/**
87+
* Offset to unset
88+
*
89+
* @param mixed $offset
90+
* @return void
91+
*/
92+
public function offsetUnset($offset)
93+
{
94+
unset($this->optionAttribute[$offset]);
95+
}
96+
97+
/**
98+
* Getting option attribute
99+
*
100+
* @return array
101+
*/
102+
protected function getOptionAttribute()
103+
{
104+
return $this->optionAttribute;
105+
}
106+
107+
/**
108+
* Put an item into an array field
109+
* if the array does not exists it will be created
110+
* and the new value is pushed to it, other ways it will be added
111+
* to the old one
112+
*
113+
* @param string $field
114+
* @param mixed $what
115+
* @return array
116+
*/
117+
protected function putIntoArrayField($field, $what)
118+
{
119+
try {
120+
121+
$collection = (array) $this->getAttribute($field, true);
122+
123+
$collection = array_push($collection, $what);
124+
125+
$this->setAttribute($field, $collection);
126+
127+
} catch (PelasticLogicException $e) {
128+
// In case array has not been created yet
129+
$this->setAttribute($field, $collection = [$what]);
130+
131+
}
132+
133+
return $collection;
134+
}
135+
136+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
<?php namespace Quince\Pelastic\Contracts\Filters;
2+
3+
interface BooleanFilterInterface extends FilterInterface {
4+
5+
/**
6+
* The filter should match the given query in a boolean or way
7+
*
8+
* @param FilterInterface $filter
9+
* @return $this
10+
*/
11+
public function should(FilterInterface $filter);
12+
13+
/**
14+
* The result must have the given criteria
15+
*
16+
* @param FilterInterface $filter
17+
* @return $this
18+
*/
19+
public function must(FilterInterface $filter);
20+
21+
/**
22+
* A collection of shoulds
23+
*
24+
* @param $filters
25+
* @return $this
26+
*/
27+
public function shoulds($filters);
28+
29+
/**
30+
* A collection of musts
31+
*
32+
* @param $filters
33+
* @return $this
34+
*/
35+
public function musts($filters);
36+
37+
/**
38+
* Collection of must nots
39+
*
40+
* @param $filters
41+
* @return $this
42+
*/
43+
public function mustNots($filters);
44+
45+
/**
46+
* The negative condition of must filter
47+
*
48+
* @param FilterInterface $filter
49+
* @return $this
50+
*/
51+
public function mustNot(FilterInterface $filter);
52+
53+
/**
54+
* A proxy to "should"
55+
*
56+
* @param FilterInterface $filter
57+
* @return $this
58+
*/
59+
public function addOr(FilterInterface $filter);
60+
61+
/**
62+
* A proxy to "must"
63+
*
64+
* @param FilterInterface $filter
65+
* @return $this
66+
*/
67+
public function addAnd(FilterInterface $filter);
68+
69+
/**
70+
* Bulk shoulds
71+
*
72+
* @param $filters
73+
* @return $this
74+
*/
75+
public function ors($filters);
76+
77+
/**
78+
* Bulk musts
79+
*
80+
* @param $filters
81+
* @return $this
82+
*/
83+
public function ands($filters);
84+
85+
/**
86+
* A human readable proxy to should
87+
*
88+
* @param FilterInterface $filter
89+
* @return $this
90+
*/
91+
public function iLike(FilterInterface $filter);
92+
93+
/**
94+
* A human readable proxy to must
95+
*
96+
* @param FilterInterface $filter
97+
* @return $this
98+
*/
99+
public function iDontLike(FilterInterface $filter);
100+
101+
/**
102+
* A human readable proxy to must
103+
*
104+
* @param FilterInterface $filter
105+
* @return $this
106+
*/
107+
public function iReallyNeed(FilterInterface $filter);
108+
109+
/**
110+
* Get musts array
111+
*
112+
* @return array
113+
*/
114+
public function getMusts();
115+
116+
/**
117+
* Get shoulds array
118+
*
119+
* @return array
120+
*/
121+
public function getShoulds();
122+
123+
/**
124+
* Get must nots array
125+
*
126+
* @return array
127+
*/
128+
public function getMustNots();
129+
130+
/**
131+
* Remove all shoulds
132+
*
133+
* @return $this
134+
*/
135+
public function removeShoulds();
136+
137+
/**
138+
* Remove all musts
139+
*
140+
* @return $this
141+
*/
142+
public function removeMusts();
143+
144+
/**
145+
* Remove all must not queries
146+
*
147+
* @return $this
148+
*/
149+
public function removeMustNots();
150+
151+
/**
152+
* Set cache
153+
*
154+
* @param (bool) $bool
155+
* @return $this
156+
*/
157+
public function setCache($bool);
158+
159+
/**
160+
* Cache status
161+
*
162+
* @return bool
163+
*/
164+
public function getCacheStatus();
165+
166+
/**
167+
* Enable cache on query
168+
*
169+
* @return $this
170+
*/
171+
public function enableCache();
172+
173+
/**
174+
* Disable cache
175+
*
176+
* @return $this
177+
*/
178+
public function disableCache();
179+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php namespace Quince\Pelastic\Contracts\Filters;
2+
3+
interface FieldFilterableInterface {
4+
5+
/**
6+
* Sets the field to perform the filter
7+
*
8+
* @param string $field
9+
* @return $this
10+
*/
11+
public function setField($field);
12+
13+
/**
14+
* Sets the value to perform the filter
15+
*
16+
* @param string $value
17+
* @return $this
18+
*/
19+
public function setValue($value);
20+
21+
/**
22+
* Sets both field and value on one place
23+
*
24+
* @param string $field
25+
* @param string $value
26+
* @return $this
27+
*/
28+
public function setFieldWithValue($field, $value);
29+
30+
/**
31+
* Get value of the filter
32+
*
33+
* @param bool $hardCheck
34+
* @return null|string
35+
*/
36+
public function getValue($hardCheck = false);
37+
38+
/**
39+
* Get field of the filter
40+
*
41+
* @param bool $hardCheck
42+
* @return null|string
43+
*/
44+
public function getField($hardCheck = false);
45+
46+
}

src/Contracts/Filters/FilterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
use Quince\Pelastic\Contracts\ArrayableInterface;
44
use Quince\Pelastic\Contracts\AccessorMutatorInterface;
55

6-
interface FilterInterface extends ArrayableInterface, AccessorMutatorInterface{
6+
interface FilterInterface extends ArrayableInterface, AccessorMutatorInterface {
77

88
}

0 commit comments

Comments
 (0)