File size: 2,044 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

declare(strict_types=1);

namespace Mautic\LeadBundle\Validator\Constraints;

use Mautic\LeadBundle\Segment\ContactSegmentFilterFactory;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Contracts\Translation\TranslatorInterface;

final class SegmentDateValidator extends ConstraintValidator
{
    public function __construct(
        private ContactSegmentFilterFactory $contactSegmentFilterFactory,
        private TranslatorInterface $translator
    ) {
    }

    /**
     * @param array<mixed> $filters
     */
    public function validate($filters, Constraint $constraint): void
    {
        foreach ($filters as $filter) {
            if (isset($filter['type']) && in_array($filter['type'], ['date', 'datetime'])) {
                $segmentFilter  = $this->contactSegmentFilterFactory->factorSegmentFilter($filter);
                $parameterValue = $segmentFilter->getParameterValue();

                if (is_array($parameterValue)) {
                    continue;
                }

                if (in_array($filter['operator'] ?? '', ['regexp', '!regexp', 'like', '!like', 'startsWith', 'endsWith', 'contains'])) {
                    continue;
                }

                if (null === $parameterValue) {
                    continue;
                }

                if (str_contains($parameterValue, '%')) {
                    return;
                }

                $formats  = ['Y-m-d', 'Y-m-d H:i', 'Y-m-d H:i:s'];

                foreach ($formats as $fmt) {
                    $dateTime = \DateTime::createFromFormat($fmt, $parameterValue);
                    if (false !== $dateTime) {
                        break;
                    }
                }

                if (false === $dateTime) {
                    $this->context->addViolation($this->translator->trans('mautic.lead.segment.date_invalid', ['%value%' => $parameterValue], 'validators'));

                    return;
                }
            }
        }
    }
}