1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace smtech\StMarksSearch\WordPress; |
4
|
|
|
|
5
|
|
|
use smtech\StMarksSearch\AbstractSearchDomain; |
6
|
|
|
use smtech\StMarksSearch\SearchResult; |
7
|
|
|
use smtech\StMarksSearch\SearchSource; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A parent object for WordPress search domains |
11
|
|
|
* |
12
|
|
|
* @author Seth Battis <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
abstract class AbstractWordPressSearchDomain extends AbstractSearchDomain |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* API access object |
18
|
|
|
* @var WordPressPest |
19
|
|
|
*/ |
20
|
|
|
protected $api; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Summary information about search domain for search results |
24
|
|
|
* @var SearchSource |
25
|
|
|
*/ |
26
|
|
|
protected $source; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Construct a WordPress search domain: `$params` must contain a `url` |
30
|
|
|
* field with a valid URL to a WordPress blog |
31
|
|
|
* |
32
|
|
|
* @param mixed[] $params |
33
|
|
|
*/ |
34
|
|
|
public function __construct($params) |
35
|
|
|
{ |
36
|
|
|
parent::__construct($params); |
37
|
|
|
|
38
|
|
|
$this->setApi($this->getUrl()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Update the API access object |
43
|
|
|
* |
44
|
|
|
* @used-by AbstractWordPressSearchDomain::__construct() |
45
|
|
|
* @param string $url |
46
|
|
|
*/ |
47
|
|
|
protected function setApi($url) |
48
|
|
|
{ |
49
|
|
|
$this->api = new WordPressPest($url); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the API access object |
54
|
|
|
* |
55
|
|
|
* @return WordPressPest |
56
|
|
|
*/ |
57
|
|
|
public function getApi() |
58
|
|
|
{ |
59
|
|
|
return $this->api; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Hook to convert individual items returned by the WordPress API into |
64
|
|
|
* SearchResults |
65
|
|
|
* |
66
|
|
|
* @used-by AbstractWordPressSearchDomain::processResponse() |
67
|
|
|
* |
68
|
|
|
* @param array $item JSON-decoded associative array representing a |
69
|
|
|
* WordPress object |
70
|
|
|
* @param string $query |
71
|
|
|
* @return SearchResult |
72
|
|
|
*/ |
73
|
|
|
abstract protected function processItem($item, $query); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Process the a response from the WordPress API listing objects |
77
|
|
|
* |
78
|
|
|
* @uses AbstractWordPressSearchDomain::processItem() |
79
|
|
|
* |
80
|
|
|
* @param string $response JSON-formatted WordPress api response |
81
|
|
|
* @param string $query |
82
|
|
|
* @return SearchResult[] Unordered |
83
|
|
|
*/ |
84
|
|
|
protected function processResponse($response, $query) |
85
|
|
|
{ |
86
|
|
|
$results = []; |
87
|
|
|
do { |
88
|
|
|
$items = $response; |
89
|
|
|
if (is_array($items)) { |
90
|
|
|
foreach ($items as $item) { |
91
|
|
|
$results[] = $this->processItem($item, $query); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$link = $this->api->lastHeader('Link'); |
96
|
|
|
if (preg_match('@<' . $this->api->base_url . '([^>]*)?(.*)>; rel="next"@', $link, $match)) { |
97
|
|
|
parse_str($match[2], $params); |
98
|
|
|
$response = $this->api->get($match[1], $params); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
} while (!empty($match)); |
101
|
|
|
|
102
|
|
|
return $results; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.