Completed
Branch feature/pre-split (d91fae)
by Anton
05:19
created

RecordIterator::pivotData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 9.4285
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\ORM\Entities;
9
10
use Spiral\ORM\ORMInterface;
11
use Spiral\ORM\RecordInterface;
12
13
/**
14
 * Provides iteration over set of specified records data using internal instances cache. Does not
15
 * implements classic "collection" methods besides 'has'.
16
 */
17
class RecordIterator implements \IteratorAggregate, \Countable, \JsonSerializable
18
{
19
    /**
20
     * @var RecordInterface[]
21
     */
22
    private $instances = [];
23
24
    /**
25
     * Pivot data which links entity to parent.
26
     *
27
     * @var array
28
     */
29
    private $pivotData = [];
30
31
    /**
32
     * @param array        $data
33
     * @param string       $class
34
     * @param ORMInterface $orm
35
     */
36
    public function __construct(array $data, $class, ORMInterface $orm)
37
    {
38
        foreach ($data as $item) {
39
            $pivotData = $this->extractPivot($item);
40
            $this->instances[] = $instance = $orm->record($class, $item);
41
42
            if (!empty($pivotData)) {
43
                $this->pivotData[spl_object_hash($instance)] = $pivotData;
44
            }
45
        }
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function count()
52
    {
53
        return count($this->instances);
54
    }
55
56
    /**
57
     * Get pivot data related to a given object if any.
58
     *
59
     * @param RecordInterface $record
60
     * @return array
61
     */
62
    public function pivotData(RecordInterface $record)
63
    {
64
        $objectHash = spl_object_hash($record);
65
66
        if (empty($this->pivotData[$objectHash])) {
67
            return [];
68
        }
69
70
        return $this->pivotData[$objectHash];
71
    }
72
73
    /**
74
     * Check if record or record with specified id presents in iteration.
75
     *
76
     * @param RecordInterface|string|int $lookup
77
     *
78
     * @return true
79
     */
80
    public function has($lookup)
81
    {
82
        foreach ($this->instances as $record) {
83
            if (
84
                is_array($lookup) && array_intersect_assoc($record->getFields(), $lookup) == $lookup
85
            ) {
86
                //Comparing via fields intersection
87
                return true;
88
89
            }
90
91
            if (
92
                is_scalar($lookup) && !empty($lookup) && $record->primaryKey() == $lookup
93
            ) {
94
                //Comparing using primary keys
95
                return true;
96
            }
97
98
            if (
99
                $record == $lookup || $record->getFields() == $lookup->getFields()
0 ignored issues
show
Bug introduced by
It seems like $lookup is not always an object, but can also be of type string|integer|double|boolean|array. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
100
            ) {
101
                //Comparing as object vars
102
                return true;
103
            }
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * Get all Records as array.
111
     *
112
     * @return RecordInterface[]
113
     */
114
    public function all()
115
    {
116
        return $this->instances;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getIterator()
123
    {
124
        return new \ArrayIterator($this->instances);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function jsonSerialize()
131
    {
132
        return $this->instances;
133
    }
134
135
    /**
136
     * Flushing references.
137
     */
138
    public function __destruct()
139
    {
140
        $this->pivotData = [];
141
        $this->instances = [];
142
    }
143
144
    /**
145
     * @param array $data
146
     * @return array|null
147
     */
148
    private function extractPivot(array &$data)
149
    {
150
        if (!empty($data[ORMInterface::PIVOT_DATA])) {
151
            $pivotData = $data[ORMInterface::PIVOT_DATA];
152
            unset($data[ORMInterface::PIVOT_DATA]);
153
154
            return $pivotData;
155
        }
156
157
        return null;
158
    }
159
}