Skip to content

Commit

Permalink
refactoring callable and comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
osorioramirez committed Jul 8, 2016
1 parent 2c5181b commit 5c2d482
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 71 deletions.
16 changes: 3 additions & 13 deletions DelegateInterface.php → AbstractCallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,11 @@
namespace Cubiche\Core\Delegate;

/**
* Delegate Interface.
* Abstract Callable class.
*
* @author Karel Osorio Ramírez <osorioramirez@gmail.com>
*/
interface DelegateInterface extends CallableInterface
abstract class AbstractCallable implements CallableInterface
{
/**
* @return mixed
*/
public function __invoke();

/**
* @param array $args
*
* @return mixed
*/
public function invokeWith(array $args);
use CallableTrait;
}
57 changes: 0 additions & 57 deletions AbstractDelegate.php

This file was deleted.

7 changes: 7 additions & 0 deletions CallableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ interface CallableInterface
* @return mixed
*/
public function __invoke();

/**
* @param array $args
*
* @return mixed
*/
public function invokeWith(array $args);
}
41 changes: 41 additions & 0 deletions CallableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Cubiche package.
*
* Copyright (c) Cubiche
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Cubiche\Core\Delegate;

/**
* Callable trait.
*
* @author Karel Osorio Ramírez <osorioramirez@gmail.com>
*/
trait CallableTrait
{
/**
* {@inheritdoc}
*/
public function __invoke()
{
return $this->invokeWith(\func_get_args());
}

/**
* {@inheritdoc}
*/
public function invokeWith(array $args)
{
return \call_user_func_array($this->innerCallable(), $args);
}

/**
* @return callable
*/
abstract protected function innerCallable();
}
39 changes: 38 additions & 1 deletion Delegate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
*
* @author Karel Osorio Ramírez <osorioramirez@gmail.com>
*/
class Delegate extends AbstractDelegate
class Delegate extends AbstractCallable
{
/**
* @var \ReflectionFunction|\ReflectionMethod
*/
private $reflection = null;

/**
* @var callable
*/
protected $callable;

/**
* @param Closure $closure
*
Expand Down Expand Up @@ -65,6 +70,22 @@ public static function fromFunction($function)
return new static($function);
}

/**
* @param callable $callable
*/
public function __construct($callable)
{
if (!\is_callable($callable)) {
throw new \InvalidArgumentException('The $callable argument must be a callable.');
}

if (\is_string($callable) && \strpos($callable, '::')) {
$callable = explode('::', $callable);
}

$this->callable = $callable;
}

/**
* @return \ReflectionFunction|\ReflectionMethod
*/
Expand All @@ -82,4 +103,20 @@ public function reflection()

return $this->reflection;
}

/**
* @return callable
*/
public function getCallable()
{
return $this->callable;
}

/**
* {@inheritdoc}
*/
protected function innerCallable()
{
return $this->getCallable();
}
}
8 changes: 8 additions & 0 deletions Tests/Fixtures/FooCallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,12 @@ public function __invoke()
{
return 'foo';
}

/**
* {@inheritdoc}
*/
public function invokeWith(array $args)
{
return \call_user_func_array($this, $args);
}
}

0 comments on commit 5c2d482

Please sign in to comment.