DataContainer   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 135
c 0
b 0
f 0
wmc 10
lcom 0
cbo 3
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 2
A deriveResourceFilter() 0 15 3
A getOffset() 0 4 1
A getLimit() 0 4 1
A getTotal() 0 4 1
A getCount() 0 4 1
A getResults() 0 4 1
1
<?php
2
3
namespace Chadicus\Marvel\Api;
4
5
use DominionEnterprises\Util;
6
7
/**
8
 * Object representation of the API result data container.
9
 */
10
final class DataContainer implements DataContainerInterface
11
{
12
    /**
13
     * The requested offset (number of skipped results) of the call.
14
     *
15
     * @var integer
16
     */
17
    private $offset;
18
19
    /**
20
     * The requested result limit.
21
     *
22
     * @var integer
23
     */
24
    private $limit;
25
26
    /**
27
     * The total number of resources available given the current filter set.
28
     *
29
     * @var integer
30
     */
31
    private $total;
32
33
    /**
34
     * The total number of results returned by this call.
35
     *
36
     * @var integer
37
     */
38
    private $count;
39
40
    /**
41
     * The list of creators returned by the call.
42
     *
43
     * @var EntityInterface[]
44
     */
45
    private $results = [];
46
47
    /**
48
     * Create a new DataContainer instance.
49
     *
50
     * @param array $input The data for the DataContainer.
51
     */
52
    public function __construct(array $input)
53
    {
54
        $resourceFilter = self::deriveResourceFilter(Util\Arrays::get($input, 'results', []));
55
56
        $filters = [
57
            'offset' => ['default' => 0, ['int', true]],
58
            'limit' => ['default' => 0, ['int', true]],
59
            'total' => ['default' => 0, ['int', true]],
60
            'count' => ['default' => 0, ['int', true]],
61
            'results' => [[$resourceFilter]],
62
        ];
63
64
        list($success, $filtered, $error) = Filterer::filter($filters, $input, ['allowUnknowns' => true]);
65
        Util::ensure(true, $success, $error);
66
67
        foreach ($filtered as $key => $value) {
68
            $this->$key = $value;
69
        }
70
    }
71
72
    /**
73
     * Helper method to derive the filter to use for the given resource array
74
     *
75
     * @param mixed $results The results array from the API.
76
     *
77
     * @return callable The filter to use
78
     */
79
    private static function deriveResourceFilter($results)
80
    {
81
        $default = function () {
82
            return [];
83
        };
84
85
        if (!is_array($results) || !isset($results[0]['resourceURI'])) {
86
            return $default;
87
        }
88
89
        $pattern = '^' . preg_quote(Client::BASE_URL) . '(?P<resource>[a-z]*)/\d+$';
90
        $matches = [];
91
        preg_match("#{$pattern}#", $results[0]['resourceURI'], $matches);
92
        return Util\Arrays::get($matches, 'resource', $default);
93
    }
94
95
    /**
96
     * Returns The requested offset (number of skipped results) of the call.
97
     *
98
     * @return integer
99
     */
100
    public function getOffset() : int
101
    {
102
        return $this->offset;
103
    }
104
105
    /**
106
     * Returns The requested result limit.
107
     *
108
     * @return integer
109
     */
110
    public function getLimit() : int
111
    {
112
        return $this->limit;
113
    }
114
115
    /**
116
     * Returns The total number of resources available given the current filter set.
117
     *
118
     * @return integer
119
     */
120
    public function getTotal() : int
121
    {
122
        return $this->total;
123
    }
124
125
    /**
126
     * Returns The total number of results returned by this call.
127
     *
128
     * @return integer
129
     */
130
    public function getCount() : int
131
    {
132
        return $this->count;
133
    }
134
135
    /**
136
     * Returns The list of creators returned by the call.
137
     *
138
     * @return EntityInterface[]
139
     */
140
    public function getResults() : array
141
    {
142
        return $this->results;
143
    }
144
}
145