Passed
Push — master ( a4f7f2...d3cc39 )
by Melech
04:09
created

Value   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 7
c 1
b 0
f 0
dl 0
loc 31
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 14 3
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Orm\Data;
15
16
use Stringable;
17
use Valkyrja\Orm\QueryBuilder\Contract\QueryBuilder;
18
19
use function is_array;
20
21
/**
22
 * Class Value.
23
 *
24
 * @author Melech Mizrachi
25
 *
26
 * @psalm-type ValueType QueryBuilder|array<string|float|int|bool|null>|string|float|int|bool|null
27
 *
28
 * @phpstan-type ValueType QueryBuilder|array<string|float|int|bool|null>|string|float|int|bool|null
29
 */
30
readonly class Value implements Stringable
31
{
32
    /**
33
     * @param non-empty-string $name  The name of the value
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
34
     * @param ValueType        $value The value
35
     */
36
    public function __construct(
37
        public string $name,
38
        public QueryBuilder|array|string|float|int|bool|null $value = null,
39
    ) {
40
    }
41
42
    /**
43
     * Get the value as a string.
44
     *
45
     * @return non-empty-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
46
     */
47
    public function __toString(): string
48
    {
49
        $nameBind = ":$this->name";
50
        $value    = $this->value;
51
52
        if ($value instanceof QueryBuilder) {
53
            return '(' . ((string) $value) . ')';
54
        }
55
56
        if (! is_array($value)) {
57
            return $nameBind;
58
        }
59
60
        return '(' . $nameBind . implode(", $nameBind", array_keys($value)) . ')';
61
    }
62
}
63