-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathViolationFactory.php
More file actions
56 lines (46 loc) · 1.85 KB
/
ViolationFactory.php
File metadata and controls
56 lines (46 loc) · 1.85 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
<?php
declare(strict_types=1);
namespace Setono\SyliusFeedPlugin\Factory;
use Setono\SyliusFeedPlugin\Model\ViolationInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationInterface;
final class ViolationFactory implements ViolationFactoryInterface
{
public function __construct(private readonly FactoryInterface $decoratedFactory)
{
}
public function createNew(): ViolationInterface
{
/** @var ViolationInterface $violation */
$violation = $this->decoratedFactory->createNew();
return $violation;
}
/**
* @param mixed|null $data
*/
public function createFromConstraintViolation(
ConstraintViolationInterface $constraintViolation,
ChannelInterface $channel,
LocaleInterface $locale,
$data = null,
): ViolationInterface {
$violation = $this->createNew();
$violation->setChannel($channel);
$violation->setLocale($locale);
$violation->setMessage(
/** @phpstan-ignore cast.string */
$constraintViolation->getPropertyPath() . ': ' . sprintf((string) $constraintViolation->getMessage(), (string) $constraintViolation->getInvalidValue()),
);
$violation->setData($data);
if ($constraintViolation instanceof ConstraintViolation) {
$constraint = $constraintViolation->getConstraint();
if (null !== $constraint && is_array($constraint->payload) && isset($constraint->payload['severity']) && is_string($constraint->payload['severity'])) {
$violation->setSeverity($constraint->payload['severity']);
}
}
return $violation;
}
}