|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created for IG Client. |
|
4
|
|
|
* User: jakim <[email protected]> |
|
5
|
|
|
* Date: 16.03.2018 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Jakim\Query; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
use Jakim\Base\Query; |
|
12
|
|
|
use jakim\ig\Endpoint; |
|
13
|
|
|
use Jakim\Mapper\ExploreTags; |
|
14
|
|
|
use Jakim\Model\Post; |
|
15
|
|
|
use Jakim\Model\Tag; |
|
16
|
|
|
|
|
17
|
|
|
class TagQuery extends Query |
|
18
|
|
|
{ |
|
19
|
|
|
protected $exploreTagsMapper; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct($httpClient, ExploreTags $exploreTagsMapper = null) |
|
22
|
|
|
{ |
|
23
|
|
|
parent::__construct($httpClient); |
|
24
|
|
|
$this->exploreTagsMapper = $exploreTagsMapper ?? new ExploreTags(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function findOne(string $tagName): Tag |
|
28
|
|
|
{ |
|
29
|
|
|
$data = $this->fetchData($tagName); |
|
30
|
|
|
$data = $this->exploreTagsMapper->normalizeData(Tag::class, $data); |
|
31
|
|
|
|
|
32
|
|
|
return $this->exploreTagsMapper->populate(Tag::class, $data); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function findTopPosts(string $tagName, bool $relations = false) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->exploreTagsMapper->postsEnvelope = ExploreTags::TOP_POSTS_ENVELOPE; |
|
38
|
|
|
|
|
39
|
|
|
$data = $this->fetchData($tagName); |
|
40
|
|
|
$items = $this->exploreTagsMapper->normalizeData(Post::class, $data); |
|
41
|
|
|
foreach ($items as $item) { |
|
42
|
|
|
yield $this->exploreTagsMapper->populate(Post::class, $item, $relations); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function findMedia(string $tagName, bool $relations = false) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->exploreTagsMapper->postsEnvelope = ExploreTags::MEDIA_ENVELOPE; |
|
49
|
|
|
|
|
50
|
|
|
$data = $this->fetchData($tagName); |
|
51
|
|
|
$items = $this->exploreTagsMapper->normalizeData(Post::class, $data); |
|
52
|
|
|
foreach ($items as $item) { |
|
53
|
|
|
yield $this->exploreTagsMapper->populate(Post::class, $item, $relations); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $tagName |
|
59
|
|
|
* @return array|null |
|
60
|
|
|
* @throws \Jakim\Exception\EmptyContentException |
|
61
|
|
|
*/ |
|
62
|
|
|
private function fetchData(string $tagName) |
|
63
|
|
|
{ |
|
64
|
|
|
$url = Endpoint::exploreTags($tagName); |
|
65
|
|
|
$data = $this->fetchContentAsArray($url); |
|
66
|
|
|
|
|
67
|
|
|
$this->throwEmptyContentExceptionIfEmpty($data); |
|
68
|
|
|
|
|
69
|
|
|
return $data; |
|
70
|
|
|
} |
|
71
|
|
|
} |