Skip to content

Commit

Permalink
adding handling for an external static method
Browse files Browse the repository at this point in the history
  • Loading branch information
enygma committed Feb 22, 2018
1 parent 3fb9262 commit ac97fe8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions README.md
Expand Up @@ -44,6 +44,21 @@ the error like (via the `Psecio\Canary\Notify\ErrorLog` handler). The JSON encod
> **NOTE:** Canary automatically pulls in the `$_GET` and `$_POST` superglobal values for evaluation so you don't need to manually pass
then in.

### Using an external data source

`Canary` also allows you to use a (static) class method to provide the `if` portion of the evaluation with data. To use it, just pass in the class
and static method name as a string:

```
<?php
$classMethod = '\Foo\Bar::criteria';
\Psecio\Canary\Instance::build()->if($classMethod)->execute();
?>
```

The return from this method must be an array otherwise an exception will be thrown.

### Supported Notifier Methods

Currently `Canary` supports the following notification methods:
Expand Down
16 changes: 16 additions & 0 deletions src/Instance.php
Expand Up @@ -131,6 +131,22 @@ public function if(...$params) : \Psecio\Canary\Instance
{
if (count($params) == 1 && $params[0] instanceof CriteriaSet) {
$this->criteria = $params[0];
} elseif (count($params) == 1 && is_string($params[0])) {
// Try to load it like a class
list($classNs, $method) = explode('::', $params[0]);
if (!class_exists($classNs)) {
throw new \InvalidArgumentException('Invalid data source: '.$classNs);
}
$result = call_user_func($classNs.'::'.$method);

if (!is_array($result)) {
throw new \InvalidArgumentException('Method call must return an array');
}
foreach ($result as $index => $value) {
$equals = new Equals($index, $value);
$this->criteria->add($equals);
}

} elseif (count($params) == 1 && $params[0] instanceof Criteria) {
$this->criteria->add($params[0]);

Expand Down

0 comments on commit ac97fe8

Please sign in to comment.