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