Test Failed
Push — master ( 513669...a14d6b )
by Julien
09:38 queued 04:28
created

PrimaryKeys::getColumnMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model;
13
14
use Phalcon\Mvc\ModelInterface;
15
use Zemit\Mvc\Model\AbstractTrait\AbstractMetaData;
16
17
trait PrimaryKeys
18
{
19
    use AbstractMetaData;
20
    use Attribute;
21
    
22
    /**
23
     * Get Primary Key Attributes from models MetaData
24
     */
25
    public function getPrimaryKeys(): array
26
    {
27
        assert($this instanceof ModelInterface);
28
        return $this->getModelsMetaData()->getPrimaryKeyAttributes($this);
29
    }
30
    
31
    /**
32
     * Get column map from models MetaData
33
     */
34
    public function getColumnMap(): array
35
    {
36
        assert($this instanceof ModelInterface);
37
        return $this->getModelsMetaData()->getColumnMap($this);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getModelsM...()->getColumnMap($this) could return the type null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
38
    }
39
    
40
    /**
41
     * Get an array of primary keys values
42
     */
43
    public function getPrimaryKeysValues(): array
44
    {
45
        $primaryKeys = $this->getPrimaryKeys();
46
        $columnMap = $this->getColumnMap();
47
        
48
        $ret = [];
49
        
50
        foreach ($primaryKeys as $primaryKey) {
51
            $attributeField = $columnMap[$primaryKey] ?? $primaryKey;
52
            $ret [$attributeField] = $this->getAttribute($attributeField);
53
        }
54
        
55
        return $ret;
56
    }
57
}
58