Skip to content

Commit d97c2f7

Browse files
committed
Add array condition support to discovery
1 parent ced3b90 commit d97c2f7

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

spec/ClassDiscoverySpec.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ function it_registers_a_class_with_a_boolean_condition()
5656
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
5757
}
5858

59+
function it_registers_a_class_with_an_array_condition()
60+
{
61+
$this->reset();
62+
63+
$this->register(
64+
'spec\Http\Discovery\AnotherClassToFind',
65+
[
66+
true,
67+
'spec\Http\Discovery\AnotherClassToFind',
68+
]
69+
);
70+
$this->register(
71+
'spec\Http\Discovery\ClassToFind',
72+
[
73+
false,
74+
'spec\Http\Discovery\ClassToFind',
75+
]
76+
);
77+
78+
$this->find()->shouldHaveType('spec\Http\Discovery\AnotherClassToFind');
79+
}
80+
5981
function it_registers_a_class_with_an_invalid_condition()
6082
{
6183
$this->reset();

src/ClassDiscovery.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ protected static function evaluateCondition($condition)
6868
return $condition();
6969
} elseif (is_bool($condition)) {
7070
return $condition;
71+
} elseif (is_array($condition)) {
72+
$evaluatedCondition = true;
73+
74+
// Immediately stop execution if the condition is false
75+
for ($i = 0; $i < count($condition) && false !== $evaluatedCondition; $i++) {
76+
$evaluatedCondition &= static::evaluateCondition($condition[$i]);
77+
}
78+
79+
return $evaluatedCondition;
7180
}
7281

7382
return false;

0 commit comments

Comments
 (0)