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