Completed
Branch feature/pre-split (c69968)
by Anton
21:25
created

LookupTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A lookupKey() 0 20 3
key() 0 1 ?
primaryColumnOf() 0 1 ?
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM\Entities\Relations\Traits;
8
9
use Spiral\ORM\ContextualCommandInterface;
10
use Spiral\ORM\RecordInterface;
11
12
/**
13
 * Looks for key values in command context AND in outer field if possible.
14
 */
15
trait LookupTrait
16
{
17
    /**
18
     * @param int                        $key
19
     * @param RecordInterface            $record
20
     * @param ContextualCommandInterface $command
21
     *
22
     * @return mixed|null
23
     */
24
    private function lookupKey(
25
        int $key,
26
        RecordInterface $record,
27
        ContextualCommandInterface $command
28
    ) {
29
        $key = $this->key($key);
30
31
        $context = $command->getContext();
32
        if (!empty($context[$key])) {
33
            //Key value found in a context
34
            return $context[$key];
35
        }
36
37
        if ($key == $this->primaryColumnOf($record)) {
38
            return $command->primaryKey();
39
        }
40
41
        //Fallback lookup in a record
42
        return $record->getField($key, null);
43
    }
44
45
    /**
46
     * Key name.
47
     *
48
     * @param int $key
49
     *
50
     * @return string
51
     */
52
    abstract protected function key(int $key): string;
53
54
    /**
55
     * Get primary key column
56
     *
57
     * @param RecordInterface $record
58
     *
59
     * @return string
60
     */
61
    abstract protected function primaryColumnOf(RecordInterface $record): string;
62
}