1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Maciej Paprocki for Bureau-VA. |
4
|
|
|
* Date: 17/02/2016 |
5
|
|
|
* Project Name: MaciekPaprocki\WordpressGuzzle |
6
|
|
|
* Time: 10:46. |
7
|
|
|
*/ |
8
|
|
|
namespace BureauVa\WordpressGuzzle\Query; |
9
|
|
|
|
10
|
|
|
use BureauVa\WordpressGuzzle\Helper\StringHelper as S; |
11
|
|
|
use Cache\Adapter\Common\AbstractCachePool as CachePool; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class AbstractRepository. |
15
|
|
|
*/ |
16
|
|
|
abstract class RepositoryAbstract |
17
|
|
|
{ |
18
|
|
|
public static $BASE_PATH = 'posts'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
4 |
|
public function __toString() |
24
|
|
|
{ |
25
|
4 |
|
$ar = (array) $this; |
26
|
|
|
|
27
|
4 |
|
return $this->prepareCall($ar); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param $ar |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
4 |
|
private function prepareCall($ar) |
36
|
|
|
{ |
37
|
4 |
|
if (count($ar) === 1 && isset($ar['post__in']) && is_numeric($ar['post__in'])) { |
38
|
4 |
|
return self::$BASE_PATH.'/'.$ar['post__in']; |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
return self::$BASE_PATH.'?'.http_build_query([ |
42
|
4 |
|
'filter' => $ar, |
43
|
2 |
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param $method |
48
|
|
|
* @param $params |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
2 |
|
public function __call($method, $params) |
53
|
|
|
{ |
54
|
2 |
|
if (substr($method, 0, 5) == 'where') { |
55
|
2 |
|
$varName = S::camelCaseToSnakeCase(mb_substr($method, 5)); |
56
|
|
|
|
57
|
2 |
|
$this->$varName = $params[0]; |
58
|
1 |
|
} |
59
|
|
|
|
60
|
2 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $id |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
4 |
|
public function whereId($id) |
69
|
|
|
{ |
70
|
4 |
|
$this->post__in = (int) $id; |
|
|
|
|
71
|
|
|
|
72
|
4 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set's. |
77
|
|
|
* |
78
|
|
|
* @param $key |
79
|
|
|
* @param $value |
80
|
|
|
*/ |
81
|
|
|
public function setNotFiltered($key, $value) |
82
|
|
|
{ |
83
|
|
|
$this->$key = $value; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
public function whereIds($ids) |
89
|
|
|
{ |
90
|
4 |
|
$this->post__in = (array) $ids; |
91
|
|
|
|
92
|
4 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Checks if there is any cache data for query. |
97
|
|
|
* |
98
|
|
|
* @param CachePool $cachePool |
|
|
|
|
99
|
|
|
*/ |
100
|
2 |
|
public function getCacheKey() |
101
|
|
|
{ |
102
|
|
|
//TODO: implement real cache check, no it uses not so smart method of caching by query address |
103
|
2 |
|
return (string) $this; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: