-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ARCANEDEV/update-laravel_7
Updating the package to support Laravel 7
- Loading branch information
Showing
5 changed files
with
135 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
# This file is for unifying the coding style for different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.{yml,yaml}] | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Arcanedev\Html\Elements\Concerns; | ||
|
||
use Closure; | ||
|
||
/** | ||
* Trait HasConditionalMethods | ||
* | ||
* @package Arcanedev\Html\Elements\Concerns | ||
* @author ARCANEDEV <[email protected]> | ||
*/ | ||
trait HasConditionalMethods | ||
{ | ||
/* ----------------------------------------------------------------- | ||
| Properties | ||
| ----------------------------------------------------------------- | ||
*/ | ||
|
||
/** | ||
* The supported conditions. | ||
* | ||
* @var array | ||
*/ | ||
protected $supportedConditions = [ | ||
'If', | ||
'Unless', | ||
'IfNotNull', | ||
]; | ||
|
||
/* ----------------------------------------------------------------- | ||
| Main Methods | ||
| ----------------------------------------------------------------- | ||
*/ | ||
|
||
/** | ||
* Conditionally transform the element. | ||
* Note that since elements are immutable, you'll need to return a new instance from the callback. | ||
* | ||
* @param bool $condition | ||
* @param \Closure $callback | ||
* | ||
* @return $this|mixed | ||
*/ | ||
public function if(bool $condition, Closure $callback) | ||
{ | ||
return $condition ? $callback($this) : $this; | ||
} | ||
|
||
/** | ||
* Conditionally transform the element. | ||
* Note that since elements are immutable, you'll need to return a new instance from the callback. | ||
* | ||
* @param bool $condition | ||
* @param \Closure $callback | ||
* | ||
* @return $this|mixed | ||
*/ | ||
public function unless(bool $condition, Closure $callback) | ||
{ | ||
return $this->if( ! $condition, $callback); | ||
} | ||
|
||
/** | ||
* Conditionally transform the element. | ||
* Note that since elements are immutable, you'll need to return a new instance from the callback. | ||
* | ||
* @param mixed $value | ||
* @param \Closure $callback | ||
* | ||
* @return mixed | ||
*/ | ||
public function ifNotNull($value, Closure $callback) | ||
{ | ||
return $this->if( ! is_null($value), $callback); | ||
} | ||
|
||
/* ----------------------------------------------------------------- | ||
| Other Methods | ||
| ----------------------------------------------------------------- | ||
*/ | ||
|
||
/** | ||
* Call the if condition. | ||
* | ||
* @param string $conditions | ||
* @param string $method | ||
* @param array $arguments | ||
* | ||
* @return \Arcanedev\Html\Elements\HtmlElement|mixed | ||
*/ | ||
protected function callConditionalMethod(string $conditions, string $method, array $arguments) | ||
{ | ||
$value = array_shift($arguments); | ||
$callback = function () use ($method, $arguments): self { | ||
return $this->{$method}(...$arguments); | ||
}; | ||
|
||
switch ($conditions) { | ||
case 'If': | ||
return $this->if((bool) $value, $callback); | ||
|
||
case 'Unless': | ||
return $this->unless((bool) $value, $callback); | ||
|
||
case 'IfNotNull': | ||
return $this->ifNotNull($value, $callback); | ||
|
||
default: | ||
return $this; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters