Passed
Push — master ( c85748...2fd0ab )
by Petr
08:11
created

TFileTable::insertRecord()   B

Complexity

Conditions 7
Paths 11

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 12
c 1
b 0
f 1
nc 11
nop 1
dl 0
loc 21
ccs 13
cts 13
cp 1
crap 7
rs 8.8333
1
<?php
2
3
namespace kalanis\kw_mapper\Mappers\Shared;
4
5
6
use kalanis\kw_mapper\Adapters\DataExchange;
7
use kalanis\kw_mapper\Interfaces\IEntryType;
8
use kalanis\kw_mapper\MapperException;
9
use kalanis\kw_mapper\Records;
10
11
12
/**
13
 * Trait TFileTable
14
 * @package kalanis\kw_mapper\Mappers\Shared
15
 * Abstract for manipulation with file content as table
16
 */
17
trait TFileTable
18
{
19
    use TFinder;
20
    use TStore;
21
22
    /** @var bool */
23
    protected $orderFromFirst = true;
24
25 3
    public function orderFromFirst(bool $orderFromFirst = true): self
26
    {
27 3
        $this->orderFromFirst = $orderFromFirst;
28 3
        return $this;
29
    }
30
31
    /**
32
     * @param Records\ARecord|Records\PageRecord $record
33
     * @throws MapperException
34
     * @return bool
35
     */
36 5
    protected function insertRecord(Records\ARecord $record): bool
37
    {
38 5
        $matches = $this->findMatched($record, !empty($this->getPrimaryKeys()));
39 5
        if (!empty($matches)) { // found!!!
40 1
            return false;
41
        }
42
43
        // pks
44 5
        $records = array_map([$this, 'toArray'], $this->records);
45 5
        foreach ($this->getPrimaryKeys() as $primaryKey) {
46 4
            $entry = $record->getEntry($primaryKey);
47 4
            if (in_array($entry->getType(), [IEntryType::TYPE_INTEGER, IEntryType::TYPE_FLOAT])) {
48 1
                if (empty($entry->getData())) {
49 1
                    $data = empty($records) ? 1 : intval(max(array_column($records, $primaryKey))) + 1 ;
50 1
                    $entry->setData($data);
51
                }
52
            }
53
        }
54
55 5
        $this->records = $this->orderFromFirst ? array_merge($this->records, [$record]) : array_merge([$record], $this->records);
56 5
        return $this->saveSource($this->records);
57
    }
58
59
    /**
60
     * @param Records\ARecord $object
61
     * @return array<string|int, string|int|float|object|array<string|int|float|object>>
62
     */
63 5
    public function toArray($object)
64
    {
65 5
        $ex = new DataExchange($object);
66 5
        return $ex->export();
67
    }
68
69
    /**
70
     * @param Records\ARecord|Records\PageRecord $record
71
     * @throws MapperException
72
     * @return bool
73
     */
74 4
    protected function updateRecord(Records\ARecord $record): bool
75
    {
76 4
        $matches = $this->findMatched($record, !empty($this->getPrimaryKeys()), true);
77 4
        if (empty($matches)) { // nothing found
78 1
            return false;
79
        }
80
81 3
        reset($matches);
82 3
        $dataLine = & $this->records[key($matches)];
83 3
        foreach ($this->getRelations() as $objectKey => $recordKey) {
84 3
            if (in_array($objectKey, $this->getPrimaryKeys())) {
85 2
                continue; // no to change pks
86
            }
87 3
            $dataLine->offsetSet($objectKey, $record->offsetGet($objectKey));
88
        }
89 3
        return $this->saveSource($this->records);
90
    }
91
92
    /**
93
     * @param Records\ARecord|Records\PageRecord $record
94
     * @throws MapperException
95
     * @return int
96
     */
97 1
    public function countRecord(Records\ARecord $record): int
98
    {
99 1
        return count($this->findMatched($record));
100
    }
101
102
    /**
103
     * @param Records\ARecord|Records\PageRecord $record
104
     * @throws MapperException
105
     * @return bool
106
     */
107 6
    protected function loadRecord(Records\ARecord $record): bool
108
    {
109 6
        $matches = $this->findMatched($record);
110 6
        if (empty($matches)) { // nothing found
111 2
            return false;
112
        }
113
114 5
        reset($matches);
115 5
        $dataLine = & $this->records[key($matches)];
116 5
        foreach ($this->getRelations() as $objectKey => $recordKey) {
117 5
            $entry = $record->getEntry($objectKey);
118 5
            $entry->setData($dataLine->offsetGet($objectKey), true);
119
        }
120 5
        return true;
121
    }
122
123
    /**
124
     * @param Records\ARecord|Records\PageRecord $record
125
     * @throws MapperException
126
     * @return bool
127
     * Scan array and remove items that have set equal values as that in passed record
128
     */
129 4
    protected function deleteRecord(Records\ARecord $record): bool
130
    {
131 4
        $toDelete = $this->findMatched($record);
132 4
        if (empty($toDelete)) {
133 1
            return false;
134
        }
135
136
        // remove matched
137 3
        foreach ($toDelete as $key => $record) {
138 3
            unset($this->records[$key]);
139
        }
140 3
        return $this->saveSource($this->records);
141
    }
142
143
    /**
144
     * @param Records\ARecord $record
145
     * @throws MapperException
146
     * @return Records\ARecord[]
147
     */
148 15
    public function loadMultiple(Records\ARecord $record): array
149
    {
150 15
        return array_values($this->findMatched($record));
151
    }
152
}
153