Completed
Push — master ( 0afe29...1d6efb )
by WEBEWEB
01:12
created

ResponseNormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 73
c 0
b 0
f 0
wmc 5
lcom 0
cbo 4
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A denormalizePhoto() 0 13 1
A denormalizePhotosResponse() 0 23 3
A denormalizeSource() 0 14 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\Normalizer;
13
14
use WBW\Library\Core\Argument\ArrayHelper;
15
use WBW\Library\Pexels\Model\Photo;
16
use WBW\Library\Pexels\Model\Response\PhotosResponse;
17
use WBW\Library\Pexels\Model\Source;
18
19
/**
20
 * Response normalizer.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Library\Pexels\Normalizer
24
 */
25
class ResponseNormalizer {
26
27
    /**
28
     * Denormalize a photo.
29
     *
30
     * @param array $response The response.
31
     * @return Photo Returns a photo.
32
     */
33
    protected static function denormalizePhoto(array $response) {
34
35
        $model = new Photo();
36
        $model->setId(intval(ArrayHelper::get($response, "id", -1)));
37
        $model->setHeight(intval(ArrayHelper::get($response, "height", -1)));
38
        $model->setPhotographer(ArrayHelper::get($response, "photographer", null));
39
        $model->setPhotographerUrl(ArrayHelper::get($response, "photographer_url", null));
40
        $model->setSrc(static::denormalizeSource(ArrayHelper::get($response, "scr", [])));
41
        $model->setUrl(ArrayHelper::get($response, "url", null));
42
        $model->setWidth(intval(ArrayHelper::get($response, "width", -1)));
43
44
        return $model;
45
    }
46
47
    /**
48
     * Denormalize a photos response.
49
     *
50
     * @param string $rawResponse The raw response.
51
     * @return PhotosResponse Returns the photos response.
52
     */
53
    public static function denormalizePhotosResponse($rawResponse) {
54
55
        $decodedResponse = json_decode(trim($rawResponse), true);
56
57
        $model = new PhotosResponse();
58
        $model->setRawResponse($rawResponse);
59
60
        if (null === $decodedResponse) {
61
            return $model;
62
        }
63
64
        $model->setNextPage(ArrayHelper::get($decodedResponse, "next_page", null));
65
        $model->setPage(intval(ArrayHelper::get($decodedResponse, "page", -1)));
66
        $model->setPerPage(intval(ArrayHelper::get($decodedResponse, "per_page", -1)));
67
        $model->setTotalResults(intval(ArrayHelper::get($decodedResponse, "total_results", -1)));
68
        $model->setUrl(ArrayHelper::get($decodedResponse, "url", null));
69
70
        foreach (ArrayHelper::get($decodedResponse, "photos", []) as $current) {
71
            $model->addPhoto(static::denormalizePhoto($current));
72
        }
73
74
        return $model;
75
    }
76
77
    /**
78
     * Denormalize a source.
79
     *
80
     * @param array $response The response.
81
     * @return Source Returns a source.
82
     */
83
    protected static function denormalizeSource(array $response) {
84
85
        $model = new Source();
86
        $model->setLandscape(ArrayHelper::get($response, "landscape", null));
87
        $model->setLarge(ArrayHelper::get($response, "large", null));
88
        $model->setLarge2x(ArrayHelper::get($response, "large2x", null));
89
        $model->setMedium(ArrayHelper::get($response, "medium", null));
90
        $model->setOriginal(ArrayHelper::get($response, "original", null));
91
        $model->setPortrait(ArrayHelper::get($response, "portrait", null));
92
        $model->setSmall(ArrayHelper::get($response, "small", null));
93
        $model->setTiny(ArrayHelper::get($response, "tiny", null));
94
95
        return $model;
96
    }
97
}
98