Passed
Pull Request — main (#2)
by Michael
03:25
created

Text::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
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\Collection\Primitive;
14
15
use Illuminate\Support\Stringable;
16
use InvalidArgumentException;
17
use MichaelRubel\ValueObjects\ValueObject;
18
19
/**
20
 * "Text" object presenting the stringable values.
21
 *
22
 * @author Michael Rubél <[email protected]>
23
 *
24
 * @template TKey of array-key
25
 * @template TValue
26
 *
27
 * @method static static make(string|Stringable $value)
28
 * @method static static from(string|Stringable $value)
29
 *
30
 * @extends ValueObject<TKey, TValue>
31
 */
32
class Text extends ValueObject
33
{
34
    /**
35
     * Create a new instance of the value object.
36
     *
37
     * @param  string|Stringable  $value
38
     */
39 13
    public function __construct(protected string|Stringable $value)
40
    {
41 13
        $this->validate();
42
    }
43
44
    /**
45
     * Get the object value.
46
     *
47
     * @return string
48
     */
49 13
    public function value(): string
50
    {
51 13
        return (string) $this->value;
52
    }
53
54
    /**
55
     * Validate the value object data.
56
     *
57
     * @return void
58
     */
59 13
    protected function validate(): void
60
    {
61 13
        if (empty($this->value())) {
62 2
            throw new InvalidArgumentException('Text cannot be empty.');
63
        }
64
    }
65
}
66