Passed
Pull Request — main (#5)
by Michael
05:54 queued 02:27
created

Name   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sanitize() 0 7 1
A __construct() 0 5 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\Support\Stringable;
16
use MichaelRubel\ValueObjects\Collection\Primitive\Text;
17
18
/**
19
 * "Name" object presenting a generic name.
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|Stringable $value)
27
 * @method static static from(string|Stringable $value)
28
 * @method static static makeOrNull(string|Stringable $value)
29
 *
30
 * @extends Text<TKey, TValue>
31
 */
32
class Name extends Text
33
{
34
    /**
35
     * Create a new instance of the value object.
36
     *
37
     * @param  string|Stringable  $value
38
     */
39 9
    public function __construct(protected string|Stringable $value)
40
    {
41 9
        parent::__construct($this->value);
42
43 7
        $this->sanitize();
44
    }
45
46
    /**
47
     * Sanitize the value.
48
     *
49
     * @return void
50
     */
51 7
    protected function sanitize(): void
52
    {
53 7
        $this->value = str($this->value())
54 7
            ->replaceMatches('/\p{C}+/u', '')
55 7
            ->replace(['\r', '\n', '\t'], '')
56 7
            ->squish()
57 7
            ->value();
58
    }
59
}
60