generated from michael-rubel/laravel-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathText.php
81 lines (70 loc) · 1.94 KB
/
Text.php
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
<?php
declare(strict_types=1);
/**
* This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects)
*
* @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository
* @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/)
* @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT
*/
namespace MichaelRubel\ValueObjects\Collection\Primitive;
use Illuminate\Support\Stringable;
use InvalidArgumentException;
use MichaelRubel\ValueObjects\ValueObject;
/**
* "Text" object presenting the stringable values.
*
* @author Michael Rubél <michael@laravel.software>
*
* @template TKey of array-key
* @template TValue
*
* @method static static make(string|Stringable $value)
* @method static static from(string|Stringable $value)
* @method static static makeOrNull(string|Stringable|null $value)
*
* @extends ValueObject<TKey, TValue>
*/
class Text extends ValueObject
{
/**
* @var string|Stringable
*/
protected string|Stringable $value;
/**
* Create a new instance of the value object.
*
* @param string|Stringable $value
*/
public function __construct(string|Stringable $value)
{
if (isset($this->value)) {
throw new InvalidArgumentException(static::IMMUTABLE_MESSAGE);
}
$this->value = $value;
if (isset($this->before)) {
($this->before)();
}
$this->validate();
}
/**
* Get the object value.
*
* @return string
*/
public function value(): string
{
return (string) $this->value;
}
/**
* Validate the value object data.
*
* @return void
*/
protected function validate(): void
{
if ($this->value() === '') {
throw new InvalidArgumentException('Text cannot be empty.');
}
}
}