MatchTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 45
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B match() 0 34 10
1
<?php
2
/**
3
 * Spiral, Core Components
4
 *
5
 * @author Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Entities\Relations\Traits;
9
10
use Spiral\ORM\RecordInterface;
11
12
/**
13
 * Provides ability to compare entity and query.
14
 */
15
trait MatchTrait
16
{
17
    /**
18
     * Match entity by field intersection, instance values or primary key.
19
     *
20
     * @param RecordInterface             $record
21
     * @param RecordInterface|array|mixed $query
22
     *
23
     * @return bool
24
     */
25
    protected function match(RecordInterface $record, $query): bool
26
    {
27
        if ($record === $query) {
28
            //Strict search
29
            return true;
30
        }
31
32
        //Primary key comparision
33
        if (is_scalar($query) && $record->primaryKey() == $query) {
34
            return true;
35
        }
36
37
        //Record based comparision
38
        if ($query instanceof RecordInterface) {
39
            //Matched by PK (assuming both same class)
40
            if (!empty($query->primaryKey()) && $query->primaryKey() == $record->primaryKey()) {
41
                return true;
42
            }
43
44
            //Matched by content (this is a bit tricky!)
45
            if ($record->packValue() == $query->packValue()) {
46
                return true;
47
            }
48
49
            return false;
50
        }
51
52
        //Field intersection
53
        if (is_array($query) && array_intersect_assoc($record->packValue(), $query) == $query) {
54
            return true;
55
        }
56
57
        return false;
58
    }
59
}