|
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
|
|
|
// Pass array with available keys and no values |
|
50
|
|
|
parent::__construct(array_combine(array_keys($objects), array_fill(0, count($objects), null))); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function convertDocument(array $document) |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->converter->assignArrayToObject( |
|
59
|
|
|
$document, |
|
60
|
|
|
new $this->alias['namespace'](), |
|
61
|
|
|
$this->alias['aliases'] |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function current() |
|
69
|
|
|
{ |
|
70
|
|
|
$value = parent::current(); |
|
71
|
|
|
|
|
72
|
|
|
// Generate objects on demand |
|
73
|
|
|
if ($value === null && $this->valid()) { |
|
74
|
|
|
$key = $this->key(); |
|
75
|
|
|
$value = $this->convertDocument($this->rawObjects[$key]); |
|
76
|
|
|
$this->rawObjects[$key] = null; |
|
77
|
|
|
$this->offsetSet($key, $value); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $value; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|