RecordIterator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 62
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getIterator() 0 19 3
1
<?php
2
/**
3
 * Spiral, Core Components
4
 *
5
 * @author Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Entities;
9
10
use Spiral\ORM\ORMInterface;
11
12
/**
13
 * Instantiates array of entities. At this moment implementation is rather simple.
14
 */
15
class RecordIterator implements \IteratorAggregate
16
{
17
    /**
18
     * Array of entity data to be fed into instantiators.
19
     *
20
     * @var array
21
     */
22
    private $data = [];
23
24
    /**
25
     * Class to be instantiated.
26
     *
27
     * @var string
28
     */
29
    private $class;
30
31
    /**
32
     * Responsible for entity construction.
33
     *
34
     * @invisible
35
     * @var ORMInterface
36
     */
37
    private $orm;
38
39
    /**
40
     * @param array        $data
41
     * @param string       $class
42
     * @param ORMInterface $orm
43
     */
44
    public function __construct(array $data, string $class, ORMInterface $orm)
45
    {
46
        $this->data = $data;
47
        $this->class = $class;
48
        $this->orm = $orm;
49
    }
50
51
    /**
52
     * Generate over data.
53
     * Method will use pibot
54
     *
55
     * @return \Generator
56
     */
57
    public function getIterator(): \Generator
58
    {
59
        foreach ($this->data as $index => $data) {
60
            if (isset($data[ORMInterface::PIVOT_DATA])) {
61
                /*
62
                 * When pivot data is provided we are able to use it as array key.
63
                 */
64
                $index = $data[ORMInterface::PIVOT_DATA];
65
                unset($data[ORMInterface::PIVOT_DATA]);
66
            }
67
68
            yield $index => $this->orm->make(
69
                $this->class,
70
                $data,
71
                ORMInterface::STATE_LOADED,
72
                true
73
            );
74
        }
75
    }
76
}