|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String; |
|
4
|
|
|
|
|
5
|
|
|
// phpcs:disable |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
|
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\UrlFieldInterface; |
|
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\Database; |
|
11
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
|
12
|
|
|
use Symfony\Component\Validator\Constraints\Url; |
|
13
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
|
14
|
|
|
|
|
15
|
|
|
// phpcs:enable |
|
16
|
|
|
trait UrlFieldTrait |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string|null |
|
21
|
|
|
*/ |
|
22
|
|
|
private $url; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
26
|
|
|
* @param ClassMetadataBuilder $builder |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function metaForUrl(ClassMetadataBuilder $builder): void |
|
29
|
|
|
{ |
|
30
|
|
|
MappingHelper::setSimpleStringFields( |
|
31
|
|
|
[UrlFieldInterface::PROP_URL], |
|
32
|
|
|
$builder, |
|
33
|
|
|
UrlFieldInterface::DEFAULT_URL, |
|
34
|
|
|
false |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* This method validates that the url is valid |
|
40
|
|
|
* |
|
41
|
|
|
* It allows the protocol to be ommitted, eg //www.edmondscommerce.co.uk |
|
42
|
|
|
* |
|
43
|
|
|
* You can extend the list of allowed protocols as you see fit |
|
44
|
|
|
* |
|
45
|
|
|
* @param ValidatorClassMetaData $metadata |
|
46
|
|
|
*/ |
|
47
|
|
|
protected static function validatorMetaForPropertyUrl(ValidatorClassMetaData $metadata): void |
|
48
|
|
|
{ |
|
49
|
|
|
$metadata->addPropertyConstraints( |
|
50
|
|
|
UrlFieldInterface::PROP_URL, |
|
51
|
|
|
[ |
|
52
|
|
|
new Url([ |
|
53
|
|
|
'relativeProtocol' => true, |
|
54
|
|
|
'protocols' => ['http', 'https'], |
|
55
|
|
|
]), |
|
56
|
|
|
new Length( |
|
57
|
|
|
[ |
|
58
|
|
|
'min' => 1, |
|
59
|
|
|
'max' => Database::MAX_VARCHAR_LENGTH, |
|
60
|
|
|
] |
|
61
|
|
|
), |
|
62
|
|
|
] |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return string|null |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getUrl(): ?string |
|
70
|
|
|
{ |
|
71
|
|
|
if (null === $this->url) { |
|
72
|
|
|
return UrlFieldInterface::DEFAULT_URL; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->url; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param string|null $url |
|
80
|
|
|
* |
|
81
|
|
|
* @return self |
|
82
|
|
|
*/ |
|
83
|
|
|
private function setUrl(?string $url): self |
|
84
|
|
|
{ |
|
85
|
|
|
$this->updatePropertyValue( |
|
|
|
|
|
|
86
|
|
|
UrlFieldInterface::PROP_URL, |
|
87
|
|
|
$url |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|