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

ObjectIterator::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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 ONGR\ElasticsearchBundle\Collection\Collection;
15
16
/**
17
 * ObjectIterator class.
18
 */
19
class ObjectIterator extends Collection
0 ignored issues
show
Deprecated Code introduced by
The class ONGR\ElasticsearchBundle\Collection\Collection has been deprecated with message: Use Doctrine\Common\Collections\ArrayCollection instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
20
{
21
    /**
22
     * @var Converter
23
     */
24
    private $converter;
25
26
    /**
27
     * @var array Aliases information.
28
     */
29
    private $alias;
30
31
    /**
32
     * @var array
33
     */
34
    private $rawObjects;
35
36
    /**
37
     * @var \Closure
38
     */
39
    private $convertCallback;
40
41
    /**
42
     * Using part of abstract iterator functionality only.
43
     *
44
     * @param Converter $converter
45
     * @param array     $objects
46
     * @param array     $alias
47
     */
48
    public function __construct($converter, $objects, $alias)
49
    {
50
        $this->converter = $converter;
51
        $this->rawObjects = $objects;
52
        $this->alias = $alias;
53
54
        // On-demand conversion callback for ArrayAccess/IteratorAggregate
55
        $this->convertCallback = function ($key) {
56
            $value = $this->convertDocument($this->rawObjects[$key]);
57
            $this->rawObjects[$key] = null;
58
            $this->offsetSet($key, $value);
59
            return $value;
60
        };
61
62
        $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...
63
            return null;
64
        };
65
66
        // Pass array with available keys and no values
67
        parent::__construct(array_map($callback, $objects));
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected function convertDocument(array $document)
74
    {
75
        return $this->converter->assignArrayToObject(
76
            $document,
77
            new $this->alias['namespace'](),
78
            $this->alias['aliases']
79
        );
80
    }
81
82
    /**
83
     * Converts a raw object when the key for the object must be determined first.
84
     *
85
     * @param array $rawObject
86
     *
87
     * @return bool|object
88
     */
89
    protected function convertFromValue(array $rawObject) {
90
        if (false === $rawObject) {
91
            return false;
92
        }
93
        $callback = $this->convertCallback;
94
        $key = key($this->rawObjects);
95
        return $callback($key);
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getIterator()
102
    {
103
        $callback = $this->convertCallback;
104
        return new ObjectCallbackIterator($callback, $this->toArray());
105
    }
106
107 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...
108
    {
109
        $first = parent::first();
110
        if ($first === null) {
111
            $first = reset($this->rawObjects);
112
            return $this->convertFromValue($first);
113
        }
114
115
        return $first;
116
    }
117
118 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...
119
    {
120
        $last = parent::last();
121
122
        if ($last === null) {
123
            $last = end($this->rawObjects);
124
            return $this->convertFromValue($last);
125
        }
126
127
        return $last;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 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...
134
    {
135
        $value = parent::get($offset);
136
137
        // Generate objects on demand
138
        if ($value === null && $this->containsKey($this->key())) {
139
            $callback = $this->convertCallback;
140
            return $callback($offset);
141
        }
142
143
        return $value;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    protected function createFrom(array $elements)
150
    {
151
        return new static($this->converter, $elements, $this->alias);
152
    }
153
}
154