Resultset::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 4
1
<?php
2
3
namespace Picqer\Financials\Exact\Query;
4
5
use Picqer\Financials\Exact\Connection;
6
7
/**
8
 * Class Resultset.
9
 */
10
class Resultset
11
{
12
    /**
13
     * @var Connection
14
     */
15
    protected $connection;
16
17
    /**
18
     * @var string
19
     */
20
    protected $url;
21
22
    /**
23
     * @var string
24
     */
25
    protected $class;
26
27
    /**
28
     * @var array
29
     */
30
    protected $params;
31
32
    /**
33
     * Resultset constructor.
34
     *
35
     * @param Connection $connection
36
     * @param string     $url
37
     * @param string     $class
38
     * @param array      $params
39
     */
40
    public function __construct(Connection $connection, $url, $class, array $params)
41
    {
42
        $this->connection = $connection;
43
        $this->url = $url;
44
        $this->class = $class;
45
        $this->params = $params;
46
    }
47
48
    /**
49
     * @return array
50
     */
51
    public function next()
52
    {
53
        $result = $this->connection->get($this->url, $this->params);
54
        $this->url = $this->connection->nextUrl;
55
        $this->params = [];
56
57
        return $this->collectionFromResult($result);
58
    }
59
60
    /**
61
     * @return bool
62
     */
63
    public function hasMore()
64
    {
65
        return $this->url !== null;
66
    }
67
68
    /**
69
     * @param array $result
70
     *
71
     * @return array
72
     */
73
    protected function collectionFromResult($result)
74
    {
75
        // If we have one result which is not an assoc array, make it the first element of an array for the
76
        // collectionFromResult function so we always return a collection from filter
77
        if ((bool) count(array_filter(array_keys($result), 'is_string'))) {
78
            $result = [$result];
79
        }
80
81
        $class = $this->class;
82
        $collection = [];
83
84
        foreach ($result as $r) {
85
            $collection[] = new $class($this->connection, $r);
86
        }
87
88
        return $collection;
89
    }
90
}
91