Snippets
Created by
Andrew Stark
last modified
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | <?php
declare(strict_types=1);
/*
* Created by BonBonSlick
*/
namespace App\Infrastructure\Validation\Request\PublicApi\Secured\Update;
use App\Domain\Core\Exceptions\BadParameterException;
use App\Domain\Core\Exceptions\WrongInstanceOfClassException;
use App\Domain\Core\ValueObjects\String\Description;
use App\Domain\Core\ValueObjects\String\Slug;
use App\Domain\Internationalization\Country\Filter\CountryFilter;
use App\Domain\Internationalization\Language\Filter\LanguageFilter;
use App\Domain\UserPack\Email\Entity\UserEmail;
use App\Domain\UserPack\Email\Filter\UserEmailFilter;
use App\Domain\UserPack\User\Entity\User;
use App\Domain\UserPack\User\Filter\UserFilter;
use App\Infrastructure\Traits\Helper\StringHelperTrait;
use App\Infrastructure\Validation\Constraints\Compound\EmailCompound;
use App\Infrastructure\Validation\Constraints\Compound\NicknameCompound;
use App\Infrastructure\Validation\Constraints\Compound\NonEmptyStringCompound;
use App\Infrastructure\Validation\Constraints\UniqueEntityConstraint;
use App\Infrastructure\Validation\Constraints\ValueExistsConstraint;
use App\Infrastructure\Validation\Request\AbstractValidationRequest;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Country;
use Symfony\Component\Validator\Constraints\Language;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\Sequentially;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
class UpdateUserAccountRequest extends AbstractValidationRequest {
use StringHelperTrait;
private ?string $slug;
private ?string $nickname;
private ?string $description;
private ?string $languageAlpha2;
private ?string $countryAlpha2;
private ?string $email;
private ?string $fallbackEmail;
public function __construct(Request $request) {
parent::__construct($request);
$this->slug = (string)$request->request->get('slug');
$this->nickname = (string)$request->request->get('nickname');
$this->description = (string)$request->request->get('description');
$this->languageAlpha2 = $this->lower((string)$request->request->get('languageAlpha2'));
$this->countryAlpha2 = $this->upper((string)$request->request->get('countryAlpha2'));
$this->email = $this->lower((string)$request->request->get('email'));
$this->fallbackEmail = $this->lower((string)$request->request->get('fallbackEmail'));
}
public static function loadValidatorMetadata(ClassMetadataInterface $classMetadata): void {
// added validation here cant be executed conditionally based on passed values
$classMetadata->addConstraint(new Callback(['callback' => 'validate']));
}
/**
* @throws WrongInstanceOfClassException
* @throws BadParameterException
*/
public function validate(ExecutionContextInterface $context): void {
/** @var ClassMetadataInterface $classMetadata */
$classMetadata = $context->getMetadata();
if (false === $classMetadata instanceof ClassMetadataInterface) {
throw new WrongInstanceOfClassException();
}
if (false === empty($this->slug())) {
$classMetadata->addPropertyConstraints(
'slug',
[
new Sequentially(
[
new NonEmptyStringCompound(),
new Length(['min' => Slug::LENGTH_MIN, 'max' => Slug::LENGTH_MAX]),
new Regex(
[
'pattern' => '/[a-z0-9]+/',
'message' => 'required.alphanumeric.values.123',
]
),
new UniqueEntityConstraint(
[
'entityClass' => User::class,
'filter' => (new UserFilter())->setSlug($this->slug()),
]
),
]
),
]
);
}
if (false === empty($this->nickname())) {
$classMetadata->addPropertyConstraints(
'nickname',
[
new NicknameCompound(),
new UniqueEntityConstraint(
[
'entityClass' => User::class,
'filter' => (new UserFilter())->setNickname($this->nickname()),
]
),
]
);
}
if (false === empty($this->description())) {
$classMetadata->addPropertyConstraints(
'description',
[
new Sequentially(
[
new NonEmptyStringCompound(),
new Length(['max' => Description::LENGTH_MAX]),
]
),
]
);
}
if (false === empty($this->languageAlpha2())) {
$classMetadata->addPropertyConstraints(
'languageAlpha2',
[
new Sequentially(
[
new NonEmptyStringCompound(),
new Length(['min' => 2, 'max' => 3]),
new Language(),
new ValueExistsConstraint(
[
'entityClass' => \App\Domain\Internationalization\Language\Entity\Language::class,
'filter' => (new LanguageFilter())->setAlpha2($this->languageAlpha2()),
]
),
]
),
]
);
}
if (false === empty($this->countryAlpha2())) {
$classMetadata->addPropertyConstraints(
'countryAlpha2',
[
new Sequentially(
[
new NonEmptyStringCompound(),
new Length(['min' => 2, 'max' => 3]),
new Country(),
new ValueExistsConstraint(
[
'entityClass' => \App\Domain\Internationalization\Country\Entity\Country::class,
'filter' => (new CountryFilter())->setAlpha2($this->countryAlpha2())
,
]
),
]
),
]
);
}
if (false === empty($this->email())) {
$classMetadata->addPropertyConstraints(
'email',
[
new Sequentially(
[
new NonEmptyStringCompound(),
new EmailCompound(),
new UniqueEntityConstraint(
[
'entityClass' => UserEmail::class,
'filter' => (new UserEmailFilter())
->setEmail($this->email())
->setIsActive(true),
]
),
],
),
]
);
}
// @todo - fallback email update
}
public function slug(): ?string {
return $this->slug;
}
public function nickname(): ?string {
return $this->nickname;
}
public function description(): ?string {
return $this->description;
}
public function languageAlpha2(): ?string {
return $this->languageAlpha2;
}
public function countryAlpha2(): ?string {
return $this->lower($this->countryAlpha2);
}
public function email(): ?string {
return $this->email;
}
public function fallbackEmail(): ?string {
return $this->fallbackEmail;
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.