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\NullableStringFieldInterface; |
9
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
10
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
11
|
|
|
|
12
|
|
|
// phpcs:enable |
13
|
|
|
trait NullableStringFieldTrait |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string|null |
18
|
|
|
*/ |
19
|
|
|
private $nullableString; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
23
|
|
|
*/ |
24
|
2 |
|
public static function metaForNullableString(ClassMetadataBuilder $builder): void |
25
|
|
|
{ |
26
|
2 |
|
MappingHelper::setSimpleStringFields( |
27
|
2 |
|
[NullableStringFieldInterface::PROP_NULLABLE_STRING], |
28
|
2 |
|
$builder, |
29
|
2 |
|
NullableStringFieldInterface::DEFAULT_NULLABLE_STRING, |
30
|
2 |
|
false |
31
|
|
|
); |
32
|
2 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* This method sets the validation for this field. |
36
|
|
|
* |
37
|
|
|
* You should add in as many relevant property constraints as you see fit. |
38
|
|
|
* |
39
|
|
|
* Remove the PHPMD suppressed warning once you start setting constraints |
40
|
|
|
* |
41
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
42
|
|
|
* @see https://symfony.com/doc/current/validation.html#supported-constraints |
43
|
|
|
* |
44
|
|
|
* @param ValidatorClassMetaData $metadata |
45
|
|
|
* |
46
|
|
|
* @throws \Symfony\Component\Validator\Exception\MissingOptionsException |
47
|
|
|
* @throws \Symfony\Component\Validator\Exception\InvalidOptionsException |
48
|
|
|
* @throws \Symfony\Component\Validator\Exception\ConstraintDefinitionException |
49
|
|
|
*/ |
50
|
2 |
|
protected static function validatorMetaForPropertyNullableString(ValidatorClassMetaData $metadata): void |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
// $metadata->addPropertyConstraint( |
53
|
|
|
// NullableStringFieldInterface::PROP_NULLABLE_STRING, |
54
|
|
|
// new NotBlank() |
55
|
|
|
// ); |
56
|
2 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return string|null |
60
|
|
|
*/ |
61
|
2 |
|
public function getNullableString(): ?string |
62
|
|
|
{ |
63
|
2 |
|
if (null === $this->nullableString) { |
64
|
2 |
|
return NullableStringFieldInterface::DEFAULT_NULLABLE_STRING; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->nullableString; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string|null $nullableString |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
|
|
private function setNullableString(?string $nullableString): self |
76
|
|
|
{ |
77
|
|
|
$this->updatePropertyValue( |
|
|
|
|
78
|
|
|
NullableStringFieldInterface::PROP_NULLABLE_STRING, |
79
|
|
|
$nullableString |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.