-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAdminAttributeDefinitionRequest.php
More file actions
57 lines (45 loc) · 1.67 KB
/
AdminAttributeDefinitionRequest.php
File metadata and controls
57 lines (45 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace PhpList\RestBundle\Identity\Request;
use PhpList\Core\Domain\Identity\Model\Dto\AdminAttributeDefinitionDto;
use PhpList\Core\Domain\Subscription\Validator\AttributeTypeValidator;
use PhpList\RestBundle\Common\Request\RequestInterface;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Exception\ValidatorException;
#[Assert\Callback('validateType')]
class AdminAttributeDefinitionRequest implements RequestInterface
{
#[Assert\NotBlank]
public string $name;
#[Assert\Choice(choices: ['hidden', 'textline'], message: 'Invalid type. Allowed values: hidden, textline.')]
public ?string $type = null;
public ?int $order = null;
public ?string $defaultValue = null;
public bool $required = false;
public function getDto(): AdminAttributeDefinitionDto
{
return new AdminAttributeDefinitionDto(
name: $this->name,
type: $this->type,
listOrder: $this->order,
defaultValue: $this->defaultValue,
required: $this->required,
);
}
public function validateType(ExecutionContextInterface $context): void
{
if ($this->type === null) {
return;
}
$validator = new AttributeTypeValidator(new IdentityTranslator());
try {
$validator->validate($this->type);
} catch (ValidatorException $e) {
$context->buildViolation($e->getMessage())
->atPath('type')
->addViolation();
}
}
}