Skip to content

Commit

Permalink
Updating the package
Browse files Browse the repository at this point in the history
  • Loading branch information
arcanedev-maroc committed Mar 19, 2024
1 parent c9a1407 commit 81619a3
Show file tree
Hide file tree
Showing 27 changed files with 709 additions and 67 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"scripts": {
"test": "phpunit --colors=always",
"test:dox": "phpunit --testdox --colors=always",
"test:cov": "phpunit --coverage-html",
"test:ci": "phpunit --coverage-text",
"cs:fix": "pint -v",
"cs:test": "pint --test -v"
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Selectable.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface Selectable
/**
* Add the selected attribute.
*/
public function selected(bool $selected = true): static;
public function selected(): static;

/**
* Add the selected if it fulfill the condition.
Expand Down
4 changes: 2 additions & 2 deletions src/Elements/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Arcanedev\Html\Elements;

use Arcanedev\Html\Elements\Concerns\HasDisabledAttribute;
use Arcanedev\Html\Elements\Concerns\HasNameAttribute;
use Arcanedev\Html\Elements\Concerns\HasTypeAttribute;
use Arcanedev\Html\Elements\Concerns\HasValueAttribute;
Expand All @@ -20,10 +21,9 @@ class Button extends HtmlElement
| -----------------------------------------------------------------
*/

use HasDisabledAttribute;
use HasNameAttribute;

use HasTypeAttribute;

use HasValueAttribute;

/* -----------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions src/Elements/Concerns/HasChildElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function initChildren(): static
*
* @return $this
*/
public function children(mixed $children, ?Closure $mapper = null): static
public function children(mixed $children, Closure|array|null $mapper = null): static
{
return $this->addChild($children, $mapper);
}
Expand All @@ -80,7 +80,7 @@ public function children(mixed $children, ?Closure $mapper = null): static
*
* @return $this
*/
public function addChild(mixed $child, ?Closure $mapper = null): static
public function addChild(mixed $child, Closure|array|null $mapper = null): static
{
if ($child === null) {
return $this;
Expand All @@ -98,7 +98,7 @@ public function addChild(mixed $child, ?Closure $mapper = null): static
*
* @return $this
*/
public function setNewChildren(mixed $children, ?Closure $mapper = null): static
public function setNewChildren(mixed $children, Closure|array|null $mapper = null): static
{
return tap(clone $this)
->initChildren()
Expand All @@ -110,7 +110,7 @@ public function setNewChildren(mixed $children, ?Closure $mapper = null): static
*
* @return $this
*/
public function prependChild(mixed $children, ?Closure $mapper = null): static
public function prependChild(mixed $children, Closure|array|null $mapper = null): static
{
return $this->prependChildren($children, $mapper);
}
Expand All @@ -120,7 +120,7 @@ public function prependChild(mixed $children, ?Closure $mapper = null): static
*
* @return $this
*/
public function prependChildren(mixed $children, ?Closure $mapper = null): static
public function prependChildren(mixed $children, Closure|array|null $mapper = null): static
{
return tap(clone $this, function (HtmlElement $elt) use ($children, $mapper): void {
$elt->getChildren()
Expand Down
9 changes: 9 additions & 0 deletions src/Elements/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@

namespace Arcanedev\Html\Elements;

use Arcanedev\Html\Elements\Concerns\HasDisabledAttribute;

/**
* Class Fieldset
*
* @author ARCANEDEV <[email protected]>
*/
class Fieldset extends HtmlElement
{
/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
*/

use HasDisabledAttribute;

/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
Expand Down
3 changes: 1 addition & 2 deletions src/Elements/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ class File extends HtmlElement
*/

use HasAutofocusAttribute;

use HasNameAttribute;

use HasRequiredAttribute;

/* -----------------------------------------------------------------
| Constants
| -----------------------------------------------------------------
Expand Down
36 changes: 21 additions & 15 deletions src/Elements/HtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
namespace Arcanedev\Html\Elements;

use Arcanedev\Html\Contracts\Elements\HtmlElement as HtmlElementContract;
use Arcanedev\Html\Elements\Concerns\{HasAttributes, HasChildElements, HasConditionalMethods};
use Arcanedev\Html\Entities\Attributes\ClassAttribute;
use Arcanedev\Html\Exceptions\InvalidHtmlException;
use Arcanedev\Html\Exceptions\MissingTagException;
use Illuminate\Support\Collection;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Support\{Collection, HtmlString, Str, Traits\Macroable};

/**
* Class HtmlElement
Expand All @@ -29,12 +27,12 @@ class HtmlElement implements HtmlElementContract
| -----------------------------------------------------------------
*/

use Concerns\HasAttributes,
Concerns\HasChildElements,
Concerns\HasConditionalMethods,
Macroable {
__call as __callMacro;
}
use HasAttributes;
use HasChildElements;
use HasConditionalMethods;
use Macroable {
__call as __callMacro;
}

/* -----------------------------------------------------------------
| Properties
Expand Down Expand Up @@ -200,11 +198,9 @@ public function text(mixed $text, bool $doubleEncode = true): static
}

/**
* Add an html child/children.
* Add a html child/children.
*
* @return $this
*
* @throws InvalidHtmlException
*/
public function html(mixed $html): static
{
Expand Down Expand Up @@ -295,11 +291,21 @@ protected function setTag(string $tag): static
* @throws MissingTagException
*/
protected function getTag(): string
{
$this->ensureHasTag();

return $this->tag;
}

/**
* Ensure the tag property is defined.
*
* @throws MissingTagException
*/
protected function ensureHasTag(): void
{
if (empty($this->tag)) {
throw MissingTagException::onClass(static::class);
}

return $this->tag;
}
}
3 changes: 3 additions & 0 deletions src/Elements/I.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ class I extends HtmlElement
| -----------------------------------------------------------------
*/

/**
* The tag type.
*/
protected string $tag = 'i';
}
9 changes: 5 additions & 4 deletions src/Elements/ListElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
*/
abstract class ListElement extends HtmlElement
{
/**
* Make an item.
*/
abstract protected function makeItem(mixed $value, array $attributes): HtmlElement;
/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
*/

/**
* Make an item.
*/
abstract protected function makeItem(mixed $value, array $attributes): HtmlElement;

/**
* Add an item.
*
Expand Down
9 changes: 9 additions & 0 deletions src/Elements/Optgroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@

namespace Arcanedev\Html\Elements;

use Arcanedev\Html\Elements\Concerns\HasDisabledAttribute;

/**
* Class Optgroup
*
* @author ARCANEDEV <[email protected]>
*/
class Optgroup extends HtmlElement
{
/* -----------------------------------------------------------------
| Traits
| -----------------------------------------------------------------
*/

use HasDisabledAttribute;

/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
Expand Down
13 changes: 6 additions & 7 deletions src/Elements/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Option extends HtmlElement implements Selectable
*/

use HasDisabledAttribute;

use HasValueAttribute;

/* -----------------------------------------------------------------
Expand All @@ -44,24 +43,24 @@ class Option extends HtmlElement implements Selectable
*/
public function selectedIf(bool $condition): static
{
return $condition ? $this->selected() : $this->unselected();
return $condition
? $this->attribute('selected')
: $this->forgetAttribute('selected');
}

/**
* Add the selected attribute.
*/
public function selected(bool $selected = true): static
public function selected(): static
{
return $selected
? $this->attribute('selected')
: $this->forgetAttribute('selected');
return $this->selectedIf(true);
}

/**
* Remove the selected attribute.
*/
public function unselected(): static
{
return $this->selected(false);
return $this->selectedIf(false);
}
}
18 changes: 18 additions & 0 deletions src/Elements/P.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Arcanedev\Html\Elements;

class P extends HtmlElement
{
/* -----------------------------------------------------------------
| Properties
| -----------------------------------------------------------------
*/

/**
* The tag type.
*/
protected string $tag = 'p';
}
6 changes: 1 addition & 5 deletions src/Elements/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ class Select extends HtmlElement
*/

use HasAutofocusAttribute;

use HasDisabledAttribute;

use HasNameAttribute;

use HasReadonlyAttribute;

use HasRequiredAttribute;

/* -----------------------------------------------------------------
Expand Down Expand Up @@ -76,7 +72,7 @@ public function options(iterable $options, array $attributes = [], array $groupA
{
return $this->children(
$options,
fn($text, $value) => is_array($text)
fn($text, $value) => is_array($text) || $text instanceof Collection
? $this->makeOptionsGroup($value, $text, $attributes, $groupAttributes[$value] ?? [])
: $this->makeOption($value, $text, $attributes[$value] ?? [])
);
Expand Down
6 changes: 0 additions & 6 deletions src/Elements/Textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,11 @@ class Textarea extends HtmlElement
*/

use HasAutofocusAttribute;

use HasDisabledAttribute;

use HasMinMaxLengthAttributes;

use HasNameAttribute;

use HasPlaceholderAttribute;

use HasReadonlyAttribute;

use HasRequiredAttribute;

/* -----------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/Entities/ChildrenCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ChildrenCollection extends Collection implements Renderable
*
* @throws InvalidChildException
*/
public static function parse(mixed $children, ?Closure $mapper = null): static
public static function parse(mixed $children, Closure|array|null $mapper = null): static
{
return static::make($children)
->unless($mapper === null, fn(ChildrenCollection $items) => $items->map($mapper))
Expand Down
9 changes: 9 additions & 0 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Arcanedev\Html\Elements\Legend;
use Arcanedev\Html\Elements\Ol;
use Arcanedev\Html\Elements\Option;
use Arcanedev\Html\Elements\P;
use Arcanedev\Html\Elements\Select;
use Arcanedev\Html\Elements\Span;
use Arcanedev\Html\Elements\Textarea;
Expand Down Expand Up @@ -277,6 +278,14 @@ public function option(?string $text = null, mixed $value = null, bool $selected
->selectedIf($selected);
}

/**
* Make a paragraph tag.
*/
public function p(HtmlElement|string|null $content = null): P
{
return P::make()->html($content);
}

/**
* Make a password input.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Elements/ATest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function it_can_create(): void
{
static::assertHtmlStringEqualsHtmlString(
'<a></a>',
A::make()->toHtml()
A::make()
);
}

Expand Down
Loading

0 comments on commit 81619a3

Please sign in to comment.