Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added notNull and null filters #15

Merged
merged 2 commits into from
Mar 3, 2017
Merged
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
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MyInMemoryRepository extends InMemoryRepository
}
```

Implementation can be seen [here](https://github.com/nilportugues/php-repository/blob/master/src/Infrastructure/Model/Repository/InMemory/InMemoryRepository.php).
Implementation can be seen [here](https://github.com/nilportugues/php-repository/blob/master/src/Infrastructure/Model/Repository/InMemory/InMemoryRepository.php).

The base InMemoryRepository implements the following interfaces:

Expand Down Expand Up @@ -62,12 +62,14 @@ Interaction with the repository requires the usage of the following classes or c
- `public function mustNot()`
- `public function should()`
- `public function clear()`
- **NilPortugues\Foundation\Domain\Model\Repository\BaseFilter**

- **NilPortugues\Foundation\Domain\Model\Repository\BaseFilter**
- `public function notStartsWith($filterName, $value)`
- `public function notEndsWith($filterName, $value)`
- `public function notEmpty($filterName)`
- `public function empty($filterName)`
- `public function notNull($filterName)`
- `public function empty($filterName)`
- `public function startsWith($filterName, $value)`
- `public function endsWith($filterName, $value)`
- `public function equal($filterName, $value)`
Expand Down Expand Up @@ -134,13 +136,13 @@ Interaction with the repository requires the usage of the following classes or c
- `public function setOrderFor($propertyName, OrderInterface $order)`
- `public function property($propertyName)`

#### Interfaces
#### Interfaces

- **NilPortugues\Foundation\Domain\Model\Repository\Contracts\Identity**

- `public function id()`
- `public function __toString()`

- **NilPortugues\Foundation\Domain\Model\Repository\Contracts\Repository**

- `public function count(Filter $filter = null)`
Expand Down
24 changes: 24 additions & 0 deletions src/Domain/Model/Repository/BaseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ public function hasEmpty(string $filterName): BaseFilterInterface
return $this;
}

/**
* @param string $filterName
*
* @return BaseFilterInterface
*/
public function notNull(string $filterName): BaseFilterInterface
{
$this->addFilter(self::NOT_NULL, $filterName, self::NOT_NULL);

return $this;
}

/**
* @param string $filterName
* @param mixed $value
Expand Down Expand Up @@ -313,4 +325,16 @@ public function empty(string $filterName): BaseFilterInterface

return $this;
}

/**
* @param string $filterName
*
* @return BaseFilterInterface
*/
public function null(string $filterName): BaseFilterInterface
{
$this->addFilter(self::NULL_FILTER, $filterName, self::NULL_FILTER);

return $this;
}
}
16 changes: 16 additions & 0 deletions src/Domain/Model/Repository/Contracts/BaseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ interface BaseFilter
const EQUALS = 'equals';
const NOT_EQUAL = 'not_equals';
const EMPTY_FILTER = 'empty';
const NULL_FILTER = 'null';
const NOT_EMPTY = 'not_empty';
const NOT_NULL = 'not_null';
const NOT_ENDS = 'not_ends';
const NOT_STARTS = 'not_starts';

Expand Down Expand Up @@ -68,6 +70,20 @@ public function notEmpty(string $filterName): BaseFilter;
*/
public function hasEmpty(string $filterName): BaseFilter;

/**
* @param string $filterName
*
* @return BaseFilter
*/
public function null(string $filterName): BaseFilter;

/**
* @param string $filterName
*
* @return BaseFilter
*/
public function notNull(string $filterName): BaseFilter;

/**
* @param string $filterName
* @param $value
Expand Down
25 changes: 24 additions & 1 deletion src/Infrastructure/Model/Repository/InMemory/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ protected static function must(array &$filteredResults, array $filters)
case BaseFilter::EMPTY_FILTER:
$filteredResults = array_filter($filteredResults, ComparisonFilter::empty($property));
break;
case BaseFilter::NOT_NULL:
$filteredResults = array_filter($filteredResults, ComparisonFilter::notNull($property));
break;
case BaseFilter::NULL_FILTER:
$filteredResults = array_filter($filteredResults, ComparisonFilter::null($property));
break;
case BaseFilter::NOT_STARTS:
$filteredResults = array_filter($filteredResults, StringFilter::notStartsWith($property, $v));
break;
Expand Down Expand Up @@ -233,6 +239,12 @@ protected static function mustNot(array &$filteredResults, array $filters)
case BaseFilter::EMPTY_FILTER:
$filteredResults = array_filter($filteredResults, ComparisonFilter::notEmpty($property));
break;
case BaseFilter::NOT_NULL:
$filteredResults = array_filter($filteredResults, ComparisonFilter::notNull($property));
break;
case BaseFilter::NULL_FILTER:
$filteredResults = array_filter($filteredResults, ComparisonFilter::null($property));
break;
case BaseFilter::NOT_STARTS:
$filteredResults = array_filter($filteredResults, StringFilter::startsWith($property, $v));
break;
Expand Down Expand Up @@ -365,7 +377,18 @@ protected static function should(array &$results, array &$filteredResults, array
array_filter($results, ComparisonFilter::empty($property))
);
break;

case BaseFilter::NOT_NULL:
$filteredResults = array_merge(
$filteredResults,
array_filter($results, ComparisonFilter::notNull($property))
);
break;
case BaseFilter::NULL_FILTER:
$filteredResults = array_merge(
$filteredResults,
array_filter($results, ComparisonFilter::null($property))
);
break;
case BaseFilter::NOT_STARTS:
$filteredResults = array_merge(
$filteredResults,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ public static function notEmpty(string $property): \Closure
return PropertyValue::get($v, $property) !== null && PropertyValue::get($v, $property) !== '';
};
}
/**
* @param string $property
*
* @return \Closure
*/
public static function null(string $property): \Closure
{
return function ($v) use ($property) {
return PropertyValue::get($v, $property) === null;
};
}

/**
* @param string $property
*
* @return \Closure
*/
public static function notNull(string $property): \Closure
{
return function ($v) use ($property) {
return PropertyValue::get($v, $property) !== null;
};
}

/**
* @param string $property
Expand Down
26 changes: 26 additions & 0 deletions tests/Domain/Model/Repository/BaseFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ public function testItShouldAddFilterForHasEmpty()
$this->assertEquals($expected, $this->filter->get());
}

public function testItShouldAddFilterForNull()
{
$this->filter->null('name');

$expected = [
'null' => [
'name' => ['null'],
],
];

$this->assertEquals($expected, $this->filter->get());
}

public function testItShouldAddFilterForNotNull()
{
$this->filter->notNull('name');

$expected = [
'not_null' => [
'name' => ['not_null'],
],
];

$this->assertEquals($expected, $this->filter->get());
}

public function testItShouldAddFilterForStartsWith()
{
$this->filter->startsWith('name', 'N');
Expand Down
18 changes: 18 additions & 0 deletions tests/Infrastructure/Model/Repository/InMemory/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,24 @@ public function testItMustBeEmpty()
$this->assertEquals(0, count($results));
}

public function testItMustNotBeNull()
{
$filter = new Filter();
$filter->must()->notNull('name');
$results = InMemoryFilter::filter($this->data, $filter);

$this->assertEquals(4, count($results));
}

public function testItMustBeNull()
{
$filter = new Filter();
$filter->must()->null('name');
$results = InMemoryFilter::filter($this->data, $filter);

$this->assertEquals(0, count($results));
}

public function testItMustNotNotStartWithScalar()
{
$filter = new Filter();
Expand Down