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

Commit

Permalink
Allow callable callabacks
Browse files Browse the repository at this point in the history
  • Loading branch information
NastuzziSamy committed May 8, 2020
1 parent e576148 commit cdd16e2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/Observers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ protected function push(BaseObserver $observer, array &$observers)
/**
* Create an observer and add it.
*
* @param string|array $data
* @param string $name
* @param \Closure $callback
* @param integer $priority
* @param string $class
* @param string|array $data
* @param string $name
* @param \Closure|callable|array $callback
* @param integer $priority
* @param string $class
* @return self
*/
public function create($data, string $name, \Closure $callback,
public function create($data, string $name, $callback,
int $priority=BaseObserver::MEDIUM_PRIORITY, string $class=null)
{
if (!\is_null($class)) {
Expand Down
33 changes: 18 additions & 15 deletions src/Observers/BaseObserver.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Create an Observer to add a Closure on a specific model event.
* Create an Observer to add a callable on a specific model event.
*
* @author Samy Nastuzzi <samy@nastuzzi.fr>
*
Expand All @@ -11,7 +11,6 @@
namespace Laramore\Observers;

use Laramore\Traits\IsLocked;
use Closure;

abstract class BaseObserver
{
Expand All @@ -27,7 +26,7 @@ abstract class BaseObserver
/**
* Callback to trigger when a specific model event happens.
*
* @var Closure
* @var \Closure|callable|array|null
*/
protected $callback;

Expand Down Expand Up @@ -58,14 +57,14 @@ abstract class BaseObserver
const LOW_PRIORITY = ((self::MIN_PRIORITY + self::MEDIUM_PRIORITY) / 2);

/**
* An observer needs at least a name and a Closure.
* An observer needs at least a name and a callable.
*
* @param string $name
* @param Closure $callback
* @param integer $priority
* @param mixed $data
* @param string $name
* @param \Closure|callable|array|null $callback
* @param integer $priority
* @param mixed $data
*/
public function __construct(string $name, Closure $callback=null, int $priority=self::MEDIUM_PRIORITY, $data=[])
public function __construct(string $name, $callback=null, int $priority=self::MEDIUM_PRIORITY, $data=[])
{
$this->setName($name);
$this->setCallback($callback);
Expand All @@ -84,11 +83,11 @@ public function getName(): string
}

/**
* Return the Closure function.
* Return the callable function.
*
* @return Closure|null
* @return \Closure|callable|array
*/
public function getCallback(): Closure
public function getCallback()
{
return $this->callback;
}
Expand Down Expand Up @@ -119,15 +118,19 @@ public function setName(string $name)
}

/**
* Define the Closure method until the observer is locked.
* Define the callable method until the observer is locked.
*
* @param Closure|null $callback
* @param \Closure|callable|array|null $callback
* @return self
*/
public function setCallback(Closure $callback=null)
public function setCallback($callback=null)
{
$this->needsToBeUnlocked();

if (!\is_callable($callback) && !is_null($callback)) {
throw new \Exception('Expecting a valid callable object.');
}

$this->callback = $callback;

return $this;
Expand Down

0 comments on commit cdd16e2

Please sign in to comment.