-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Icinga/proper-meta-data-for-filters
Proper meta data for filters
- Loading branch information
Showing
6 changed files
with
207 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |