PrimaryKey   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 99
rs 10
wmc 12

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A __construct() 0 4 1
A columns() 0 3 1
A getValue() 0 17 6
A getValueFromEntity() 0 4 1
A getValueFromDataMapper() 0 3 1
A isComposite() 0 3 1
1
<?php
2
3
/**
4
 * Platine ORM
5
 *
6
 * Platine ORM provides a flexible and powerful ORM implementing a data-mapper pattern.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine ORM
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file PrimaryKey.php
33
 *
34
 *  The PrimaryKey class
35
 *
36
 *  @package    Platine\Orm\Relation
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   https://www.platine-php.com
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Orm\Relation;
48
49
use Platine\Orm\Entity;
50
use Platine\Orm\Mapper\DataMapper;
51
use Platine\Orm\Mapper\Proxy;
52
use Stringable;
53
54
/**
55
 * @class PrimaryKey
56
 * @package Platine\Orm\Relation
57
 * @template TEntity as Entity
58
 */
59
class PrimaryKey implements Stringable
60
{
61
    /**
62
     *
63
     * @var array<int, string>
64
     */
65
    private array $columns = [];
66
67
    /**
68
     * Whether the primary key is composite
69
     * @var bool
70
     */
71
    private bool $composite = false;
72
73
    /**
74
     * Create new instance
75
     * @param string ...$columns
76
     */
77
    public function __construct(string ...$columns)
78
    {
79
        $this->columns = array_values($columns);
80
        $this->composite = count($columns) > 1;
81
    }
82
83
    /**
84
     *
85
     * @return array<int, string>
86
     */
87
    public function columns(): array
88
    {
89
        return $this->columns;
90
    }
91
92
    /**
93
     *
94
     * @return bool
95
     */
96
    public function isComposite(): bool
97
    {
98
        return $this->composite;
99
    }
100
101
    /**
102
     * Return the primary key value(s)
103
     * @param array<string, mixed> $columns
104
     * @param bool $map
105
     *
106
     * @return mixed|array<string, mixed>|null
107
     */
108
    public function getValue(array $columns, bool $map = false): mixed
109
    {
110
        if (!$this->composite && !$map) {
111
            return isset($columns[$this->columns[0]])
112
                    ? $columns[$this->columns[0]]
113
                    : null;
114
        }
115
116
        /** @var array<string, mixed> $values */
117
        $values = [];
118
        foreach ($this->columns as $column) {
119
            $values[$column] = isset($columns[$column])
120
                                ? $columns[$column]
121
                                : null;
122
        }
123
124
        return $values;
125
    }
126
127
    /**
128
     * Get the value from data mapper
129
     * @param DataMapper<TEntity> $mapper
130
     * @param bool $map
131
     *
132
     * @return mixed|array<string, mixed>|null
133
     */
134
    public function getValueFromDataMapper(DataMapper $mapper, bool $map = false): mixed
135
    {
136
        return $this->getValue($mapper->getRawColumns(), $map);
137
    }
138
139
    /**
140
     *
141
     * @param TEntity $entity
0 ignored issues
show
Bug introduced by
The type Platine\Orm\Relation\TEntity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
142
     * @param bool $map
143
     * @return mixed|array<string, mixed>|null
144
     */
145
    public function getValueFromEntity(Entity $entity, bool $map = false): mixed
146
    {
147
        $columns = Proxy::instance()->getEntityColumns($entity);
148
        return $this->getValue($columns, $map);
149
    }
150
151
    /**
152
     * The string representation
153
     * @return string
154
     */
155
    public function __toString(): string
156
    {
157
        return implode(', ', $this->columns);
158
    }
159
}
160