ClassString   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
eloc 13
dl 0
loc 86
ccs 18
cts 18
cp 1
rs 10
c 2
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A classExists() 0 3 1
A __construct() 0 9 2
A validate() 0 4 2
A instantiate() 0 3 1
A instantiateWith() 0 3 1
A interfaceExists() 0 3 1
A value() 0 3 1
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 Illuminate\Validation\ValidationException;
16
use InvalidArgumentException;
17
use MichaelRubel\ValueObjects\ValueObject;
18
19
/**
20
 * "ClassString" object presenting a class string.
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 $string)
28
 * @method static static from(string $string)
29
 * @method static static makeOrNull(string|null $string)
30
 *
31
 * @extends ValueObject<TKey, TValue>
32
 */
33
class ClassString extends ValueObject
34
{
35
    /**
36
     * @var string
37
     */
38
    protected string $string;
39
40
    /**
41
     * Create a new instance of the value object.
42
     *
43
     * @param  string  $string
44
     */
45 19
    public function __construct(string $string)
46
    {
47 19
        if (isset($this->string)) {
48 1
            throw new InvalidArgumentException(static::IMMUTABLE_MESSAGE);
49
        }
50
51 19
        $this->string = $string;
52
53 19
        $this->validate();
54
    }
55
56
    /**
57
     * Determine if the class exists for this class string.
58
     *
59
     * @return bool
60
     */
61 4
    public function classExists(): bool
62
    {
63 4
        return class_exists($this->value());
64
    }
65
66
    /**
67
     * Determine if the interface exists for this class string.
68
     *
69
     * @return bool
70
     */
71 4
    public function interfaceExists(): bool
72
    {
73 4
        return interface_exists($this->value());
74
    }
75
76
    /**
77
     * Instantiate the class string if possible.
78
     *
79
     * @param  array  $parameters
80
     *
81
     * @return object
82
     */
83 2
    public function instantiate(array $parameters = []): object
84
    {
85 2
        return app($this->value(), $parameters);
86
    }
87
88
    /**
89
     * Instantiate the class string if possible.
90
     *
91
     * @param  array  $parameters
92
     *
93
     * @return object
94
     */
95 1
    public function instantiateWith(array $parameters): object
96
    {
97 1
        return $this->instantiate($parameters);
98
    }
99
100
    /**
101
     * Get the object value.
102
     *
103
     * @return string
104
     */
105 19
    public function value(): string
106
    {
107 19
        return $this->string;
108
    }
109
110
    /**
111
     * Validate the value object data.
112
     *
113
     * @return void
114
     */
115 19
    protected function validate(): void
116
    {
117 19
        if ($this->value() === '') {
118 2
            throw ValidationException::withMessages(['Class string cannot be empty.']);
119
        }
120
    }
121
}
122