Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/ConditionalBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class ConditionalBuilder extends ParentDelegationBuilder
* Creates the first rule. Additional rules can be chained use `or` and `and`
* @param string $name
* @param string $operator
* @param string $value
* @param string|null $value
*/
public function __construct($name, $operator, $value)
public function __construct($name, $operator, $value = null)
{
$this->and($name, $operator, $value);
}
Expand All @@ -38,10 +38,10 @@ public function build()
* Creates an AND condition
* @param string $name
* @param string $operator
* @param string $value
* @param string|null $value
* @return $this
*/
public function andCondition($name, $operator, $value)
public function andCondition($name, $operator, $value = null)
{
$orCondition = $this->popOrCondition();
$orCondition[] = $this->createCondition($name, $operator, $value);
Expand All @@ -54,10 +54,10 @@ public function andCondition($name, $operator, $value)
* Creates an OR condition
* @param string $name
* @param string $operator
* @param string $value
* @param string|null $value
* @return $this
*/
public function orCondition($name, $operator, $value)
public function orCondition($name, $operator, $value = null)
{
$condition = $this->createCondition($name, $operator, $value);
$this->pushOrCondition([$condition]);
Expand All @@ -69,16 +69,16 @@ public function orCondition($name, $operator, $value)
* Creates a condition
* @param string $name
* @param string $operator
* @param string $value
* @param string|null $value
* @return array
*/
protected function createCondition($name, $operator, $value)
protected function createCondition($name, $operator, $value = null)
{
return [
return array_filter([
'field' => $name,
'operator' => $operator,
'value' => $value,
];
], fn($value) => $value !== null);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/LocationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ class LocationBuilder extends ConditionalBuilder
* @param string $value
* @return array
*/
protected function createCondition($name, $operator, $value)
protected function createCondition($name, $operator, $value = '')
{
if ($value === '') {
throw new \InvalidArgumentException('Value is required for location conditions');
}

return [
'param' => $name,
'operator' => $operator,
Expand Down
20 changes: 18 additions & 2 deletions tests/ConditionalBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class ConditionalBuilderTest extends TestCase
{
use ArraySubsetAsserts;

public function testContionalLogic()
{
$builder = new ConditionalBuilder('color', '==', 'other');
Expand All @@ -27,6 +27,22 @@ public function testContionalLogic()
$this->assertArraySubset($expectedConfig, $builder->build());
}

public function testContionalLogicOperatorOnly()
{
$builder = new ConditionalBuilder('color', '!=empty');

$expectedConfig = [
[
[
'field' => 'color',
'operator' => '!=empty',
],
]
];

$this->assertSame($expectedConfig, $builder->build());
}

public function testAnd()
{
$builder = new ConditionalBuilder('color', '==', 'other');
Expand Down Expand Up @@ -86,4 +102,4 @@ public function testOr()

$this->assertArraySubset($expectedConfig, $builder->build());
}
}
}