|
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\Facades\Validator; |
|
16
|
|
|
use Illuminate\Support\Stringable; |
|
17
|
|
|
use Illuminate\Validation\ValidationException; |
|
18
|
|
|
use MichaelRubel\ValueObjects\Collection\Primitive\Text; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* "Url" object presenting a URL. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Michael Rubél <[email protected]> |
|
24
|
|
|
* |
|
25
|
|
|
* @template TKey of array-key |
|
26
|
|
|
* @template TValue |
|
27
|
|
|
* |
|
28
|
|
|
* @method static static make(string|Stringable $value) |
|
29
|
|
|
* @method static static from(string|Stringable $value) |
|
30
|
|
|
* @method static static makeOrNull(string|Stringable|null $value) |
|
31
|
|
|
* |
|
32
|
|
|
* @extends Text<TKey, TValue> |
|
33
|
|
|
*/ |
|
34
|
|
|
class Url extends Text |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Create a new instance of the value object. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string|Stringable $value |
|
40
|
|
|
*/ |
|
41
|
16 |
|
public function __construct(string|Stringable $value) |
|
42
|
|
|
{ |
|
43
|
16 |
|
parent::__construct($value); |
|
44
|
|
|
|
|
45
|
14 |
|
$this->value = url($value); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
14 |
|
$validator = Validator::make( |
|
48
|
14 |
|
['url' => $this->value()], |
|
49
|
14 |
|
['url' => $this->validationRules()], |
|
50
|
14 |
|
); |
|
51
|
|
|
|
|
52
|
14 |
|
if ($validator->fails()) { |
|
53
|
2 |
|
throw ValidationException::withMessages([__('Your URL is invalid.')]); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Define the rules for email validator. |
|
59
|
|
|
* |
|
60
|
|
|
* @return array |
|
61
|
|
|
*/ |
|
62
|
14 |
|
protected function validationRules(): array |
|
63
|
|
|
{ |
|
64
|
14 |
|
return ['required', 'url']; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.