Skip to content

Commit

Permalink
Merge pull request #20 from Icinga/proper-meta-data-for-filters
Browse files Browse the repository at this point in the history
Proper meta data for filters
  • Loading branch information
nilmerg authored Mar 19, 2021
2 parents 69fce35 + d5fd08d commit 1280dd6
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 10 deletions.
89 changes: 89 additions & 0 deletions src/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace ipl\Stdlib;

class Data
{
/** @var array */
protected $data = [];

/**
* Check whether there's any data
*
* @return bool
*/
public function isEmpty()
{
return empty($this->data);
}

/**
* Check whether the given data exists
*
* @param string $name The name of the data
*
* @return bool
*/
public function has($name)
{
return array_key_exists($name, $this->data);
}

/**
* Get the value of the given data
*
* @param string $name The name of the data
* @param mixed $default The value to return if there's no such data
*
* @return mixed
*/
public function get($name, $default = null)
{
if ($this->has($name)) {
return $this->data[$name];
}

return $default;
}

/**
* Set the value of the given data
*
* @param string $name The name of the data
* @param mixed $value
*
* @return $this
*/
public function set($name, $value)
{
$this->data[$name] = $value;

return $this;
}

/**
* Merge the given data
*
* @param Data $with
*
* @return $this
*/
public function merge(self $with)
{
$this->data = array_merge($this->data, $with->data);

return $this;
}

/**
* Clear all data
*
* @return $this
*/
public function clear()
{
$this->data = [];

return $this;
}
}
11 changes: 7 additions & 4 deletions src/Filter/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

use ArrayIterator;
use Countable;
use ipl\Stdlib\Properties;
use IteratorAggregate;
use OutOfBoundsException;

abstract class Chain implements Rule, IteratorAggregate, Countable
abstract class Chain implements Rule, MetaDataProvider, IteratorAggregate, Countable
{
use Properties;
use MetaData;

/** @var Rule[] */
protected $rules = [];
Expand All @@ -28,10 +27,14 @@ public function __construct(Rule ...$rules)
}

/**
* Clone this chain's rules
* Clone this chain's meta data and rules
*/
public function __clone()
{
if ($this->metaData !== null) {
$this->metaData = clone $this->metaData;
}

foreach ($this->rules as $i => $rule) {
$this->rules[$i] = clone $rule;
}
Expand Down
18 changes: 12 additions & 6 deletions src/Filter/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@

namespace ipl\Stdlib\Filter;

use Exception;
use InvalidArgumentException;
use ipl\Stdlib\Properties;

abstract class Condition implements Rule
abstract class Condition implements Rule, MetaDataProvider
{
use Properties;
use MetaData;

/** @var string */
protected $column;
Expand All @@ -28,6 +24,16 @@ public function __construct($column, $value)
->setValue($value);
}

/**
* Clone this condition's meta data
*/
public function __clone()
{
if ($this->metaData !== null) {
$this->metaData = clone $this->metaData;
}
}

/**
* Set this condition's column
*
Expand Down
20 changes: 20 additions & 0 deletions src/Filter/MetaData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace ipl\Stdlib\Filter;

use ipl\Stdlib\Data;

trait MetaData
{
/** @var Data */
protected $metaData;

public function metaData()
{
if ($this->metaData === null) {
$this->metaData = new Data();
}

return $this->metaData;
}
}
15 changes: 15 additions & 0 deletions src/Filter/MetaDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace ipl\Stdlib\Filter;

use ipl\Stdlib\Data;

interface MetaDataProvider
{
/**
* Get this rule's meta data
*
* @return Data
*/
public function metaData();
}
64 changes: 64 additions & 0 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace ipl\Tests\Stdlib;

use ipl\Stdlib\Data;

class DataTest extends \PHPUnit\Framework\TestCase
{
public function testDataIsEmpty()
{
$data = new Data();

$this->assertTrue($data->isEmpty());
}

public function testDataHas()
{
$data = new Data();

$this->assertFalse($data->has('foo'));

$data->set('foo', 'bar');

$this->assertTrue($data->has('foo'));
}

public function testDataGet()
{
$data = new Data();

$this->assertNull($data->get('foo'));
$this->assertEquals('oof', $data->get('foo', 'oof'));

$data->set('foo', 'bar');

$this->assertEquals('bar', $data->get('foo'));
}

public function testDataMerge()
{
$data = new Data();

$toMerge = new Data();
$toMerge->set('foo', 'bar');

$this->assertNull($data->get('foo'));

$data->merge($toMerge);

$this->assertEquals('bar', $data->get('foo'));
}

public function testDataClear()
{
$data = new Data();
$data->set('foo', 'bar');

$this->assertFalse($data->isEmpty());

$data->clear();

$this->assertTrue($data->isEmpty());
}
}

0 comments on commit 1280dd6

Please sign in to comment.