Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
}
],
"require": {
"php": ">=7.0",
"illuminate/database": ">=5.4"
"php": "^8.2",
"illuminate/support": "^10.0",
"illuminate/database": "^10.0"
},
"require-dev": {
"kahlan/kahlan": "~1.1"
"kahlan/kahlan": "5.2.3"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 5 additions & 4 deletions spec/BuilderSpec.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?php

use kahlan\plugin\Stub;
use Kahlan\Plugin\Double;
use Kahlan\Plugin\Stub;
use Sofa\Hookable\Builder;

describe('Sofa\Hookable\Builder', function () {

beforeEach(function () {
$query = Stub::create(['class' => 'Illuminate\Database\Query\Builder']);
$this->eloquent = Stub::classname(['class' => 'Illuminate\Database\Eloquent\Builder']);
$query = Double::instance(['class' => 'Illuminate\Database\Query\Builder']);
$this->eloquent = Double::classname(['class' => 'Illuminate\Database\Eloquent\Builder']);
$this->builder = new Builder(new $query);
});

Expand All @@ -17,7 +18,7 @@
});

it('calls hook defined on the model', function () {
$model = Stub::create();
$model = Double::instance();
expect($model)->toReceive('queryHook');
Stub::on($this->builder)->method('getModel', function () use ($model) {return $model;});
$this->builder->select(['column', 'value']);
Expand Down
11 changes: 6 additions & 5 deletions spec/HookableSpec.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?php

use kahlan\plugin\Stub;
use Kahlan\Plugin\Double;
use Kahlan\Plugin\Stub;
use Sofa\Hookable\Hookable;

describe('Sofa\Hookable\Hookable', function () {

it('resolves hooks in instance scope', function () {
$parent = Stub::classname();
$parent = Double::classname();
Stub::on($parent)->method('getAttribute', function () { return 'value'; });

$hookableClass = Stub::classname(['uses' => Hookable::class, 'extends' => $parent]);
$hookableClass = Double::classname(['uses' => Hookable::class, 'extends' => $parent]);
$hookableClass::hook('getAttribute', function ($next, $value, $args) {
$this->instanceMethod();
});
Expand All @@ -20,9 +21,9 @@
});

it('flushes all hooks with the flushHooks method', function () {
$parent = Stub::classname();
$parent = Double::classname();

$hookableClass = Stub::classname(['uses' => Hookable::class, 'extends' => $parent]);
$hookableClass = Double::classname(['uses' => Hookable::class, 'extends' => $parent]);
$hookableClass::hook('method1', function ($next, $value, $args) {});
$hookableClass::hook('method2', function ($next, $value, $args) {});

Expand Down
6 changes: 3 additions & 3 deletions spec/PipelineSpec.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use kahlan\plugin\Stub;
use Kahlan\Plugin\Double;
use Kahlan\Plugin\Stub;
use Sofa\Hookable\Pipeline;

describe('\Sofa\Hookable\Pipeline', function () {
Expand Down Expand Up @@ -43,11 +44,10 @@ function ($next, $payload) { $payload .= ',third'; return $next($payload); },
};
$pipeline = new Pipeline($pipes);

$args = Stub::create(['implements' => ['Sofa\Hookable\Contracts\ArgumentBag']]);
$args = Double::instance(['implements' => ['Sofa\Hookable\Contracts\ArgumentBag']]);
Stub::on($args)->method('get')->andReturn('bar', 'bar');

expect($args)->toReceive('get');
expect($args)->toReceiveNext('get');
$result = $pipeline->send($payload)->with($args)->to($destination);
expect($result)->toBe('start,pipe-bar,end-bar');
});
Expand Down
29 changes: 18 additions & 11 deletions src/ArgumentBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Sofa\Hookable;

class ArgumentBag implements \Sofa\Hookable\Contracts\ArgumentBag
class ArgumentBag implements Contracts\ArgumentBag
{
/**
* Arguments being passed.
Expand All @@ -24,15 +24,15 @@ public function __construct(array $items)
/**
* @inheritdoc
*/
public function all()
public function all(): array
{
return $this->items;
}

/**
* @inheritdoc
*/
public function first()
public function first(): mixed
{
$items = $this->items;

Expand All @@ -42,7 +42,7 @@ public function first()
/**
* @inheritdoc
*/
public function last()
public function last(): mixed
{
$items = array_reverse($this->items);

Expand All @@ -52,26 +52,33 @@ public function last()
/**
* @inheritdoc
*/
public function get($key, $default = null)
public function get(string $key, mixed $default = null): mixed
{
return array_key_exists($key, $this->items) ? $this->items[$key] : $default;
}

/**
* Set value at given key.
*
* @param string $key
* @param mixed $value
* @inheritDoc
*/
public function set($key, $value)
public function set(string $key, mixed $value): void
{
$this->items[$key] = $value;
}

/**
* @inheritDoc
*/
public function unset(string $key): void
{
if (isset($this->items[$key])) {
unset($this->items[$key]);
}
}

/**
* @inheritdoc
*/
public function isEmpty()
public function isEmpty(): bool
{
return ! count($this->items);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Builder extends EloquentBuilder
*/
public function callParent($method, array $args)
{
return call_user_func_array("parent::{$method}", $args);
return parent::$method(...array_values($args));
}

/**
Expand Down Expand Up @@ -124,7 +124,7 @@ public function select($columns = ['*'])
*/
public function where($column, $operator = null, $value = null, $boolean = 'and')
{
if (!in_array(strtolower($operator), $this->operators, true)) {
if (is_string($operator) && !in_array(strtolower($operator), $this->operators, true)) {
list($value, $operator) = [$operator, '='];
}

Expand Down
32 changes: 26 additions & 6 deletions src/Contracts/ArgumentBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,55 @@ interface ArgumentBag
*
* @return array
*/
public function all();
public function all(): array;

/**
* Fetch first argument from the bag.
*
* @return mixed
*/
public function first();
public function first(): mixed;

/**
* Fetch last argument from the bag.
*
* @return mixed
*/
public function last();
public function last(): mixed;

/**
* Get argument with given key.
*
* @param string|int $key
* @param string $key
* @param mixed $default
*
* @return mixed
*/
public function get($key, $default = null);
public function get(string $key, mixed $default = null): mixed;

/**
* Set value at given key.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function set(string $key, mixed $value): void;

/**
* Unset value at given key.
*
* @param string $key
*
* @return void
*/
public function unset(string $key): void;

/**
* Determine whether the bag is empty.
*
* @return boolean
*/
public function isEmpty();
public function isEmpty(): bool;
}