Skip to content

Commit

Permalink
Conditions update
Browse files Browse the repository at this point in the history
  • Loading branch information
misantron committed Jan 23, 2018
1 parent 474740d commit 9403ff9
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/Helper/Escape.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,21 @@ protected function escapeArray(array $values): string

/**
* @param array $items
* @return string
* @return array
*/
protected function escapeList(array $items): string
protected function escapeList(array $items): array
{
$type = $this->isIntegerArray($items) ? 'integer': 'string';
$filtered = array_filter($items);
$filtered = array_unique(array_filter($items));
if (empty($filtered)) {
throw new \InvalidArgumentException('Value list is empty');
throw new \InvalidArgumentException('Invalid values: value list is empty');
}
if ($type === 'string') {
$filtered = array_map(function (string $item) {
return $this->quote($item);
}, $filtered);
}
return implode(',', $filtered);
return $filtered;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/Query/Condition/InArrayCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@
class InArrayCondition extends Condition
{
/**
* @var mixed
* @var string|int|float|bool
*/
private $value;

/**
* @param string $column
* @param mixed $value
* @param string|int|float|bool $value
* @param string $operator
*/
public function __construct(string $column, $value, string $operator)
{
parent::__construct($column, $operator);

if (!is_scalar($value)) {
throw new \InvalidArgumentException('Invalid value: value must be a scalar');
}

$this->value = $this->escapeValue($value);
}

/**
* @param string $column
* @param mixed $value
* @param string|int|float|bool $value
* @param string $operator
* @return InArrayCondition
*/
Expand All @@ -50,6 +54,6 @@ protected function getAcceptableOperators(): array
*/
public function __toString(): string
{
return sprintf('%s %s ANY (%s)', $this->value, $this->operator, $this->column);
return sprintf('%s %s ANY(%s)', $this->value, $this->operator, $this->column);
}
}
4 changes: 2 additions & 2 deletions src/Query/Condition/InCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class InCondition extends Condition
{
/**
* @var string
* @var array
*/
private $values;

Expand Down Expand Up @@ -50,6 +50,6 @@ protected function getAcceptableOperators(): array
*/
public function __toString(): string
{
return sprintf('%s %s (%s)', $this->column, $this->operator, $this->values);
return sprintf('%s %s (%s)', $this->column, $this->operator, implode(',', $this->values));
}
}
17 changes: 13 additions & 4 deletions tests/Query/Condition/InArrayConditionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public function testConstructorWithInvalidOperator()
new InArrayCondition('foo', 3, '>');
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid value: value must be a scalar
*/
public function testConstructorWithNotScalarValue()
{
new InArrayCondition('foo', [], '=');
}

public function testConstructor()
{
$condition = new InArrayCondition('foo', 5, '=');
Expand All @@ -46,12 +55,12 @@ public function testToString()
{
$condition = new InArrayCondition('foo', 5, '=');

$this->assertEquals("5 = ANY (foo)", $condition->__toString());
$this->assertEquals("5 = ANY (foo)", (string)$condition);
$this->assertEquals("5 = ANY(foo)", $condition->__toString());
$this->assertEquals("5 = ANY(foo)", (string)$condition);

$condition = new InArrayCondition('foo', 'bar', '!=');

$this->assertEquals("'bar' != ANY (foo)", $condition->__toString());
$this->assertEquals("'bar' != ANY (foo)", (string)$condition);
$this->assertEquals("'bar' != ANY(foo)", $condition->__toString());
$this->assertEquals("'bar' != ANY(foo)", (string)$condition);
}
}
57 changes: 57 additions & 0 deletions tests/Query/Condition/InConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace MediaTech\Query\Tests\Query\Condition;


use MediaTech\Query\Helper\Escape;
use MediaTech\Query\Query\Condition\InCondition;
use MediaTech\Query\Tests\BaseTestCase;

class InConditionTest extends BaseTestCase
{
use Escape;

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid values: value list is empty
*/
public function testConstructorWithEmptyValueList()
{
new InCondition('foo', [''], 'IN');
}

public function testConstructor()
{
$condition = new InCondition('foo', [1,2,3], 'IN');

$this->assertAttributeEquals('foo', 'column', $condition);
$this->assertAttributeEquals([1,2,3], 'values', $condition);
$this->assertAttributeEquals('IN', 'operator', $condition);

$condition = new InCondition('foo', ['bar','baz'], 'NOT IN');

$this->assertAttributeEquals('foo', 'column', $condition);
$this->assertAttributeEquals($this->escapeList(['bar','baz']), 'values', $condition);
$this->assertAttributeEquals('NOT IN', 'operator', $condition);
}

public function testCreate()
{
$condition = InCondition::create('foo', [1,2,3], 'NOT IN');

$this->assertEquals(new InCondition('foo', [1,2,3], 'NOT IN'), $condition);
}

public function testToString()
{
$condition = new InCondition('foo', [1,2,3], 'IN');

$this->assertEquals('foo IN (1,2,3)', $condition->__toString());
$this->assertEquals('foo IN (1,2,3)', (string)$condition);

$condition = new InCondition('foo', ['bar','baz'], 'NOT IN');

$this->assertEquals("foo NOT IN ('bar','baz')", $condition->__toString());
$this->assertEquals("foo NOT IN ('bar','baz')", (string)$condition);
}
}

0 comments on commit 9403ff9

Please sign in to comment.