ForeignKey   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 131
rs 10
wmc 22

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getInverseValue() 0 18 6
A __construct() 0 4 1
A getValue() 0 19 6
A isComposite() 0 3 1
A extractValue() 0 18 6
A __toString() 0 3 1
A columns() 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 ForeignKey.php
33
 *
34
 *  The ForeignKey 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 Stringable;
50
51
/**
52
 * @class ForeignKey
53
 * @package Platine\Orm\Relation
54
 */
55
class ForeignKey implements Stringable
56
{
57
    /**
58
     *
59
     * @var array<string, string>
60
     */
61
    private array $columns = [];
62
63
    /**
64
     * Whether the foreign key is composite
65
     * @var bool
66
     */
67
    private bool $composite = false;
68
69
    /**
70
     * Create new instance
71
     * @param array<string, string> $columns
72
     */
73
    public function __construct(array $columns)
74
    {
75
        $this->columns = $columns;
76
        $this->composite = count($columns) > 1;
77
    }
78
79
    /**
80
     *
81
     * @return array<string, string>
82
     */
83
    public function columns(): array
84
    {
85
        return $this->columns;
86
    }
87
88
    /**
89
     *
90
     * @return bool
91
     */
92
    public function isComposite(): bool
93
    {
94
        return $this->composite;
95
    }
96
97
    /**
98
     * Return the foreign key value(s)
99
     * @param array<string, array<mixed>> $columns
100
     * @param bool $map
101
     *
102
     * @return mixed|array<string, mixed>|null
103
     */
104
    public function getValue(array $columns, bool $map = false): mixed
105
    {
106
        if (!$map && !$this->composite) {
107
            $column = array_keys($this->columns);
108
109
            return isset($columns[$column[0]])
110
                    ? $columns[$column[0]]
111
                    : null;
112
        }
113
114
        /** @var array<string, mixed> $values */
115
        $values = [];
116
        foreach ($this->columns as $candidate => $column) {
117
            $values[$column] = isset($columns[$candidate])
118
                                ? $columns[$candidate]
119
                                : null;
120
        }
121
122
        return $values;
123
    }
124
125
    /**
126
     * Return the foreign key inverse value(s)
127
     * @param array<string, mixed> $columns
128
     * @param bool $map
129
     *
130
     * @return mixed|array<string, mixed>|null
131
     */
132
    public function getInverseValue(array $columns, bool $map = false): mixed
133
    {
134
        if (!$map && !$this->composite) {
135
            $column = array_values($this->columns);
136
137
            return isset($columns[$column[0]])
138
                    ? $columns[$column[0]]
139
                    : null;
140
        }
141
        /** @var array<string, mixed> $values */
142
        $values = [];
143
        foreach ($this->columns as $candidate => $column) {
144
            $values[$candidate] = isset($columns[$column])
145
                                ? $columns[$column]
146
                                : null;
147
        }
148
149
        return $values;
150
    }
151
152
    /**
153
     * Extract the foreign key value(s)
154
     * @param array<string, mixed> $columns
155
     * @param bool $map
156
     *
157
     * @return mixed|array<string, mixed>|null
158
     */
159
    public function extractValue(array $columns, bool $map = false): mixed
160
    {
161
        if (!$map && !$this->composite) {
162
            $column = array_values($this->columns);
163
164
            return isset($columns[$column[0]])
165
                    ? $columns[$column[0]]
166
                    : null;
167
        }
168
169
        $values = [];
170
        foreach ($this->columns as $column) {
171
            $values[$column] = isset($columns[$column])
172
                                ? $columns[$column]
173
                                : null;
174
        }
175
176
        return $values;
177
    }
178
179
    /**
180
     * The string representation
181
     * @return string
182
     */
183
    public function __toString(): string
184
    {
185
        return implode(', ', $this->columns);
186
    }
187
}
188