Skip to content

Commit

Permalink
Refactor code (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Feb 23, 2024
1 parent 658c94e commit e571bd6
Show file tree
Hide file tree
Showing 283 changed files with 3,183 additions and 2,742 deletions.
11 changes: 5 additions & 6 deletions infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
"PHPForge\\Html\\Attribute\\Custom\\HasAttributes::attributes",
"PHPForge\\Html\\Attribute\\Custom\\HasContent::content",
"PHPForge\\Html\\Attribute\\Custom\\HasEnclosedByLabel::enclosedByLabel",
"PHPForge\\Html\\Attribute\\Custom\\HasLabel::isNotLabel",
"PHPForge\\Html\\Attribute\\Custom\\HasLabel::labelAttributes",
"PHPForge\\Html\\Attribute\\Custom\\HasLabel::labelClass",
"PHPForge\\Html\\Attribute\\Custom\\HasLabel::labelContent",
"PHPForge\\Html\\Attribute\\Custom\\HasLabel::labelFor",
"PHPForge\\Html\\Attribute\\Custom\\HasLabel::notLabel",
"PHPForge\\Html\\Attribute\\Custom\\HasLabelCollection::label",
"PHPForge\\Html\\Attribute\\Custom\\HasLabelCollection::labelAttributes",
"PHPForge\\Html\\Attribute\\Custom\\HasLabelCollection::labelClass",
"PHPForge\\Html\\Attribute\\Custom\\HasLabelCollection::labelFor",
"PHPForge\\Html\\Attribute\\Custom\\HasLabelCollection::notLabel",
"PHPForge\\Html\\Attribute\\Field\\HasGenerateField::generateField",
"PHPForge\\Html\\Attribute\\HasClass::class",
"PHPForge\\Html\\Attribute\\HasId::getId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@

namespace PHPForge\Html\Attribute\Component;

use PHPForge\Html\Helper\CssClass;
use PHPForge\Html\Helper\Encode;
use PHPForge\Html\Helper\{CssClass, Encode};
use PHPForge\Widget\ElementInterface;

/**
* Is used by widgets that implement brand methods.
* Is used by widgets that implement brand collection.
*/
trait HasBrand
trait HasBrandCollection
{
protected bool $brandContainer = false;
protected array $brandContainerAttributes = [];
Expand Down
44 changes: 0 additions & 44 deletions src/Attribute/Component/HasFirstAndLastItemClass.php

This file was deleted.

28 changes: 28 additions & 0 deletions src/Attribute/Component/HasFirstItemClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Attribute\Component;

/**
* Is used by widgets that implement the first item class method.
*/
trait HasFirstItemClass
{
protected string $firstItemClass = '';

/**
* Set the first item class.
*
* @param string $value The `CSS` class that will be assigned to the first item.
*
* @return static A new instance of the current class with the specified first item class.
*/
public function firstItemClass(string $value): static
{
$new = clone $this;
$new->firstItemClass = $value;

return $new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@
namespace PHPForge\Html\Attribute\Component;

use InvalidArgumentException;
use PHPForge\Html\Helper\CssClass;
use PHPForge\Html\Svg;
use PHPForge\Html\Tag;
use PHPForge\Html\{Helper\CssClass, Svg, Tag};

/**
* Is used by widgets that implement the icon methods.
* Is used by widgets that implement the icon collection.
*/
trait HasIcon
trait HasIconCollection
{
protected bool $icon = true;
protected string $icon = '';
protected array $iconAttributes = [];
protected string $iconClass = '';
protected bool $iconContainer = false;
protected array $iconContainerAttributes = [];
protected string $iconContainerTag = 'div';
protected string $iconContent = '';
protected string $iconFilePath = '';
protected false|string $iconTag = 'i';
protected bool $isIcon = true;

/**
* @return string The icon of the menu item.
*/
public function getIcon(): string
{
return $this->icon;
}

/**
* @return array The `HTML` attributes of the icon of the menu item.
Expand All @@ -41,21 +47,13 @@ public function getIconContainerAttributes(): array
}

/**
* @return string The icon of the menu item.
*/
public function getIconContent(): string
{
return $this->iconContent;
}

/**
* Enable or disable the icon.
* Set the icon `HTML` content.
*
* @param bool $value `true` to enable the icon, `false` to disable it.
* @param string $value The icon `HTML` content.
*
* @return static A new instance of the current class with the specified icon value.
* @return static A new instance of the current class with the specified icon content.
*/
public function icon(bool $value): static
public function icon(string $value): static
{
$new = clone $this;
$new->icon = $value;
Expand Down Expand Up @@ -158,21 +156,6 @@ public function iconContainerClass(string $value, bool $override = false): stati
return $new;
}

/**
* Set the icon `HTML` content.
*
* @param string $value The icon `HTML` content.
*
* @return static A new instance of the current class with the specified icon content.
*/
public function iconContent(string $value): static
{
$new = clone $this;
$new->iconContent = $value;

return $new;
}

/**
* Set the icon file path.
*
Expand Down Expand Up @@ -210,27 +193,40 @@ public function iconTag(false|string $value): static
return $new;
}

/**
* Disable the icon.
*
* @return static A new instance of the current class with the icon disabled.
*/
public function notIcon(): static
{
$new = clone $this;
$new->isIcon = false;

return $new;
}

private function renderIconTag(): string
{
if (
$this->icon === false ||
($this->iconClass === '' && $this->iconContent === '' && $this->iconFilePath === '')
$this->isIcon === false ||
($this->icon === '' && $this->iconClass === '' && $this->iconFilePath === '')
) {
return '';
}

$iconTag = match ($this->iconTag) {
false => $this->iconContent,
false => $this->icon,
'svg' => Svg::widget()
->attributes($this->iconAttributes)
->class($this->iconClass)
->content($this->iconContent)
->content($this->icon)
->filePath($this->iconFilePath)
->render(),
default => Tag::widget()
->attributes($this->iconAttributes)
->class($this->iconClass)
->content($this->iconContent)
->content($this->icon)
->tagName($this->iconTag)
->render(),
};
Expand Down
28 changes: 28 additions & 0 deletions src/Attribute/Component/HasLastItemClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace PHPForge\Html\Attribute\Component;

/**
* Is used by widgets that implement the last item class method.
*/
trait HasLastItemClass
{
protected string $lastItemClass = '';

/**
* Sets the last item class.
*
* @param string $value The `CSS` class that will be assigned to the last item.
*
* @return static A new instance of the current class with the specified last item class.
*/
public function lastItemClass(string $value): static
{
$new = clone $this;
$new->lastItemClass = $value;

return $new;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
namespace PHPForge\Html\Attribute\Component;

use InvalidArgumentException;
use PHPForge\Html\Helper\CssClass;

use function in_array;
use function sprintf;
use PHPForge\Html\{Attribute\Custom\HasValidateInList, Helper\CssClass};

/**
* Is used by widgets that implement list methods.
* Is used by widgets that implement list collection.
*/
trait HasList
trait HasListCollection
{
use HasValidateInList;

protected array $listAttributes = [];
protected bool $listContainer = false;
protected array $listContainerAttributes = [];
Expand Down Expand Up @@ -104,12 +103,19 @@ public function listContainerClass(string $value, bool $override = false): stati
* @param false|string $value The list type. `ul` for an unordered list, `ol` for an ordered list, `false` to
* disable.
*
* @throws InvalidArgumentException If the value is not one of: "ul", "ol".
*
* @return static A new instance of the current class with the specified list type for tag `<ul>` or `<ol>`.
*/
public function listType(string|false $value): static
{
if ($value !== false && in_array($value, ['ul', 'ol'], true) === false) {
throw new InvalidArgumentException(sprintf('Invalid list type "%s".', $value));
if ($value !== false) {
$this->validateInList(
$value,
'Invalid value "%s" for the list type method. Allowed values are: "%s".',
'ol',
'ul',
);
}

$new = clone $this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use PHPForge\Html\Helper\CssClass;

/**
* Is used by widgets that implement list item methods.
* Is used by widgets that implement list item collection.
*/
trait HasListItem
trait HasListItemCollection
{
protected array $listItemAttributes = [];
protected bool $listItemTag = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use PHPForge\Html\Helper\CssClass;

/**
* Is used by widgets that implement container methods.
* Is used by widgets that implement container for list items collection.
*/
trait HasListItemContainer
trait HasListItemContainerCollection
{
protected bool $listItemContainer = false;
protected array $listItemContainerAttributes = [];
Expand Down
3 changes: 1 addition & 2 deletions src/Attribute/Component/HasMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

namespace PHPForge\Html\Attribute\Component;

use PHPForge\Html\Helper\Encode;
use PHPForge\Widget\ElementInterface;
use PHPForge\{Html\Helper\Encode, Widget\ElementInterface};

/**
* Is used by widgets that implement menu methods.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace PHPForge\Html\Attribute\Component;

/**
* Is used by widgets which implement the page methods.
* Is used by widgets which implement the page collection.
*/
trait HasPage
trait HasPageCollection
{
protected int $page = 1;
protected string $pageName = 'page';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
namespace PHPForge\Html\Attribute\Component;

/**
* Is used by widgets which implement the page size methods.
* Is used by widgets which implement the page size collection.
*/
trait HasPageSize
trait HasPageSizeCollection
{
protected int $pageSize = 10;
protected string $pageSizeName = 'page-size';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use PHPForge\Html\Helper\CssClass;

/**
* Is used by widgets which implement the summary methods.
* Is used by widgets which implement the summary collection.
*/
trait HasSummary
trait HasSummaryCollection
{
protected array $summaryAttributes = [];
protected string $summaryLabel = 'Page ';
Expand Down

0 comments on commit e571bd6

Please sign in to comment.