Passed
Pull Request — main (#4)
by Michael
05:15 queued 01:31
created

ValueObject::equals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 2
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\ValueObjects;
6
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Support\Traits\Conditionable;
9
use Illuminate\Support\Traits\Macroable;
10
use InvalidArgumentException;
11
12
/**
13
 * @template TKey of array-key
14
 * @template TValue
15
 *
16
 * @implements Arrayable<TKey, TValue>
17
 */
18
abstract class ValueObject implements Arrayable
19
{
20
    use Macroable, Conditionable;
21
22
    /**
23
     * Get the object value.
24
     *
25
     * @return mixed
26
     */
27
    abstract public function value();
28
29
    /**
30
     * Convenient method to create a value object statically.
31
     *
32
     * @param  mixed  $values
33
     *
34
     * @return static
35
     */
36 9
    public static function make(mixed ...$values): static
37
    {
38 9
        return new static(...$values);
39
    }
40
41
    /**
42
     * Convenient method to create a value object statically.
43
     *
44
     * @param  mixed  $values
45
     *
46
     * @return static
47
     */
48 8
    public static function from(mixed ...$values): static
49
    {
50 8
        return static::make(...$values);
51
    }
52
53
    /**
54
     * Create a value object or return null.
55
     *
56
     * @param  mixed  $values
57
     *
58
     * @return static|null
59
     */
60 1
    public static function makeOrNull(mixed ...$values): static|null
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '|', expecting '{' or ';' on line 60 at column 63
Loading history...
61
    {
62 1
        return rescue(fn () => static::make(...$values), report: false);
63
    }
64
65
    /**
66
     * Check if objects are instances of same class
67
     * and share the same properties and values.
68
     *
69
     * @param  ValueObject<int|string, mixed>  $object
70
     *
71
     * @return bool
72
     */
73 3
    public function equals(ValueObject $object): bool
74
    {
75 3
        return $this == $object;
76
    }
77
78
    /**
79
     * Inversion for `equals` method.
80
     *
81
     * @param  ValueObject<int|string, mixed>  $object
82
     *
83
     * @return bool
84
     */
85 2
    public function notEquals(ValueObject $object): bool
86
    {
87 2
        return ! $this->equals($object);
88
    }
89
90
    /**
91
     * Get an array representation of the value object.
92
     *
93
     * @return array
94
     */
95 5
    public function toArray(): array
96
    {
97 5
        return (array) $this->value();
98
    }
99
100
    /**
101
     * Get string representation of the value object.
102
     *
103
     * @return string
104
     */
105 13
    public function __toString(): string
106
    {
107 13
        return (string) $this->value();
108
    }
109
110
    /**
111
     * Get the internal property value.
112
     *
113
     * @param  string  $name
114
     *
115
     * @return mixed
116
     */
117 7
    public function __get(string $name): mixed
118
    {
119 7
        return $this->{$name};
120
    }
121
122
    /**
123
     * Make sure value object is immutable.
124
     *
125
     * @param  string  $name
126
     * @param  mixed  $value
127
     *
128
     * @return void
129
     */
130 7
    public function __set(string $name, mixed $value): void
131
    {
132 7
        throw new InvalidArgumentException('Value objects are immutable, you cannot modify properties. Create a new object instead.');
133
    }
134
}
135