RowSearchResult::getPrimaryKeyColumn()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * @copyright   (c) 2017-present brian ridley
5
 * @author      brian ridley <[email protected]>
6
 * @license     http://opensource.org/licenses/MIT MIT
7
 */
8
9
namespace ptlis\GrepDb\Search\Result;
10
11
use ptlis\GrepDb\Metadata\MySQL\ColumnMetadata;
12
use ptlis\GrepDb\Metadata\MySQL\TableMetadata;
13
14
/**
15
 * DTO representing a row with matched fields.
16
 */
17
final class RowSearchResult
18
{
19
    /** @var FieldSearchResult[] */
20
    private $fieldMatchList = [];
21
22
    /** @var null|ColumnMetadata */
23
    private $primaryKeyColumn;
24
25
    /** @var mixed|null */
26
    private $primaryKeyValue;
27
28
    /** @var TableMetadata */
29
    private $tableMetadata;
30
31
32
    /**
33
     * @param TableMetadata $tableMetadata
34
     * @param FieldSearchResult[] $fieldMatchList
35
     * @param ColumnMetadata|null $primaryKeyColumn
36
     * @param mixed|null $primaryKeyValue
37
     */
38 3
    public function __construct(
39
        TableMetadata $tableMetadata,
40
        array $fieldMatchList,
41
        ?ColumnMetadata $primaryKeyColumn = null,
42
        $primaryKeyValue = null
43
    ) {
44 3
        $this->tableMetadata = $tableMetadata;
45 3
        foreach ($fieldMatchList as $fieldMatch) {
46 3
            $this->fieldMatchList[$fieldMatch->getMetadata()->getColumnName()] = $fieldMatch;
47
        }
48 3
        $this->primaryKeyColumn = $primaryKeyColumn;
49 3
        $this->primaryKeyValue = $primaryKeyValue;
50 3
    }
51
52
    public function getTableMetadata(): TableMetadata
53
    {
54
        return $this->tableMetadata;
55
    }
56
57
    /**
58
     * Get array of matching columns in this row.
59
     *
60
     * @return FieldSearchResult[]
61
     */
62 1
    public function getMatchingFields(): array
63
    {
64 1
        return $this->fieldMatchList;
65
    }
66
67
    /**
68
     * Returns true if the column result exists.
69
     */
70 3
    public function hasColumnResult(string $columnName): bool
71
    {
72 3
        return array_key_exists($columnName, $this->fieldMatchList);
73
    }
74
75
    /**
76
     * Returns the column result matching the passed name.
77
     */
78 2
    public function getColumnResult(string $columnName): FieldSearchResult
79
    {
80 2
        if (!$this->hasColumnResult($columnName)) {
81 1
            throw new \RuntimeException('Could not find changed column named "' . $columnName . '"');
82
        }
83
84 1
        return $this->fieldMatchList[$columnName];
85
    }
86
87
    /**
88
     * Returns the primary key column's metadata.
89
     *
90
     * @throws \RuntimeException If the result doesn't have a primary key
91
     */
92 1
    public function getPrimaryKeyColumn(): ?ColumnMetadata
93
    {
94 1
        return $this->primaryKeyColumn;
95
    }
96
97
    /**
98
     * Returns the primary key value.
99
     *
100
     * @throws \RuntimeException If the result doesn't have a primary key
101
     * @return mixed|null
102
     */
103 1
    public function getPrimaryKeyValue()
104
    {
105 1
        return $this->primaryKeyValue;
106
    }
107
}