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\Complex; |
14
|
|
|
|
15
|
|
|
use MichaelRubel\ValueObjects\ValueObject; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* "ClassString" object presenting a class string. |
19
|
|
|
* |
20
|
|
|
* @author Michael Rubél <[email protected]> |
21
|
|
|
* |
22
|
|
|
* @template TKey of array-key |
23
|
|
|
* @template TValue |
24
|
|
|
* |
25
|
|
|
* @method static static make(string|null $string) |
26
|
|
|
* @method static static from(string|null $string) |
27
|
|
|
* |
28
|
|
|
* @extends ValueObject<TKey, TValue> |
29
|
|
|
*/ |
30
|
|
|
class ClassString extends ValueObject |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Create a new instance of the value object. |
34
|
|
|
* |
35
|
|
|
* @param string $string |
36
|
|
|
*/ |
37
|
15 |
|
public function __construct(protected string $string) |
38
|
|
|
{ |
39
|
15 |
|
$this->validate(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Determine if the class exists for this class string. |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
4 |
|
public function classExists(): bool |
48
|
|
|
{ |
49
|
4 |
|
return class_exists($this->value()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Determine if the interface exists for this class string. |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
4 |
|
public function interfaceExists(): bool |
58
|
|
|
{ |
59
|
4 |
|
return interface_exists($this->value()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Instantiate the class string if possible. |
64
|
|
|
* |
65
|
|
|
* @param array $parameters |
66
|
|
|
* |
67
|
|
|
* @return object |
68
|
|
|
*/ |
69
|
1 |
|
public function instantiate(array $parameters = []): object |
70
|
|
|
{ |
71
|
1 |
|
return app($this->value(), $parameters); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Instantiate the class string if possible. |
76
|
|
|
* |
77
|
|
|
* @param array $parameters |
78
|
|
|
* |
79
|
|
|
* @return object |
80
|
|
|
*/ |
81
|
1 |
|
public function instantiateWith(array $parameters): object |
82
|
|
|
{ |
83
|
1 |
|
return $this->instantiate($parameters); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the object value. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
11 |
|
public function value(): string |
92
|
|
|
{ |
93
|
11 |
|
return $this->string; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Validate the value object data. |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
*/ |
101
|
15 |
|
protected function validate(): void |
102
|
|
|
{ |
103
|
15 |
|
if (blank($this->string)) { |
104
|
1 |
|
throw new \InvalidArgumentException('Class string cannot be blank.'); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|