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

ClassString::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
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 16
    public function __construct(protected string $string)
38
    {
39 16
        $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 2
    public function instantiate(array $parameters = []): object
70
    {
71 2
        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 12
    public function value(): string
92
    {
93 12
        return $this->string;
94
    }
95
96
    /**
97
     * Validate the value object data.
98
     *
99
     * @return void
100
     */
101 16
    protected function validate(): void
102
    {
103 16
        if (blank($this->string)) {
104 1
            throw new \InvalidArgumentException('Class string cannot be blank.');
105
        }
106
    }
107
}
108