Skip to content

Commit 88bbcf2

Browse files
committed
feat: Replace conditions evaluation with new implementation
1 parent eafe281 commit 88bbcf2

5 files changed

Lines changed: 21 additions & 287 deletions

File tree

src/Conditions.php

Lines changed: 0 additions & 196 deletions
This file was deleted.

src/Datafile/Conditions.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ final class Conditions implements ConditionInterface
1313
{
1414
private ConditionInterface $expression;
1515

16+
/**
17+
* @param string|list<array<string, list<array{attribute: string, operator: string, value?: mixed, regexFlags?: string}>>|array{attribute: string, operator: string, value?: mixed, regexFlags?: string}> $conditions
18+
* @return self
19+
*/
1620
public static function createFromMixed($conditions): self
1721
{
1822
if ($conditions === '*') {

src/Datafile/Content.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ final class Content
1919
*/
2020
public static function createFromPath(string $path): self
2121
{
22-
if (file_exists($path)) {
23-
$content = file_get_contents($path);
22+
if (file_exists($path) === false) {
23+
throw new \InvalidArgumentException("File '$path' not found");
2424
}
2525

26-
return self::createFromArray(json_decode($content, true, 512, JSON_THROW_ON_ERROR));
26+
return self::createFromJson(file_get_contents($path));
27+
}
28+
29+
public static function createFromJson(string $json): self
30+
{
31+
return self::createFromArray(json_decode($json, true, 512, JSON_THROW_ON_ERROR));
2732
}
2833

2934
/**
@@ -35,7 +40,7 @@ public static function createFromPath(string $path): self
3540
* }>
3641
* } $data
3742
*/
38-
private static function createFromArray(array $data): self
43+
public static function createFromArray(array $data): self
3944
{
4045
return new self(
4146
$data['schemaVersion'],

src/Datafile/Segment.php

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
final class Segment
1111
{
1212
private bool $archived;
13-
/**
14-
* @var Condition|array<Condition>|string
15-
*/
16-
private $conditions; // Can be Condition, List<Condition>, or String
13+
private Conditions $conditions;
1714
private string $description;
1815

1916
/**
2017
* @param array<array{
2118
* archived: bool,
22-
* conditions: array{}|list<array{}>|string,
19+
* conditions: list<array<string, mixed>>|string,
2320
* description: string
2421
* }> $segments
2522
* @return array<Segment>
@@ -35,7 +32,7 @@ public static function createManyFromArray(array $segments): array
3532
/**
3633
* @param array{
3734
* archived: bool,
38-
* conditions: array{}|list<array{}>|string,
35+
* conditions: list<array<string, mixed>>|string,
3936
* description: string
4037
* } $data
4138
*/
@@ -50,7 +47,7 @@ public static function createFromArray(array $data): self
5047

5148
public function __construct(
5249
string $description,
53-
ConditionInterface $conditions,
50+
Conditions $conditions,
5451
bool $archived = false
5552
)
5653
{
@@ -64,15 +61,7 @@ public function isArchived(): bool
6461
return $this->archived;
6562
}
6663

67-
public function getKey(): string
68-
{
69-
return $this->key;
70-
}
71-
72-
/**
73-
* @return Condition|array<Condition>|string
74-
*/
75-
public function getConditions()
64+
public function getConditions(): Conditions
7665
{
7766
return $this->conditions;
7867
}

src/DatafileReader.php

Lines changed: 3 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Featurevisor;
44

5+
use Featurevisor\Datafile\Conditions;
56
use Psr\Log\LoggerInterface;
67

78
class DatafileReader
@@ -93,77 +94,7 @@ public function getRegex(string $regexString, string $regexFlags = ''): string
9394

9495
public function allConditionsAreMatched($conditions, array $context): bool
9596
{
96-
if (is_string($conditions)) {
97-
if ($conditions === '*') {
98-
return true;
99-
}
100-
// Try to parse as JSON
101-
$parsed = json_decode($conditions, true);
102-
if (json_last_error() === JSON_ERROR_NONE) {
103-
$conditions = $parsed;
104-
} else {
105-
return false;
106-
}
107-
}
108-
109-
$getRegex = function(string $regexString, string $regexFlags) {
110-
return $this->getRegex($regexString, $regexFlags);
111-
};
112-
113-
if (is_array($conditions)) {
114-
// If it's an empty array, always match (true)
115-
if (count($conditions) === 0) {
116-
return true;
117-
}
118-
// Logical operators
119-
if (isset($conditions['and']) && is_array($conditions['and'])) {
120-
foreach ($conditions['and'] as $subCondition) {
121-
if (!$this->allConditionsAreMatched($subCondition, $context)) {
122-
return false;
123-
}
124-
}
125-
return true;
126-
}
127-
if (isset($conditions['or']) && is_array($conditions['or'])) {
128-
foreach ($conditions['or'] as $subCondition) {
129-
if ($this->allConditionsAreMatched($subCondition, $context)) {
130-
return true;
131-
}
132-
}
133-
return false;
134-
}
135-
if (isset($conditions['not']) && is_array($conditions['not'])) {
136-
foreach ($conditions['not'] as $subCondition) {
137-
if ($this->allConditionsAreMatched($subCondition, $context)) {
138-
return false;
139-
}
140-
}
141-
return true;
142-
}
143-
// If it's a plain array, treat as AND (all must match)
144-
if (array_keys($conditions) === range(0, count($conditions) - 1)) {
145-
foreach ($conditions as $subCondition) {
146-
if (!$this->allConditionsAreMatched($subCondition, $context)) {
147-
return false;
148-
}
149-
}
150-
return true;
151-
}
152-
// If it's a single condition (associative array)
153-
if (isset($conditions['attribute'])) {
154-
try {
155-
return Conditions::conditionIsMatched($conditions, $context, $getRegex);
156-
} catch (\Exception $e) {
157-
$this->logger->warning($e->getMessage(), [
158-
'exception' => $e,
159-
'condition' => $conditions,
160-
'context' => $context,
161-
]);
162-
return false;
163-
}
164-
}
165-
}
166-
return false;
97+
return Conditions::createFromMixed($conditions)->isSatisfiedBy($context);
16798
}
16899

169100
public function segmentIsMatched(array $segment, array $context): bool
@@ -173,6 +104,7 @@ public function segmentIsMatched(array $segment, array $context): bool
173104

174105
public function allSegmentsAreMatched($groupSegments, array $context): bool
175106
{
107+
var_dump($groupSegments);
176108
if ($groupSegments === '*') {
177109
return true;
178110
}

0 commit comments

Comments
 (0)