Completed
Push — master ( 407357...7ade0b )
by WEBEWEB
01:13
created

RequestSerializer::serializeSearchPhotosRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the pexels-library package.
5
 *
6
 * (c) 2019 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Pexels\Serializer;
13
14
use InvalidArgumentException;
15
use WBW\Library\Core\Argument\Helper\ArrayHelper;
16
use WBW\Library\Pexels\Model\Request\CuratedPhotosRequest;
17
use WBW\Library\Pexels\Model\Request\PopularVideosRequest;
18
use WBW\Library\Pexels\Model\Request\SearchPhotosRequest;
19
use WBW\Library\Pexels\Model\Request\SearchVideosRequest;
20
21
/**
22
 * Request serializer.
23
 *
24
 * @author webeweb <https://github.com/webeweb/>
25
 * @package WBW\Library\Pexels\Serializer
26
 */
27
class RequestSerializer {
28
29
    /**
30
     * Serialize a curated photos request.
31
     *
32
     * @param CuratedPhotosRequest $request The curated photos request.
33
     * @return array Returns the parameters.
34
     */
35
    public static function serializeCuratedPhotosRequest(CuratedPhotosRequest $request) {
36
37
        $parameters = [];
38
39
        ArrayHelper::set($parameters, "per_page", $request->getPerPage(), [null, CuratedPhotosRequest::PER_PAGE_DEFAULT]);
40
        ArrayHelper::set($parameters, "page", $request->getPage(), [null, 1]);
41
42
        return $parameters;
43
    }
44
45
    /**
46
     * Serialize a popular videos request.
47
     *
48
     * @param PopularVideosRequest $request The popular photos request.
49
     * @return array Returns the parameters.
50
     */
51
    public static function serializePopularVideosRequest(PopularVideosRequest $request) {
52
53
        $parameters = [];
54
55
        ArrayHelper::set($parameters, "per_page", $request->getPerPage(), [null, PopularVideosRequest::PER_PAGE_DEFAULT]);
56
        ArrayHelper::set($parameters, "page", $request->getPage(), [null, 1]);
57
        ArrayHelper::set($parameters, "min_width", $request->getMinWidth(), [null]);
58
        ArrayHelper::set($parameters, "max_width", $request->getMaxWidth(), [null]);
59
        ArrayHelper::set($parameters, "min_duration", $request->getMinDuration(), [null]);
60
        ArrayHelper::set($parameters, "max_duration", $request->getMaxDuration(), [null]);
61
62
        return $parameters;
63
    }
64
65
    /**
66
     * Serialize a search photos request.
67
     *
68
     * @param SearchPhotosRequest $request The search photos request.
69
     * @return array Returns the parameters.
70
     * @throws InvalidArgumentException Throws an invalid argument exception if a mandatory parameter is missing.
71
     */
72
    public static function serializeSearchPhotosRequest(SearchPhotosRequest $request) {
73
74
        $parameters = [];
75
76
        if (null === $request->getQuery()) {
77
            throw new InvalidArgumentException("The mandatory parameter \"query\" is missing");
78
        }
79
80
        ArrayHelper::set($parameters, "query", $request->getQuery());
81
        ArrayHelper::set($parameters, "per_page", $request->getPerPage(), [null, SearchPhotosRequest::PER_PAGE_DEFAULT]);
82
        ArrayHelper::set($parameters, "page", $request->getPage(), [null, 1]);
83
84
        return $parameters;
85
    }
86
87
    /**
88
     * Serialize a search videos request.
89
     *
90
     * @param SearchVideosRequest $request The search photos request.
91
     * @return array Returns the parameters.
92
     * @throws InvalidArgumentException Throws an invalid argument exception if a mandatory parameter is missing.
93
     */
94
    public static function serializeSearchVideosRequest(SearchVideosRequest $request) {
95
96
        $parameters = [];
97
98
        if (null === $request->getQuery()) {
99
            throw new InvalidArgumentException("The mandatory parameter \"query\" is missing");
100
        }
101
102
        ArrayHelper::set($parameters, "query", $request->getQuery());
103
        ArrayHelper::set($parameters, "per_page", $request->getPerPage(), [null, SearchVideosRequest::PER_PAGE_DEFAULT]);
104
        ArrayHelper::set($parameters, "page", $request->getPage(), [null, 1]);
105
        ArrayHelper::set($parameters, "min_width", $request->getMinWidth(), [null]);
106
        ArrayHelper::set($parameters, "max_width", $request->getMaxWidth(), [null]);
107
        ArrayHelper::set($parameters, "min_duration", $request->getMinDuration(), [null]);
108
        ArrayHelper::set($parameters, "max_duration", $request->getMaxDuration(), [null]);
109
110
        return $parameters;
111
    }
112
}
113