Skip to content

Commit

Permalink
Updating the package
Browse files Browse the repository at this point in the history
* Adding the `IfNotNull` condition.
* Add range for range inputs
* Format date and time (input) values
* Handling `0` values in inputs
  • Loading branch information
arcanedev-maroc committed Sep 4, 2019
1 parent 9132616 commit 7c1e422
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 36 deletions.
20 changes: 11 additions & 9 deletions src/Contracts/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ public function button($content = null, $type = null);
* Make a checkbox input.
*
* @param string|null $name
* @param bool $checked
* @param bool|null $checked
* @param string|null $value
*
* @return \Arcanedev\Html\Elements\Input
*/
public function checkbox($name = null, $checked = false, $value = '1');
public function checkbox($name = null, $checked = null, $value = '1');

/**
* Parse and render `class` attribute.
Expand All @@ -58,10 +58,11 @@ public function class($classes);
*
* @param string|null $name
* @param string|null $value
* @param bool $format
*
* @return \Arcanedev\Html\Elements\Input
*/
public function date($name = '', $value = '');
public function date($name = null, $value = null, bool $format = true);

/**
* Make a div element.
Expand Down Expand Up @@ -89,7 +90,7 @@ public function element($tag);
*
* @return \Arcanedev\Html\Elements\Input
*/
public function email($name, $value = null);
public function email($name = null, $value = null);

/**
* Make a fieldset tag.
Expand Down Expand Up @@ -212,12 +213,12 @@ public function password($name = null);
* Make a radio input.
*
* @param string|null $name
* @param bool $checked
* @param bool|null $checked
* @param string|null $value
*
* @return \Arcanedev\Html\Elements\Input
*/
public function radio($name = null, $checked = false, $value = null);
public function radio($name = null, $checked = null, $value = null);

/**
* Make a reset button.
Expand Down Expand Up @@ -260,12 +261,12 @@ public function submit($text = null);
/**
* Make a tel link.
*
* @param string $number
* @param string $phoneNumber
* @param string|null $text
*
* @return \Arcanedev\Html\Elements\A
*/
public function telLink($number, $text = null);
public function telLink($phoneNumber, $text = null);

/**
* Make a text input.
Expand All @@ -292,10 +293,11 @@ public function textarea($name = null, $value = null);
*
* @param string|null $name
* @param string|null $value
* @param bool $format
*
* @return \Arcanedev\Html\Elements\Input
*/
public function time($name = null, $value = null);
public function time($name = null, $value = null, $format = true);

/**
* Make a number input.
Expand Down
26 changes: 22 additions & 4 deletions src/Elements/HtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +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)
*/
abstract class HtmlElement implements HtmlElementContract
{
Expand Down Expand Up @@ -228,6 +229,20 @@ 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->unless(is_null($value), $callback);
}

