Issues (1)

src/ValueObject.php (1 issue)

Labels
Severity
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects)
7
 *
8
 * @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository
9
 * @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/)
10
 * @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT
11
 */
12
13
namespace MichaelRubel\ValueObjects;
14
15
use Illuminate\Contracts\Support\Arrayable;
16
use Illuminate\Support\Traits\Conditionable;
17
use Illuminate\Support\Traits\Macroable;
18
use InvalidArgumentException;
19
use MichaelRubel\ValueObjects\Concerns\HandlesCallbacks;
20
use MichaelRubel\ValueObjects\Contracts\Immutable;
21
use Throwable;
22
23
/**
24
 * Base "ValueObject".
25
 *
26
 * @author Michael Rubél <[email protected]>
27
 *
28
 * @template TKey of array-key
29
 * @template TValue
30
 *
31
 * @implements Arrayable<TKey, TValue>
32
 */
33
abstract class ValueObject implements Arrayable, Immutable
34
{
35
    use Conditionable, HandlesCallbacks, Macroable;
36
37
    /**
38
     * Get the object value.
39
     *
40
     * @return mixed
41
     */
42
    abstract public function value();
43
44
    /**
45
     * Convenient method to create a value object statically.
46
     *
47
     * @param  mixed  $values
48
     *
49
     * @return static
50
     */
51 12
    public static function make(mixed ...$values): static
52
    {
53 12
        return new static(...$values);
54
    }
55
56
    /**
57
     * Convenient method to create a value object statically.
58
     *
59
     * @param  mixed  $values
60
     *
61
     * @return static
62
     */
63 10
    public static function from(mixed ...$values): static
64
    {
65 10
        return static::make(...$values);
66
    }
67
68
    /**
69
     * Create a value object or return null.
70
     *
71
     * @param  mixed  $values
72
     *
73
     * @return static|null
74
     */
75 1
    public static function makeOrNull(mixed ...$values): ?static
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STATIC on line 75 at column 58
Loading history...
76
    {
77
        try {
78 1
            return static::make(...$values);
79 1
        } catch (Throwable) {
80 1
            return null;
81
        }
82
    }
83
84
    /**
85
     * Check if objects are instances of same class
86
     * and share the same properties and values.
87
     *
88
     * @param  ValueObject<int|string, mixed>  $object
89
     *
90
     * @return bool
91
     */
92 3
    public function equals(ValueObject $object): bool
93
    {
94 3
        return $this == $object;
95
    }
96
97
    /**
98
     * Inversion for `equals` method.
99
     *
100
     * @param  ValueObject<int|string, mixed>  $object
101
     *
102
     * @return bool
103
     */
104 2
    public function notEquals(ValueObject $object): bool
105
    {
106 2
        return ! $this->equals($object);
107
    }
108
109
    /**
110
     * Get an array representation of the value object.
111
     *
112
     * @return array
113
     */
114 7
    public function toArray(): array
115
    {
116 7
        return (array) $this->value();
117
    }
118
119
    /**
120
     * Get string representation of the value object.
121
     *
122
     * @return string
123
     */
124 18
    public function toString(): string
125
    {
126 18
        return (string) $this->value();
127
    }
128
129
    /**
130
     * Get string representation of the value object.
131
     *
132
     * @return string
133
     */
134 17
    public function __toString(): string
135
    {
136 17
        return $this->toString();
137
    }
138
139
    /**
140
     * Get the internal property value.
141
     *
142
     * @param  string  $name
143
     *
144
     * @return mixed
145
     */
146 12
    public function __get(string $name): mixed
147
    {
148 12
        return $this->{$name};
149
    }
150
151
    /**
152
     * Make sure value object is immutable.
153
     *
154
     * @param  string  $name
155
     * @param  mixed  $value
156
     *
157
     * @return void
158
     * @throws InvalidArgumentException
159
     */
160 12
    public function __set(string $name, mixed $value): void
161
    {
162 12
        throw new InvalidArgumentException(static::IMMUTABLE_MESSAGE);
163
    }
164
}
165