Completed
Pull Request — 5.0 (#775)
by Michael
02:13
created

ObjectIterator::map()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
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 ONGR\ElasticsearchBundle\Result;
13
14
use Closure;
15
use Doctrine\Common\Collections\ArrayCollection;
16
17
/**
18
 * ObjectIterator class.
19
 */
20
class ObjectIterator extends ArrayCollection
21
{
22
    /**
23
     * @var Converter
24
     */
25
    private $converter;
26
27
    /**
28
     * @var array Aliases information.
29
     */
30
    private $alias;
31
32
    /**
33
     * @var array
34
     */
35
    private $rawObjects;
36
37
    /**
38
     * @var \Closure
39
     */
40
    private $convertCallback;
41
42
    /**
43
     * Using part of abstract iterator functionality only.
44
     *
45
     * @param Converter $converter
46
     * @param array     $objects
47
     * @param array     $alias
48
     */
49
    public function __construct($converter, $objects, $alias)
50
    {
51
        $this->converter = $converter;
52
        $this->rawObjects = $objects;
53
        $this->alias = $alias;
54
55
        // On-demand conversion callback for ArrayAccess/IteratorAggregate
56
        $this->convertCallback = function ($key) {
57
            $value = $this->convertDocument($this->rawObjects[$key]);
58
            $this->rawObjects[$key] = null;
59
            $this->offsetSet($key, $value);
60
            return $value;
61
        };
62
63
        $callback = function ($v) {
0 ignored issues
show
Unused Code introduced by
The parameter $v is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
            return null;
65
        };
66
67
        // Pass array with available keys and no values
68
        parent::__construct(array_map($callback, $objects));
69
    }
70
71
    /**
72
     * Converts all existing array values into their document equivalents.
73
     *
74
     * @return array
75
     */
76
    public function toArray()
77
    {
78
        $all = parent::toArray();
79
        $callback = $this->convertCallback;
80
        array_walk($all, function (&$value, $key) use ($callback) {
81
            if ($value === null) {
82
                $value = $callback($key);
83
            }
84
        });
85
        return $all;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    protected function convertDocument(array $document)
92
    {
93
        return $this->converter->assignArrayToObject(
94
            $document,
95
            new $this->alias['namespace'](),
96
            $this->alias['aliases']
97
        );
98
    }
99
100
    /**
101
     * Converts a raw object when the key for the object must be determined first.
102
     *
103
     * @param array $rawObject
104
     *
105
     * @return bool|object
106
     */
107
    protected function convertFromValue(array $rawObject)
108
    {
109
        if (false === $rawObject) {
110
            return false;
111
        }
112
        $callback = $this->convertCallback;
113
        $key = key($this->rawObjects);
114
        return $callback($key);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function getIterator()
121
    {
122
        $callback = $this->convertCallback;
123
        return new ObjectCallbackIterator($callback, $this->toArray());
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 View Code Duplication
    public function first()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $first = parent::first();
132
        if ($first === null) {
133
            $first = reset($this->rawObjects);
134
            return $this->convertFromValue($first);
135
        }
136
137
        return $first;
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143 View Code Duplication
    public function last()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
    {
145
        $last = parent::last();
146
147
        if ($last === null) {
148
            $last = end($this->rawObjects);
149
            return $this->convertFromValue($last);
150
        }
151
152
        return $last;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 View Code Duplication
    public function next()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
    {
160
        $next = parent::next();
161
162
        if ($next === null) {
163
            $next = next($this->rawObjects);
164
            return $this->convertFromValue($next);
165
        }
166
167
        return $next;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173 View Code Duplication
    public function current()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
    {
175
        $current = parent::current();
176
177
        if ($current === null) {
178
            $current = current($this->rawObjects);
179
            return $this->convertFromValue($current);
180
        }
181
182
        return $current;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188 View Code Duplication
    public function get($offset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
    {
190
        $value = parent::get($offset);
191
192
        // Generate objects on demand
193
        if ($value === null && $this->containsKey($this->key())) {
194
            $callback = $this->convertCallback;
195
            return $callback($offset);
196
        }
197
198
        return $value;
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204
    public function getValues()
205
    {
206
        return array_values($this->toArray());
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function map(Closure $func)
213
    {
214
        return new ArrayCollection(array_map($func, $this->toArray()));
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function filter(Closure $p)
221
    {
222
        return new ArrayCollection(array_filter($this->toArray(), $p));
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    protected function createFrom(array $elements)
229
    {
230
        return new static($this->converter, $elements, $this->alias);
231
    }
232
}
233