1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\String; |
6
|
|
|
|
7
|
|
|
// phpcs:disable |
8
|
|
|
|
9
|
|
|
use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder; |
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Interfaces\String\IsbnFieldInterface; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\MappingHelper; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Schema\Database; |
13
|
|
|
use Symfony\Component\Validator\Constraints\Isbn; |
14
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
15
|
|
|
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; |
16
|
|
|
use Symfony\Component\Validator\Exception\InvalidOptionsException; |
17
|
|
|
use Symfony\Component\Validator\Exception\MissingOptionsException; |
18
|
|
|
use Symfony\Component\Validator\Mapping\ClassMetadata as ValidatorClassMetaData; |
19
|
|
|
|
20
|
|
|
// phpcs:enable |
21
|
|
|
trait IsbnFieldTrait |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
private $isbn; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
31
|
|
|
* @param ClassMetadataBuilder $builder |
32
|
|
|
*/ |
33
|
|
|
public static function metaForIsbn(ClassMetadataBuilder $builder): void |
34
|
|
|
{ |
35
|
|
|
MappingHelper::setSimpleStringFields( |
36
|
|
|
[IsbnFieldInterface::PROP_ISBN], |
37
|
|
|
$builder, |
38
|
|
|
IsbnFieldInterface::DEFAULT_ISBN, |
39
|
|
|
true |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* This method sets the validation for this field. |
45
|
|
|
* |
46
|
|
|
* You should add in as many relevant property constraints as you see fit. |
47
|
|
|
* |
48
|
|
|
* @param ValidatorClassMetaData $metadata |
49
|
|
|
* |
50
|
|
|
* @throws MissingOptionsException |
51
|
|
|
* @throws InvalidOptionsException |
52
|
|
|
* @throws ConstraintDefinitionException |
53
|
|
|
*/ |
54
|
|
|
protected static function validatorMetaForPropertyIsbn(ValidatorClassMetaData $metadata): void |
55
|
|
|
{ |
56
|
|
|
$metadata->addPropertyConstraints( |
57
|
|
|
IsbnFieldInterface::PROP_ISBN, |
58
|
|
|
[ |
59
|
|
|
new Isbn(), |
60
|
|
|
new Length( |
61
|
|
|
[ |
62
|
|
|
'min' => 0, |
63
|
|
|
'max' => Database::MAX_VARCHAR_LENGTH, |
64
|
|
|
] |
65
|
|
|
), |
66
|
|
|
] |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string|null |
72
|
|
|
*/ |
73
|
|
|
public function getIsbn(): ?string |
74
|
|
|
{ |
75
|
|
|
if (null === $this->isbn) { |
76
|
|
|
return IsbnFieldInterface::DEFAULT_ISBN; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->isbn; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string|null $isbn |
84
|
|
|
* |
85
|
|
|
* @return self |
86
|
|
|
*/ |
87
|
|
|
private function setIsbn(?string $isbn): self |
88
|
|
|
{ |
89
|
|
|
$this->updatePropertyValue( |
|
|
|
|
90
|
|
|
IsbnFieldInterface::PROP_ISBN, |
91
|
|
|
$isbn |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|