SearchIterator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
/**
4
 * This file is a part of nekland places api package
5
 *
6
 * (c) Nekland <[email protected]>
7
 *
8
 * For the full license, take a look to the LICENSE file
9
 * on the root directory of this project
10
 */
11
12
namespace Nekland\PlacesApi\Iterator;
13
14
use Nekland\PlacesApi\Api\Search;
15
16
class SearchIterator implements \Iterator
17
{
18
    /**
19
     * @var Search
20
     */
21
    private $api;
22
23
    /**
24
     * @var array
25
     */
26
    private $body;
27
28
    /**
29
     * @var string
30
     */
31
    private $currentPage;
32
33
    /**
34
     * @var string
35
     */
36
    private $nextPage;
37
38
    /**
39
     * @var integer
40
     */
41
    private $key;
42
43
    public function __construct(Search $api, array $body)
44
    {
45
        $this->api = $api;
46
        $this->body = $body;
47
        $this->key = 0;
48
    }
49
50
    /**
51
     * (PHP 5 &gt;= 5.0.0)<br/>
52
     * Return the current element
53
     * @link http://php.net/manual/en/iterator.current.php
54
     * @return mixed Can return any type.
55
     */
56
    public function current()
57
    {
58
        // Getting the first page
59
        if ($this->currentPage === null) {
60
            $data = $this->api->searchWithBody($this->body);
61
            if (isset($data['next_page_token'])) {
62
                $this->nextPage = $data['next_page_token'];
63
            }
64
65
            return $data;
66
        }
67
68
        // Getting another page
69
        $body = array_merge($this->body, ['pagetoken' => $this->currentPage]);
70
        $data = $this->api->searchWithBody($body);
71
        if (isset($data['next_page_token'])) {
72
            $this->nextPage = $data['next_page_token'];
73
        } else {
74
            $this->nextPage = null;
75
        }
76
77
        return $data;
78
    }
79
80
    /**
81
     * (PHP 5 &gt;= 5.0.0)<br/>
82
     * Move forward to next element
83
     * @link http://php.net/manual/en/iterator.next.php
84
     * @return void Any returned value is ignored.
85
     */
86
    public function next()
87
    {
88
        $this->currentPage = $this->nextPage;
89
        $this->key++;
90
    }
91
92
    /**
93
     * (PHP 5 &gt;= 5.0.0)<br/>
94
     * Return the key of the current element
95
     * @link http://php.net/manual/en/iterator.key.php
96
     * @return mixed scalar on success, or null on failure.
97
     */
98
    public function key()
99
    {
100
        return $this->key;
101
    }
102
103
    /**
104
     * (PHP 5 &gt;= 5.0.0)<br/>
105
     * Checks if current position is valid
106
     * @link http://php.net/manual/en/iterator.valid.php
107
     * @return boolean The return value will be casted to boolean and then evaluated.
108
     * Returns true on success or false on failure.
109
     */
110
    public function valid()
111
    {
112
        // fails only when
113
        if ($this->key !== 0 && $this->currentPage === null) {
114
            return false;
115
        }
116
117
        return true;
118
    }
119
120
    /**
121
     * (PHP 5 &gt;= 5.0.0)<br/>
122
     * Rewind the Iterator to the first element
123
     * @link http://php.net/manual/en/iterator.rewind.php
124
     * @return void Any returned value is ignored.
125
     */
126
    public function rewind()
127
    {
128
        $this->key = 0;
129
        $this->currentPage = null;
130
        $this->nextPage = null;
131
    }
132
}
133