Skip to content

Instantly share code, notes, and snippets.

@brennen
Created November 15, 2011 22:38
Show Gist options
  • Save brennen/1368608 to your computer and use it in GitHub Desktop.
Save brennen/1368608 to your computer and use it in GitHub Desktop.
Some kind of thing that does things with things.
<?php
namespace bpb;
$events = array(
array(
'name' => 'brennen',
'dog' => 'silent'
),
array(
'name' => 'suzy',
'dog' => 'bark',
),
array(
'name' => 'ralph',
'dog' => 'bark',
)
);
foreach ($events as $event) {
Check::out($event)
->where(array('name' => 'brennen'))
->call(function ($e) {
print "got b.\n";
})
->where(array('name' => 'ralph'))
->call(function ($e) {
print "got r.\n";
});
}
class Check {
protected $_match = true;
protected $_event = null;
// syntax sugar
public static function out (array $event)
{
return new self($event);
}
// ignore this
public function __construct (array $event)
{
$this->_event = $event;
}
public function where (array $pattern)
{
$this->_match = true;
foreach ($pattern as $field => $check) {
if ($this->_event[$field] != $check) {
$this->_match = false;
break;
}
}
return $this;
}
public function call ($callback)
{
if ($this->_match)
$callback($this->_event);
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment