-
Notifications
You must be signed in to change notification settings - Fork 0
/
Query.php
100 lines (76 loc) · 2.13 KB
/
Query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Expresser\Support;
use InvalidArgumentException;
use Expresser\Contracts\Support\Queryable;
abstract class Query implements Queryable
{
protected $query;
public function __construct($query)
{
$this->setQuery($query);
}
public function getQuery()
{
return $this->query;
}
public function setQuery($query)
{
if (!property_exists($query, 'query_vars')) {
throw new InvalidArgumentException('$query not a valid type of WordPress Query');
}
$this->query = $query;
if (!is_array($this->query->query_vars)) {
$this->query->query_vars = [];
}
return $this;
}
public function getQueryVar($name)
{
$value = $this->getQueryVarValue($name);
if ($this->hasGetMutator($name)) {
$value = $this->mutateQueryVar($name, $value);
}
return $value;
}
public function getQueryVarValue($name)
{
if (isset($this->query->query_vars[$name])) {
return $this->query->query_vars[$name];
}
}
public function hasGetMutator($name)
{
return method_exists($this, 'get'.studly_case($name).'QueryVar');
}
public function mutateQueryVar($name, $value)
{
$value = $this->{'get'.studly_case($name).'QueryVar'}($value);
return $value;
}
public function setQueryVar($name, $value)
{
if ($this->hasSetMutator($name)) {
$this->{'set'.studly_case($name).'QueryVar'}($value);
} else {
$this->query->query_vars[$name] = $value;
}
return $this;
}
public function hasSetMutator($name)
{
return method_exists($this, 'set'.studly_case($name).'QueryVar');
}
public function hasQueryVar($name)
{
$queryVar = $this->getQueryVar($name);
return !(is_null($queryVar) || empty($queryVar));
}
public function removeQueryVar($name)
{
if (isset($this->query->query_vars[$name])) {
unset($this->query->query_vars[$name]);
}
return $this;
}
abstract public function execute();
}