summaryrefslogtreecommitdiffstats
path: root/vendor/ipl/orm/src/Behavior/BoolCast.php
blob: ad1748aba70707959c805709ed0b1e3989ca7e24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php

namespace ipl\Orm\Behavior;

use InvalidArgumentException;
use ipl\Orm\Contract\PropertyBehavior;
use ipl\Orm\Exception\ValueConversionException;

use function ipl\Stdlib\get_php_type;

/**
 * Convert specific database values from and to boolean
 *
 * To unify the support of boolean values in different database systems,
 * specific database values are converted to and from boolean values,
 * e.g. by default `n` is converted to `false` and `y` to `true` and vice versa respectively,
 * which could be stored as `ENUM('n', 'y')`.
 */
class BoolCast extends PropertyBehavior
{
    /** @var mixed Database value for boolean `false` */
    protected $falseValue = 'n';

    /** @var mixed Database value for boolean `true` */
    protected $trueValue = 'y';

    /** @var bool Whether to throw an exception if the value is not equal to the value for false or true */
    protected $strict = true;

    /**
     * Get the database value representing boolean `false`
     *
     * @return mixed
     */
    public function getFalseValue()
    {
        return $this->falseValue;
    }

    /**
     * Set the database value representing boolean `false`
     *
     * @param mixed $falseValue
     *
     * @return $this
     */
    public function setFalseValue($falseValue): self
    {
        $this->falseValue = $falseValue;

        return $this;
    }

    /**
     * Get the database value representing boolean `true`
     *
     * @return mixed
     */
    public function getTrueValue()
    {
        return $this->trueValue;
    }

    /**
     * Get the database value representing boolean `true`
     *
     * @param mixed $trueValue
     *
     * @return $this
     */
    public function setTrueValue($trueValue): self
    {
        $this->trueValue = $trueValue;

        return $this;
    }

    /**
     * Get whether to throw an exception if the value is not equal to the value for false or true
     *
     * @return bool
     */
    public function isStrict(): bool
    {
        return $this->strict;
    }

    /**
     * Set whether to throw an exception if the value is not equal to the value for false or true
     *
     * @param bool $strict
     *
     * @return $this
     */
    public function setStrict(bool $strict): self
    {
        $this->strict = $strict;

        return $this;
    }

    public function fromDb($value, $key, $_)
    {
        switch (true) {
            case $this->trueValue === $value:
                return true;
            case $this->falseValue === $value:
                return false;
            default:
                if ($this->isStrict() && $value !== null) {
                    throw new InvalidArgumentException(sprintf(
                        'Expected %s or %s, got %s instead',
                        $this->trueValue,
                        $this->falseValue,
                        $value
                    ));
                }

                return $value;
        }
    }

    public function toDb($value, $key, $_)
    {
        if ($value === null) {
            return null;
        }

        if (! is_bool($value)) {
            if (
                $this->isStrict()
                && $value !== '*'
                && $value !== $this->getFalseValue()
                && $value !== $this->getTrueValue()
            ) {
                throw new ValueConversionException(sprintf(
                    'Expected bool, got %s instead',
                    get_php_type($value)
                ));
            }

            return $value;
        }

        return $value ? $this->trueValue : $this->falseValue;
    }
}