SyncedTrait::key()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Entities\Relations\Traits;
9
10
use Spiral\ORM\Record;
11
use Spiral\ORM\RecordInterface;
12
13
/**
14
 * Checks if two records are synced (only for simple relations)
15
 */
16
trait SyncedTrait
17
{
18
    /**
19
     * If record not synced or can't be synced. Only work for PK based relations.
20
     *
21
     * @param RecordInterface $inner
22
     * @param RecordInterface $outer
23
     *
24
     * @return bool
25
     */
26
    protected function isSynced(RecordInterface $inner, RecordInterface $outer): bool
27
    {
28
        if (empty($inner->primaryKey()) || empty($outer->primaryKey())) {
29
            //Parent not saved
30
            return false;
31
        }
32
33
        //Comparing FK values
34
        return $outer->getField(
35
                $this->key(Record::OUTER_KEY)
36
            ) == $inner->getField(
37
                $this->key(Record::INNER_KEY)
38
            );
39
    }
40
41
    /**
42
     * Key name.
43
     *
44
     * @param int $key
45
     *
46
     * @return string|null
47
     */
48
    abstract protected function key(int $key);
49
}
50