Passed
Push — main ( 9f60de...d94bc7 )
by Michael
07:22 queued 03:29
created

Url::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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) 2023 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\Facades\Validator;
16
use Illuminate\Validation\ValidationException;
17
use MichaelRubel\ValueObjects\Collection\Primitive\Text;
18
19
/**
20
 * "Url" object presenting a URL.
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 $value)
28
 * @method static static from(string $value)
29
 * @method static static makeOrNull(string|null $value)
30
 *
31
 * @extends Text<TKey, TValue>
32
 */
33
class Url extends Text
34
{
35
    /**
36
     * Create a new instance of the value object.
37
     *
38
     * @param  string  $value
39
     */
40 15
    public function __construct(string $value)
41
    {
42 15
        parent::__construct($value);
43
44 14
        $this->value = url($value);
45
46 14
        $validator = Validator::make(
47 14
            ['url' => $this->value()],
48 14
            ['url' => $this->validationRules()],
49 14
        );
50
51 14
        if ($validator->fails()) {
52 2
            throw ValidationException::withMessages([__('Your URL is invalid.')]);
53
        }
54
    }
55
56
    /**
57
     * Define the rules for email validator.
58
     *
59
     * @return array
60
     */
61 14
    protected function validationRules(): array
62
    {
63 14
        return ['required', 'url'];
64
    }
65
}
66