Skip to content

Commit ae26ff5

Browse files
committed
Where search accepts now almost everything
1 parent 10f5842 commit ae26ff5

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
1919
- Custom UID alternative support added
2020
- Fetch additional extensions using `Folder::query(["FEATURE_NAME"])`
2121
- Optionally move a message during "deletion" instead of just "flagging" it #106 (thanks @EthraZa)
22+
- `WhereQuery::where()` accepts now a wide range of criteria / values.
2223

2324
### Affected Classes
2425
- [Header::class](src/Header.php)
2526
- [Protocol::class](src/Connection/Protocols/Protocol.php)
2627
- [Query::class](src/Query/Query.php)
28+
- [WhereQuery::class](src/Query/WhereQuery.php)
2729

2830
### Breaking changes
2931
- All protocol methods which had a `boolean` `$uid` option no longer support a boolean. Use `IMAP::ST_UID` or `IMAP::NIL` instead. If you want to use an alternative to `UID` just use the string instead.

src/Query/WhereQuery.php

+40-23
Original file line numberDiff line numberDiff line change
@@ -117,34 +117,55 @@ protected function validate_criteria($criteria) {
117117
}
118118

119119
/**
120+
* Register search parameters
120121
* @param mixed $criteria
121122
* @param null $value
122123
*
123124
* @return $this
124125
* @throws InvalidWhereQueryCriteriaException
126+
*
127+
* Examples:
128+
* $query->from("[email protected]")->seen();
129+
* $query->whereFrom("[email protected]")->whereSeen();
130+
* $query->where([["FROM" => "[email protected]"], ["SEEN"]]);
131+
* $query->where(["FROM" => "[email protected]"])->where(["SEEN"]);
132+
* $query->where(["FROM" => "[email protected]", "SEEN"]);
133+
* $query->where("FROM", "[email protected]")->where("SEEN");
125134
*/
126135
public function where($criteria, $value = null) {
127136
if (is_array($criteria)) {
128137
foreach ($criteria as $key => $value) {
129138
if (is_numeric($key)) {
130-
return $this->where($value);
139+
$this->where($value);
140+
}else{
141+
$this->where($key, $value);
131142
}
132-
return $this->where($key, $value);
133143
}
134144
} else {
135-
$criteria = $this->validate_criteria($criteria);
136-
$value = $this->parse_value($value);
137-
138-
if ($value === null || $value === '') {
139-
$this->query->push([$criteria]);
140-
} else {
141-
$this->query->push([$criteria, $value]);
142-
}
145+
$this->push_search_criteria($criteria, $value);
143146
}
144147

145148
return $this;
146149
}
147150

151+
/**
152+
* Push a given search criteria and value pair to the search query
153+
* @param $criteria string
154+
* @param $value mixed
155+
*
156+
* @throws InvalidWhereQueryCriteriaException
157+
*/
158+
protected function push_search_criteria($criteria, $value){
159+
$criteria = $this->validate_criteria($criteria);
160+
$value = $this->parse_value($value);
161+
162+
if ($value === null || $value === '') {
163+
$this->query->push([$criteria]);
164+
} else {
165+
$this->query->push([$criteria, $value]);
166+
}
167+
}
168+
148169
/**
149170
* @param Closure $closure
150171
*
@@ -468,8 +489,7 @@ public function whereLanguage($country_code) {
468489
* @return WhereQuery
469490
* @throws InvalidWhereQueryCriteriaException
470491
*/
471-
public function whereUid($uid)
472-
{
492+
public function whereUid($uid) {
473493
return $this->where('UID', $uid);
474494
}
475495

@@ -481,8 +501,7 @@ public function whereUid($uid)
481501
* @return WhereQuery
482502
* @throws InvalidWhereQueryCriteriaException
483503
*/
484-
public function whereUidIn($uids)
485-
{
504+
public function whereUidIn($uids) {
486505
$uids = implode(',', $uids);
487506
return $this->where('UID', $uids);
488507
}
@@ -491,10 +510,9 @@ public function whereUidIn($uids)
491510
* Apply the callback if the given "value" is truthy.
492511
* copied from @url https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/Traits/Conditionable.php
493512
*
494-
* @param mixed $value
495-
* @param callable $callback
496-
* @param callable|null $default
497-
513+
* @param mixed $value
514+
* @param callable $callback
515+
* @param callable|null $default
498516
* @return $this|mixed
499517
*/
500518
public function when($value, $callback, $default = null) {
@@ -511,14 +529,13 @@ public function when($value, $callback, $default = null) {
511529
* Apply the callback if the given "value" is falsy.
512530
* copied from @url https://github.com/laravel/framework/blob/8.x/src/Illuminate/Support/Traits/Conditionable.php
513531
*
514-
* @param mixed $value
515-
* @param callable $callback
516-
* @param callable|null $default
517-
532+
* @param mixed $value
533+
* @param callable $callback
534+
* @param callable|null $default
518535
* @return $this|mixed
519536
*/
520537
public function unless($value, $callback, $default = null) {
521-
if (! $value) {
538+
if (!$value) {
522539
return $callback($this, $value) ?: $this;
523540
} elseif ($default) {
524541
return $default($this, $value) ?: $this;

0 commit comments

Comments
 (0)