Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/40'
Browse files Browse the repository at this point in the history
Close #40
  • Loading branch information
weierophinney committed May 17, 2017
2 parents e8c1aca + 42a65a7 commit 90c72b9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ All notable changes to this project will be documented in this file, in reverse

- Nothing.

### Changes

- [#40](https://github.com/zendframework/zend-filter/pull/40) updates the
`Callback` filter's `setCallback()` method to allow passing a string name of a
class that is instantiable without constructor arguments, and which defines
`__invoke()`.

### Deprecated

- Nothing.
Expand Down
13 changes: 13 additions & 0 deletions doc/book/standard-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,19 @@ $filter = new Zend\Filter\Callback(array('MyClass', 'reverse'));
print $filter->filter('Hello!');
```

As of PHP 5.5 you can use ::class resolution for given callback class:

```php
class MyClass
{
public function __invoke($param);
}

// The filter definition
$filter = new Zend\Filter\Callback(MyClass::class);
print $filter->filter('Hello!');
```

To get the actual set callback use `getCallback()` and to set another callback
use `setCallback()`.

Expand Down
6 changes: 5 additions & 1 deletion src/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Callback extends AbstractFilter
*/
public function __construct($callbackOrOptions = [], $callbackParams = [])
{
if (is_callable($callbackOrOptions)) {
if (is_callable($callbackOrOptions) || is_string($callbackOrOptions)) {
$this->setCallback($callbackOrOptions);
$this->setCallbackParams($callbackParams);
} else {
Expand All @@ -44,6 +44,10 @@ public function __construct($callbackOrOptions = [], $callbackParams = [])
*/
public function setCallback($callback)
{
if (is_string($callback) && class_exists($callback)) {
$callback = new $callback();
}

if (! is_callable($callback)) {
throw new Exception\InvalidArgumentException(
'Invalid parameter for callback: must be callable'
Expand Down
11 changes: 11 additions & 0 deletions test/CallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public function testStaticCallback()
$this->assertEquals('staticCallback-test', $filter('test'));
}

public function testStringClassCallback()
{
$filter = new CallbackFilter(self::class);
$this->assertEquals('stringClassCallback-test', $filter('test'));
}

public function testSettingDefaultOptions()
{
$filter = new CallbackFilter([$this, 'objectCallback'], 'param');
Expand Down Expand Up @@ -75,6 +81,11 @@ public static function staticCallback($value)
return 'staticCallback-' . $value;
}

public function __invoke($value)
{
return 'stringClassCallback-' . $value;
}

public function objectCallbackWithParams($value, $param = null)
{
return 'objectCallbackWithParams-' . $value . '-' . $param;
Expand Down

0 comments on commit 90c72b9

Please sign in to comment.