Filter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Wallhaven;
4
5
/**
6
 * Fluent Interface for searching
7
 *
8
 * @package Wallhaven
9
 */
10
class Filter
11
{
12
    private $wallhaven;
13
14
    private $keywords    = "";
15
    private $categories  = Category::ALL;
16
    private $purity      = Purity::SFW;
17
    private $sorting     = Sorting::RELEVANCE;
18
    private $order       = Order::DESC;
19
    private $resolutions = [];
20
    private $ratios      = [];
21
    private $pages       = 1;
22
23
    public function __construct(Wallhaven $wallhaven)
24
    {
25
        $this->wallhaven = $wallhaven;
26
    }
27
28
    /**
29
     * @param string $keywords What to search for. Can be keywords or #tagnames, e.g. #cars
30
     *
31
     * @return self
32
     */
33
    public function keywords($keywords)
34
    {
35
        $this->keywords = $keywords;
36
37
        return $this;
38
    }
39
40
    /**
41
     * @param int $categories Categories to include. This is a bit field, for example:
42
     *                        <samp>Category::GENERAL | Category::PEOPLE</samp>
43
     *
44
     * @return self
45
     */
46
    public function categories($categories)
47
    {
48
        $this->categories = $categories;
49
50
        return $this;
51
    }
52
53
    /**
54
     * @param int $purity Purity of wallpapers. This is a bit field, for example:
55
     *                    <samp>Purity::SFW | Purity::NSFW</samp>
56
     *
57
     * @return self
58
     */
59
    public function purity($purity)
60
    {
61
        $this->purity = $purity;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @param string $sorting Sorting, e.g. <samp>Sorting::RELEVANCE</samp>
68
     *
69
     * @return self
70
     */
71
    public function sorting($sorting)
72
    {
73
        $this->sorting = $sorting;
74
75
        return $this;
76
    }
77
78
    /**
79
     * @param string $order Order of results. Can be <samp>Order::ASC</samp> or <samp>Order::DESC</samp>
80
     *
81
     * @return self
82
     */
83
    public function order($order)
84
    {
85
        $this->order = $order;
86
87
        return $this;
88
    }
89
90
    /**
91
     * @param string[] $resolutions Array of resolutions in the format of WxH, for example:
92
     *                              <samp>['1920x1080', '1280x720']</samp>
93
     *
94
     * @return self
95
     */
96
    public function resolutions(array $resolutions)
97
    {
98
        $this->resolutions = $resolutions;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @param string[] $ratios Array of ratios in the format of WxH, for example:
105
     *                         <samp>['16x9', '4x3']</samp>
106
     *
107
     * @return self
108
     */
109
    public function ratios(array $ratios)
110
    {
111
        $this->ratios = $ratios;
112
113
        return $this;
114
    }
115
116
    /**
117
     * Set number of pages of wallpapers to fetch. A single page typically consists of 24, 32 or 64 wallpapers.
118
     *
119
     * @param int $pages Number of pages.
120
     *
121
     * @throws \InvalidArgumentException Thrown if the number of pages is negative or zero.
122
     * @return self
123
     */
124
    public function pages($pages)
125
    {
126
        if ($pages <= 0) {
127
            throw new \InvalidArgumentException("Number of pages must be positive.");
128
        }
129
130
        $this->pages = $pages;
131
132
        return $this;
133
    }
134
135
    /**
136
     * Execute the search with the specified filters.
137
     *
138
     * @return WallpaperList Wallpapers matching the specified filters.
139
     */
140
    public function getWallpapers()
141
    {
142
        $wallpapers = new WallpaperList();
143
144
        for ($i = 1; $i <= $this->pages; ++$i) {
145
            $wallpapers->addAll($this->wallhaven->search(
146
                $this->keywords,
147
                $this->categories,
148
                $this->purity,
149
                $this->sorting,
150
                $this->order,
151
                $this->resolutions,
152
                $this->ratios,
153
                $i
154
            ));
155
        }
156
157
        return $wallpapers;
158
    }
159
}
160