Skip to content

Commit 8d6882c

Browse files
committed
MatchFilterForLeadTrait fixes
1 parent 50f3615 commit 8d6882c

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

Polyfill/EventListener/MatchFilterForLeadTrait.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace MauticPlugin\CustomObjectsBundle\Polyfill\EventListener;
44

55
use Mautic\LeadBundle\Entity\LeadListRepository;
6+
use Mautic\LeadBundle\Exception\OperatorsNotFoundException;
7+
use Mautic\LeadBundle\Segment\OperatorOptions;
68

79
/**
810
* Polyfill for \Mautic\EmailBundle\EventListener\MatchFilterForLeadTrait.
@@ -13,6 +15,176 @@ trait MatchFilterForLeadTrait
1315

1416
private LeadListRepository $segmentRepository;
1517

18+
/**
19+
* @param mixed[] $filter
20+
* @param mixed[] $lead
21+
*/
22+
protected function matchFilterForLead(array $filter, array $lead): bool
23+
{
24+
if (empty($lead['id'])) {
25+
// Lead in generated for preview with faked data
26+
return false;
27+
}
28+
29+
$groups = [];
30+
$groupNum = 0;
31+
32+
foreach ($filter as $data) {
33+
if (!array_key_exists($data['field'], $lead)) {
34+
continue;
35+
}
36+
37+
/*
38+
* Split the filters into groups based on the glue.
39+
* The first filter and any filters whose glue is
40+
* "or" will start a new group.
41+
*/
42+
if (0 === $groupNum || 'or' === $data['glue']) {
43+
++$groupNum;
44+
$groups[$groupNum] = null;
45+
}
46+
47+
/*
48+
* If the group has been marked as false, there
49+
* is no need to continue checking the others
50+
* in the group.
51+
*/
52+
if (false === $groups[$groupNum]) {
53+
continue;
54+
}
55+
56+
/*
57+
* If we are checking the first filter in a group
58+
* assume that the group will not match.
59+
*/
60+
if (null === $groups[$groupNum]) {
61+
$groups[$groupNum] = false;
62+
}
63+
64+
$leadValues = $lead[$data['field']];
65+
$leadValues = 'custom_object' === $data['object'] ? $leadValues : [$leadValues];
66+
$filterVal = $data['filter'];
67+
$subgroup = null;
68+
69+
if (is_array($leadValues)) {
70+
foreach ($leadValues as $leadVal) {
71+
if ($subgroup) {
72+
break;
73+
}
74+
75+
switch ($data['type']) {
76+
case 'boolean':
77+
if (null !== $leadVal) {
78+
$leadVal = (bool) $leadVal;
79+
}
80+
81+
if (null !== $filterVal) {
82+
$filterVal = (bool) $filterVal;
83+
}
84+
break;
85+
case 'datetime':
86+
case 'time':
87+
if (!is_null($leadVal) && !is_null($filterVal)) {
88+
$leadValCount = substr_count($leadVal, ':');
89+
$filterValCount = substr_count($filterVal, ':');
90+
91+
if (2 === $leadValCount && 1 === $filterValCount) {
92+
$filterVal .= ':00';
93+
}
94+
}
95+
break;
96+
case 'tags':
97+
case 'select':
98+
case 'multiselect':
99+
if (!is_array($leadVal) && !empty($leadVal)) {
100+
$leadVal = explode('|', $leadVal);
101+
}
102+
if (!is_null($filterVal) && !is_array($filterVal)) {
103+
$filterVal = explode('|', $filterVal);
104+
}
105+
break;
106+
case 'number':
107+
$leadVal = (int) $leadVal;
108+
$filterVal = (int) $filterVal;
109+
break;
110+
}
111+
112+
switch ($data['operator']) {
113+
case '=':
114+
if ('boolean' === $data['type']) {
115+
$groups[$groupNum] = $leadVal === $filterVal;
116+
} else {
117+
$groups[$groupNum] = $leadVal == $filterVal;
118+
}
119+
break;
120+
case '!=':
121+
if ('boolean' === $data['type']) {
122+
$groups[$groupNum] = $leadVal !== $filterVal;
123+
} else {
124+
$groups[$groupNum] = $leadVal != $filterVal;
125+
}
126+
break;
127+
case 'gt':
128+
$groups[$groupNum] = $leadVal > $filterVal;
129+
break;
130+
case 'gte':
131+
$groups[$groupNum] = $leadVal >= $filterVal;
132+
break;
133+
case 'lt':
134+
$groups[$groupNum] = $leadVal < $filterVal;
135+
break;
136+
case 'lte':
137+
$groups[$groupNum] = $leadVal <= $filterVal;
138+
break;
139+
case 'empty':
140+
$groups[$groupNum] = empty($leadVal);
141+
break;
142+
case '!empty':
143+
$groups[$groupNum] = !empty($leadVal);
144+
break;
145+
case 'like':
146+
$matchVal = str_replace(['.', '*', '%'], ['\.', '\*', '.*'], $filterVal);
147+
$groups[$groupNum] = 1 === preg_match('/'.$matchVal.'/', $leadVal);
148+
break;
149+
case '!like':
150+
$matchVal = str_replace(['.', '*'], ['\.', '\*'], $filterVal);
151+
$matchVal = str_replace('%', '.*', $matchVal);
152+
$groups[$groupNum] = 1 !== preg_match('/'.$matchVal.'/', $leadVal);
153+
break;
154+
case OperatorOptions::IN:
155+
$groups[$groupNum] = $this->checkLeadValueIsInFilter($leadVal, $filterVal, false);
156+
break;
157+
case OperatorOptions::NOT_IN:
158+
$groups[$groupNum] = $this->checkLeadValueIsInFilter($leadVal, $filterVal, true);
159+
break;
160+
case 'regexp':
161+
$groups[$groupNum] = 1 === preg_match('/'.$filterVal.'/i', $leadVal);
162+
break;
163+
case '!regexp':
164+
$groups[$groupNum] = 1 !== preg_match('/'.$filterVal.'/i', $leadVal);
165+
break;
166+
case 'startsWith':
167+
$groups[$groupNum] = 0 === strncmp($leadVal, $filterVal, strlen($filterVal));
168+
break;
169+
case 'endsWith':
170+
$endOfString = substr($leadVal, strlen($leadVal) - strlen($filterVal));
171+
$groups[$groupNum] = 0 === strcmp($endOfString, $filterVal);
172+
break;
173+
case 'contains':
174+
$groups[$groupNum] = false !== strpos((string) $leadVal, (string) $filterVal);
175+
break;
176+
default:
177+
throw new OperatorsNotFoundException('Operator is not defined or invalid operator found.');
178+
}
179+
180+
$subgroup = $groups[$groupNum];
181+
}
182+
}
183+
}
184+
185+
return in_array(true, $groups);
186+
}
187+
16188
/**
17189
* @param mixed[] $data
18190
* @param mixed[] $lead

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ parameters:
2323
- Tests/Unit/Serializer/ApiNormalizerTest.php # PHPSTAN is confused because it exists only if API Platform does.
2424
- DataPersister/CustomItemDataPersister.php # PHPSTAN is confused because it exists only if API Platform does.
2525
- Tests/Unit/EventListener/SegmentFiltersChoicesGenerateSubscriberTest.php
26+
- Polyfill/EventListener/MatchFilterForLeadTrait.php
2627
dynamicConstantNames:
2728
- MAUTIC_ENV
2829
- MAUTIC_TABLE_PREFIX

0 commit comments

Comments
 (0)