HigherOrderWhenProxy.php000064400000004311147366441110011351 0ustar00target = $target; } /** * Set the condition on the proxy. * * @param bool $condition * @return $this */ public function condition($condition) { [$this->condition, $this->hasCondition] = [$condition, true]; return $this; } /** * Indicate that the condition should be negated. * * @return $this */ public function negateConditionOnCapture() { $this->negateConditionOnCapture = true; return $this; } /** * Proxy accessing an attribute onto the target. * * @param string $key * @return mixed */ public function __get($key) { if (! $this->hasCondition) { $condition = $this->target->{$key}; return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition); } return $this->condition ? $this->target->{$key} : $this->target; } /** * Proxy a method call on the target. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (! $this->hasCondition) { $condition = $this->target->{$method}(...$parameters); return $this->condition($this->negateConditionOnCapture ? ! $condition : $condition); } return $this->condition ? $this->target->{$method}(...$parameters) : $this->target; } } LICENSE.md000064400000002063147366441110006160 0ustar00The MIT License (MIT) Copyright (c) Taylor Otwell Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Traits/Conditionable.php000064400000004320147366441110011303 0ustar00condition($value); } if ($value) { return $callback($this, $value) ?? $this; } elseif ($default) { return $default($this, $value) ?? $this; } return $this; } /** * Apply the callback if the given "value" is (or resolves to) falsy. * * @template TUnlessParameter * @template TUnlessReturnType * * @param (\Closure($this): TUnlessParameter)|TUnlessParameter|null $value * @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $callback * @param (callable($this, TUnlessParameter): TUnlessReturnType)|null $default * @return $this|TUnlessReturnType */ public function unless($value = null, ?callable $callback = null, ?callable $default = null) { $value = $value instanceof Closure ? $value($this) : $value; if (func_num_args() === 0) { return (new HigherOrderWhenProxy($this))->negateConditionOnCapture(); } if (func_num_args() === 1) { return (new HigherOrderWhenProxy($this))->condition(! $value); } if (! $value) { return $callback($this, $value) ?? $this; } elseif ($default) { return $default($this, $value) ?? $this; } return $this; } } composer.json000064400000001363147366441110007300 0ustar00{ "name": "illuminate/conditionable", "description": "The Illuminate Conditionable package.", "license": "MIT", "homepage": "https://laravel.com", "support": { "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, "authors": [ { "name": "Taylor Otwell", "email": "taylor@laravel.com" } ], "require": { "php": "^8.0.2" }, "autoload": { "psr-4": { "Illuminate\\Support\\": "" } }, "extra": { "branch-alias": { "dev-master": "10.x-dev" } }, "config": { "sort-packages": true }, "minimum-stability": "dev" }