|
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 |
|
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) { |
|
|
|
|
|
|
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
|
|
View Code Duplication |
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
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
View Code Duplication |
public function offsetGet($offset) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$value = parent::offsetGet($offset); |
|
93
|
|
|
|
|
94
|
|
|
// Generate objects on demand |
|
95
|
|
|
if ($value === null && $this->valid()) { |
|
96
|
|
|
$value = $this->convertDocument($this->rawObjects[$offset]); |
|
97
|
|
|
$this->rawObjects[$offset] = null; |
|
98
|
|
|
$this->offsetSet($offset, $value); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $value; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.