1 | <?php |
||
6 | class HasWhereExpression implements ExpressionInterface |
||
7 | { |
||
8 | /** @var \Wandu\Database\Query\Expression\WhereExpression */ |
||
9 | protected $where; |
||
10 | |||
11 | /** @var \Wandu\Database\Query\Expression\OrderByExpression */ |
||
12 | protected $orderBy; |
||
13 | |||
14 | /** @var \Wandu\Database\Query\Expression\LimitExpression */ |
||
15 | protected $limit; |
||
16 | |||
17 | /** |
||
18 | * @param string|array|callable $name |
||
19 | * @param string $operator |
||
20 | * @param string $value |
||
21 | * @return static |
||
22 | */ |
||
23 | 15 | public function where($name, $operator = null, $value = null) |
|
24 | { |
||
25 | 15 | if (!isset($this->where)) { |
|
26 | 15 | $this->where = new WhereExpression(); |
|
27 | } |
||
28 | 15 | $this->where->where($name, $operator, $value); |
|
29 | 15 | return $this; |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * @param string|array|callable $name |
||
34 | * @param string $operator |
||
35 | * @param string $value |
||
36 | * @return static |
||
37 | */ |
||
38 | public function andWhere($name, $operator = null, $value = null) |
||
39 | { |
||
40 | if (!isset($this->where)) { |
||
41 | $this->where = new WhereExpression(); |
||
42 | } |
||
43 | $this->where->andWhere($name, $operator, $value); |
||
44 | return $this; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @param string|array|callable $name |
||
49 | * @param string $operator |
||
50 | * @param string $value |
||
51 | * @return static |
||
52 | */ |
||
53 | 5 | public function orWhere($name, $operator = null, $value = null) |
|
54 | { |
||
55 | 5 | if (!isset($this->where)) { |
|
56 | $this->where = new WhereExpression(); |
||
57 | } |
||
58 | 5 | $this->where->orWhere($name, $operator, $value); |
|
59 | 5 | return $this; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param array|string $name |
||
64 | * @param boolean $asc |
||
65 | * @return static |
||
66 | */ |
||
67 | 7 | public function orderBy($name, $asc = true) |
|
68 | { |
||
69 | 7 | if (!isset($this->orderBy)) { |
|
70 | 7 | $this->orderBy = new OrderByExpression(); |
|
71 | } |
||
72 | 7 | $this->orderBy->orderBy($name, $asc); |
|
73 | 7 | return $this; |
|
74 | } |
||
75 | |||
76 | /** |
||
77 | * @param array|string $name |
||
78 | * @param boolean $asc |
||
79 | * @return static |
||
80 | */ |
||
81 | 1 | public function andOrderBy($name, $asc = true) |
|
82 | { |
||
83 | 1 | if (!isset($this->orderBy)) { |
|
84 | $this->orderBy = new OrderByExpression(); |
||
85 | } |
||
86 | 1 | $this->orderBy->andOrderBy($name, $asc); |
|
87 | 1 | return $this; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param int $take |
||
92 | * @return static |
||
93 | */ |
||
94 | 7 | public function take($take = null) |
|
95 | { |
||
96 | 7 | if (!isset($this->limit)) { |
|
97 | 7 | $this->limit = new LimitExpression(); |
|
98 | } |
||
99 | 7 | $this->limit->take($take); |
|
100 | 7 | return $this; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param int $offset |
||
105 | * @return static |
||
106 | */ |
||
107 | 5 | public function offset($offset = null) |
|
108 | { |
||
109 | 5 | if (!isset($this->limit)) { |
|
110 | $this->limit = new LimitExpression(); |
||
111 | } |
||
112 | 5 | $this->limit->offset($offset); |
|
113 | 5 | return $this; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param int $offset |
||
118 | * @param int $take |
||
119 | * @return static |
||
120 | */ |
||
121 | public function limit($offset, $take) |
||
122 | { |
||
123 | if (!isset($this->limit)) { |
||
124 | $this->limit = new LimitExpression(); |
||
125 | } |
||
126 | $this->limit->limit($offset, $take); |
||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | 21 | public function toSql() |
|
134 | { |
||
135 | 21 | $parts = array_reduce([ |
|
136 | 21 | $this->where, |
|
137 | 21 | $this->orderBy, |
|
138 | 21 | $this->limit, |
|
139 | 21 | ], function ($carry, $expression) { |
|
140 | /* @var \Wandu\Database\Contracts\ExpressionInterface $expression */ |
||
141 | 21 | if (isset($expression) && $sqlPart = $expression->toSql()) { |
|
142 | 19 | $carry[] = $sqlPart; |
|
143 | 19 | return $carry; |
|
144 | } |
||
145 | 13 | return $carry; |
|
146 | 21 | }, []); |
|
147 | 21 | return implode(' ', $parts); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 20 | public function getBindings() |
|
167 | } |
||
168 |