/**
* Open the html element.
*
Expand Down Expand Up @@ -325,7 +340,7 @@ public function __toString()
*/
public function __call($name, array $arguments = [])
{
if (Str::endsWith($name, $conditions = ['If', 'Unless'])) {
if (Str::endsWith($name, $conditions = ['If', 'Unless', 'IfNotNull'])) {
foreach ($conditions as $condition) {
if (method_exists($this, $method = str_replace($condition, '', $name)))
return $this->callConditionalMethod($condition, $method, $arguments);
Expand All @@ -346,17 +361,20 @@ public function __call($name, array $arguments = [])
*/
protected function callConditionalMethod($type, $method, array $arguments)
{
$condition = (bool) array_shift($arguments);
$value = array_shift($arguments);
$callback = function () use ($method, $arguments) {
return $this->{$method}(...$arguments);
};

switch ($type) {
case 'If':
return $this->if($condition, $callback);
return $this->if((bool) $value, $callback);

case 'Unless':
return $this->unless($condition, $callback);
return $this->unless((bool) $value, $callback);

case 'IfNotNull':
return $this->ifNotNull($value, $callback);

default:
return $this;
Expand Down
92 changes: 80 additions & 12 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
Legend, Option, Select, Span, Textarea
};
use Arcanedev\Html\Entities\Attributes\ClassAttribute;
use DateTimeImmutable;
use Illuminate\Support\Str;

/**
* Class Html
Expand All @@ -15,6 +17,14 @@
*/
class Html implements HtmlContract
{
/* -----------------------------------------------------------------
| Constants
| -----------------------------------------------------------------
*/

const HTML_DATE_FORMAT = 'Y-m-d';
const HTML_TIME_FORMAT = 'H:i:s';

/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
Expand Down Expand Up @@ -54,18 +64,18 @@ public function button($content = null, $type = null)
* Make a checkbox input.
*
* @param string|null $name
* @param bool $checked
* @param bool|null $checked
* @param string|null $value
*
* @return \Arcanedev\Html\Elements\Input
*/
public function checkbox($name = null, $checked = false, $value = '1')
public function checkbox($name = null, $checked = null, $value = '1')
{
return Input::make()
->attribute('type', 'checkbox')
->attributeIf($name, 'name', $name)
->attributeIf($name, 'id', $name)
->attributeIf(! is_null($value), 'value', $value)
->attributeIf( ! is_null($value), 'value', $value)
->attributeIf((bool) $checked, 'checked');
}

Expand All @@ -86,12 +96,19 @@ public function class($classes)
*
* @param string|null $name
* @param string|null $value
* @param bool $format
*
* @return \Arcanedev\Html\Elements\Input
*/
public function date($name = '', $value = '')
public function date($name = null, $value = null, bool $format = true)
{
return $this->input('date', $name, $value);
$input = $this->input('date', $name, $value);

return $format
&& $input->hasAttribute('value')
&& ! empty($value = $input->getAttribute('value')->value())
? $input->value(static::formatDateTime($value, static::HTML_DATE_FORMAT))
: $input;
}

/**
Expand Down Expand Up @@ -121,12 +138,12 @@ public function element($tag)
/**
* Make an email input.
*
* @param string $name
* @param string|null $name
* @param string|null $value
*
* @return \Arcanedev\Html\Elements\Input
*/
public function email($name, $value = null)
public function email($name = null, $value = null)
{
return $this->input('email', $name, $value);
}
Expand Down Expand Up @@ -305,18 +322,37 @@ public function password($name = null)
* Make a radio input.
*
* @param string|null $name
* @param bool $checked
* @param bool|null $checked
* @param string|null $value
*
* @return \Arcanedev\Html\Elements\Input
*/
public function radio($name = null, $checked = false, $value = null)
public function radio($name = null, $checked = null, $value = null)
{
return $this->input('radio', $name, $value)
->attributeIf($name, 'id', $value === null ? $name : ($name.'_'.str_slug($value)))
->attributeIf($name, 'id', $value === null ? $name : ($name.'_'.Str::slug($value)))
->attributeIf(( ! is_null($value)) || $checked, 'checked');
}

/**
* Make a range input.
*
* @param string|null $name
* @param string|null $value
* @param string|null $min
* @param string|null $max
* @param string|null $step
*
* @return \Arcanedev\Html\Elements\Input
*/
public function range($name = null, $value = null, $min = null, $max = null, $step = null)
{
return $this->input('range', $name, $value)
->attributeIfNotNull($min, 'min', $min)
->attributeIfNotNull($max, 'max', $max)
->attributeIfNotNull($step, 'step', $step);
}

/**
* Make a reset button.
*
Expand Down Expand Up @@ -418,12 +454,19 @@ public function textarea($name = null, $value = null)
*
* @param string|null $name
* @param string|null $value
* @param bool $format
*
* @return \Arcanedev\Html\Elements\Input
*/
public function time($name = null, $value = null)
public function time($name = null, $value = null, $format = true)
{
return $this->input('time', $name, $value);
$input = $this->input('time', $name, $value);

return $format
&& $input->hasAttribute('value')
&& ! empty($value = $input->getAttribute('value')->value())
? $input->value(static::formatDateTime($value, self::HTML_TIME_FORMAT))
: $input;
}

/**
Expand Down Expand Up @@ -474,4 +517,29 @@ public function dl(array $attributes = [])
{
return Elements\Dl::make()->attributes($attributes);
}

/* -----------------------------------------------------------------
| Other Methods
| -----------------------------------------------------------------
*/

/**
* Format the date/time value.
*
* @param string $value
* @param string $format
*
* @return string
*/
protected static function formatDateTime($value, string $format)
{
try {
return empty($value)
? $value
: (new DateTimeImmutable($value))->format($format);
}
catch (\Exception $e) {
return $value;
}
}
}
Loading

0 comments on commit 7c1e422

Please sign in to comment.