/** * Call custom handlers for where call. * * @param string $method * @param \Sofa\Hookable\ArgumentBag $args * @return mixed */ protected function callHook($method, ArgumentBag $args) { if ($this->hasHook($args->get('column')) || in_array($method, ['select', 'addSelect'])) { return $this->getModel()->queryHook($this, $method, $args); } return $this->callParent($method, $args->all()); }
/** * Allow custom where method calls on the builder. * * @param \Sofa\Hookable\Builder $query * @param string $method * @param \Sofa\Hookable\ArgumentBag $args * @return \Sofa\Hookable\Builder */ public function queryHook(Builder $query, $method, ArgumentBag $args) { $hooks = $this->boundHooks(__FUNCTION__); $params = compact('method', 'args'); $payload = $query; $destination = function ($query) use($method, $args) { return call_user_func_array([$query, 'callParent'], [$method, $args->all()]); }; return $this->pipe($hooks, $payload, $params, $destination); }
<?php use Sofa\Hookable\ArgumentBag; describe('Sofa\\Hookable\\ArgumentBag', function () { beforeEach(function () { $this->bag = new ArgumentBag(['foo' => 'bar', 'faz' => 'baz', 'f*x' => 'blox']); }); it('shows whether is empty', function () { expect($this->bag->isEmpty())->toBe(false); $empty = new ArgumentBag([]); expect($empty->isEmpty())->toBe(true); }); it('gets, sets and updates elements values', function () { expect($this->bag->get('foo'))->toBe('bar'); $this->bag->set('foo', 'baz'); expect($this->bag->get('foo'))->toBe('baz'); }); it('provides api for first and last elements', function () { expect($this->bag->last())->toBe('blox'); expect($this->bag->first())->toBe('bar'); }); it('gets all elements as array', function () { expect($this->bag->all())->toBe(['foo' => 'bar', 'faz' => 'baz', 'f*x' => 'blox']); }); });