Completed
Pull Request — master (#527)
by Mantas
10:45 queued 03:22
created

ObjectIterator::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
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;
15
16
/**
17
 * ObjectIterator class.
18
 */
19
class ObjectIterator extends Collection
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
     * Using part of abstract iterator functionality only.
38
     *
39
     * @param Converter $converter
40
     * @param array     $objects
41
     * @param array     $alias
42
     */
43
    public function __construct($converter, $objects, $alias)
44
    {
45
        $this->converter = $converter;
46
        $this->rawObjects = $objects;
47
        $this->alias = $alias;
48
49
        $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...
50
            return null;
51
        };
52
53
        // Pass array with available keys and no values
54
        parent::__construct(array_map($callback, $objects));
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function convertDocument(array $document)
61
    {
62
        return $this->converter->assignArrayToObject(
63
            $document,
64
            new $this->alias['namespace'](),
65
            $this->alias['aliases']
66
        );
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function current()
73
    {
74
        $value = parent::current();
75
76
        // Generate objects on demand
77
        if ($value === null && $this->valid()) {
78
            $key = $this->key();
79
            $value = $this->convertDocument($this->rawObjects[$key]);
80
            $this->rawObjects[$key] = null;
81
            $this->offsetSet($key, $value);
82
        }
83
84
        return $value;
85
    }
86
}
87