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

PrimaryKeys   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 39
ccs 0
cts 14
cp 0
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnMap() 0 4 1
A getPrimaryKeysValues() 0 13 2
A getPrimaryKeys() 0 4 1
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