Skip to content

Commit

Permalink
Merge pull request #7 from ARCANEDEV/update-laravel_7
Browse files Browse the repository at this point in the history
Updating the package to support Laravel 7
  • Loading branch information
arcanedev-maroc authored Mar 5, 2020
2 parents 08f6b92 + 1b2caac commit 246acf9
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 94 deletions.
10 changes: 5 additions & 5 deletions .editorconfig
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
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (c) 2019 | ARCANEDEV(c) - php-html
Copyright (c) 2019-2020 | ARCANEDEV(c) - php-html

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
"type": "library",
"license": "MIT",
"require": {
"php": ">=7.2.0",
"illuminate/support": "^6.0"
"php": "^7.2.5",
"illuminate/support": "^7.0"
},
"require-dev": {
"ext-dom": "*",
"phpunit/phpunit": "^8.0",
"phpunit/phpcov": "^6.0"
"phpunit/phpunit": "^8.5|^9.0"
},
"autoload": {
"psr-4": {
Expand All @@ -31,5 +30,7 @@
"psr-4": {
"Arcanedev\\Html\\Tests\\": "tests/"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
115 changes: 115 additions & 0 deletions src/Elements/Concerns/HasConditionalMethods.php
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;
}
}
}
91 changes: 8 additions & 83 deletions src/Elements/HtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@
namespace Arcanedev\Html\Elements;

use Arcanedev\Html\Contracts\Elements\HtmlElement as HtmlElementContract;
use Arcanedev\Html\Exceptions\InvalidHtmlException;
use Arcanedev\Html\Exceptions\MissingTagException;
use Closure;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Arcanedev\Html\Exceptions\{InvalidHtmlException, MissingTagException};
use Illuminate\Support\{Collection, HtmlString, Str};
use Illuminate\Support\Traits\Macroable;

/**
Expand All @@ -21,7 +17,7 @@
*
* @method \Arcanedev\Html\Elements\HtmlElement|mixed attributeIf(bool $condition, string $attribute, mixed $value = null)
* @method \Arcanedev\Html\Elements\HtmlElement|mixed attributeUnless(bool $condition, string $attribute, mixed $value = null)
* @method \Arcanedev\Html\Elements\HtmlElement|mixed attributeIfNotNull(mixed $value, string $attribute, mixed $value = null)
* @method \Arcanedev\Html\Elements\HtmlElement|mixed attributeIfNotNull(mixed $valueToCheck, string $attribute, mixed $value = null)
*/
abstract class HtmlElement implements HtmlElementContract
{
Expand All @@ -32,6 +28,7 @@ abstract class HtmlElement implements HtmlElementContract

use Concerns\HasAttributes,
Concerns\HasChildElements,
Concerns\HasConditionalMethods,
Macroable {
__call as __callMacro;
}
Expand Down Expand Up @@ -211,48 +208,6 @@ public function html($html)
return $this->setNewChildren($html);
}

/**
* 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);
}

/**
* Open the html element.
*
Expand Down Expand Up @@ -348,47 +303,17 @@ public function __toString()
*/
public function __call($name, array $arguments = [])
{
if (Str::endsWith($name, $conditions = ['If', 'Unless', 'IfNotNull'])) {
foreach ($conditions as $condition) {
if (method_exists($this, $method = str_replace($condition, '', $name)))
if (Str::endsWith($name, $this->supportedConditions)) {
foreach ($this->supportedConditions as $condition) {
if (method_exists($this, $method = str_replace($condition, '', $name))) {
return $this->callConditionalMethod($condition, $method, $arguments);
}
}
}

return $this->__callMacro($name, $arguments);
}

/**
* Call the if condition.
*
* @param string $type
* @param string $method
* @param array $arguments
*
* @return \Arcanedev\Html\Elements\HtmlElement|mixed
*/
protected function callConditionalMethod($type, $method, array $arguments)
{
$value = array_shift($arguments);
$callback = function () use ($method, $arguments) {
return $this->{$method}(...$arguments);
};

switch ($type) {
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;
}
}

/**
* Clone the object.
*/
Expand Down

0 comments on commit 246acf9

Please sign in to comment.