Completed
Pull Request — 5.0 (#775)
by Michael
15:21
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
     * {@inheritdoc}
84
     */
85
    public function getIterator()
86
    {
87
        $callback = $this->convertCallback;
88
        return new ObjectCallbackIterator($callback, $this->toArray());
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 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...
95
    {
96
        $value = parent::get($offset);
97
98
        // Generate objects on demand
99
        if ($value === null && $this->containsKey($this->key())) {
100
            $callback = $this->convertCallback;
101
            return $callback($offset);
102
        }
103
104
        return $value;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    protected function createFrom(array $elements)
111
    {
112
        return new static($this->converter, $elements, $this->alias);
113
    }
114
}
115