Skip to content

Commit

Permalink
Add override parameter to class methods. (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 19, 2023
1 parent 89460ce commit f586de0
Show file tree
Hide file tree
Showing 32 changed files with 346 additions and 178 deletions.
10 changes: 6 additions & 4 deletions src/Attribute/Component/HasBrand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ public function brandContainerAttributes(array $values): static
* Set the `CSS` class for the brand container.
*
* @param string $value The `CSS` class for the brand container.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified brand container class.
*/
public function brandContainerClass(string $value): static
public function brandContainerClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->brandContainerAttributes, $value);
CssClass::add($new->brandContainerAttributes, $value, $override);

return $new;
}
Expand Down Expand Up @@ -132,13 +133,14 @@ public function brandLinkAttributes(array $value): static
* Set the `CSS` class for the brand link.
*
* @param string $value The `CSS` class for the brand link.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified brand link class.
*/
public function brandLinkClass(string $value): static
public function brandLinkClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->brandLinkAttributes, $value);
CssClass::add($new->brandLinkAttributes, $value, $override);

return $new;
}
Expand Down
10 changes: 6 additions & 4 deletions src/Attribute/Component/HasIcon.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ public function iconAttributes(array $values): static
* Set the `CSS` class for the icon.
*
* @param string $value The icon CSS class.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified icon CSS class.
*/
public function iconClass(string $value): static
public function iconClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->iconAttributes, $value);
CssClass::add($new->iconAttributes, $value, $override);

return $new;
}
Expand Down Expand Up @@ -103,13 +104,14 @@ public function iconContainerAttributes(array $values): static
* Set the `CSS` class for the icon container.
*
* @param string $value The CSS class name.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified icon container CSS class.
*/
public function iconContainerClass(string $value): static
public function iconContainerClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->iconContainerAttributes, $value);
CssClass::add($new->iconContainerAttributes, $value, $override);

return $new;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Attribute/Component/HasLinkAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ public function linkAttributes(array $values): static
* Set the `CSS` class for the link tag.
*
* @param string $value The `CSS` class for the link tag.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified link class.
*/
public function linkClass(string $value): static
public function linkClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->linkAttributes, $value);
CssClass::add($new->linkAttributes, $value, $override);

return $new;
}
Expand Down
10 changes: 6 additions & 4 deletions src/Attribute/Component/HasList.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ public function listAttributes(array $values): static
* Set the `CSS` class for the `<ul>` or `<ol>` tag.
*
* @param string $value The CSS class name.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified `<ul>` or `<ol>` class.
*/
public function listClass(string $value): static
public function listClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->listAttributes, $value);
CssClass::add($new->listAttributes, $value, $override);

return $new;
}
Expand Down Expand Up @@ -84,13 +85,14 @@ public function listContainerAttributes(array $values): static
* Set the `CSS` class for the container for tag `<ul>` or `<ol>`.
*
* @param string $value The CSS class name.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified container class for tag `<ul>` or `<ol>`.
*/
public function listContainerClass(string $value): static
public function listContainerClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->listContainerAttributes, $value);
CssClass::add($new->listContainerAttributes, $value, $override);

return $new;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Attribute/Component/HasListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ public function listItemAttributes(array $values): static
* Set the `CSS` class for tag `<li>`.
*
* @param string $value The CSS class name.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified class for tag `<li>`.
*/
public function listItemClass(string $value): static
public function listItemClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->listItemAttributes, $value);
CssClass::add($new->listItemAttributes, $value, $override);

return $new;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Attribute/Component/HasListItemContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ public function listItemContainerAttributes(array $values = []): static
* Set the `CSS` class for the container for list items.
*
* @param string $value The CSS class name.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified container class for list items.
*/
public function listItemContainerClass(string $value): static
public function listItemContainerClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->listItemContainerAttributes, $value);
CssClass::add($new->listItemContainerAttributes, $value, $override);

return $new;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Attribute/Component/HasSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public function summaryAttributes(array $values): static
* Set the `CSS` `HTML` class attribute for summary container.
*
* @param string $value The `CSS` attribute for summary container.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified class value for summary container.
*/
public function summaryClass(string $value): static
public function summaryClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->summaryAttributes, $value);
CssClass::add($new->summaryAttributes, $value, $override);

return $new;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Attribute/Component/HasToggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ public function toggleAttributes(array $values): static
* Set the `CSS` class for the toggle.
*
* @param string $value The `CSS` class for the toggle.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified toggle class.
*/
public function toggleClass(string $value): static
public function toggleClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->toggleAttributes, $value);
CssClass::add($new->toggleAttributes, $value, $override);

return $new;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Attribute/Custom/HasContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ public function containerAttributes(array $values = []): static
* Set the `CSS` class for the container.
*
* @param string $value The CSS class name.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified container class.
*/
public function containerClass(string $value): static
public function containerClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->containerAttributes, $value);
CssClass::add($new->containerAttributes, $value, $override);

return $new;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Attribute/Custom/HasContainerMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,14 @@ public function containerMenuAttributes(array $values = []): static
* Set the `CSS` class for the container menu.
*
* @param string $value The CSS class name.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified container menu class.
*/
public function containerMenuClass(string $value): static
public function containerMenuClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->containerMenuAttributes, $value);
CssClass::add($new->containerMenuAttributes, $value, $override);

return $new;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Attribute/Custom/HasLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ public function labelAttributes(array $values): static
* Set the `CSS` class for the label.
*
* @param string $value The value of the class attribute.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified label class.
*/
public function labelClass(string $value): static
public function labelClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->labelAttributes, $value);
CssClass::add($new->labelAttributes, $value, $override);

return $new;
}
Expand Down
10 changes: 6 additions & 4 deletions src/Attribute/Custom/HasPrefixAndSuffix.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ public function prefixContainerAttributes(array $values): static
* Set the `CSS` class for the prefix container.
*
* @param string $value The CSS class name.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified prefix container class.
*/
public function prefixContainerClass(string $value): static
public function prefixContainerClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->prefixContainerAttributes, $value);
CssClass::add($new->prefixContainerAttributes, $value, $override);

return $new;
}
Expand Down Expand Up @@ -153,13 +154,14 @@ public function suffixContainerAttributes(array $values): static
* Set the `CSS` class for the suffix container.
*
* @param string $value The CSS class name.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified suffix container class.
*/
public function suffixContainerClass(string $value): static
public function suffixContainerClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->suffixContainerAttributes, $value);
CssClass::add($new->suffixContainerAttributes, $value, $override);

return $new;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Attribute/Custom/HasUnchecked.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ public function uncheckAttributes(array $values): static
/**
* Set the `CSS` class for the unchecked element.
*
* @param string $class The CSS class name.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified unchecked class.
*/
public function uncheckClass(string $class): static
public function uncheckClass(string $class, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->uncheckAttributes, $class);
CssClass::add($new->uncheckAttributes, $class, $override);

return $new;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Attribute/HasClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ trait HasClass
* Set the `CSS` `HTML` class attribute.
*
* @param string $value The `CSS` attribute of the widget.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified class value.
*
* @link https://html.spec.whatwg.org/#classes
*/
public function class(string $value): static
public function class(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->attributes, $value);
CssClass::add($new->attributes, $value, $override);

return $new;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Attribute/Tag/HasItemsAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ public function itemsAttributes(array $values = []): static
* Set the `CSS` `HTML` class attribute for the items.
*
* @param string $value The `CSS` attribute for the items.
* @param bool $override If `true` the value will be overridden.
*
* @return static A new instance of the current class with the specified class value for the items.
*/
public function itemsClass(string $value): static
public function itemsClass(string $value, bool $override = false): static
{
$new = clone $this;
CssClass::add($new->itemsAttributes, $value);
CssClass::add($new->itemsAttributes, $value, $override);

return $new;
}
Expand Down
43 changes: 25 additions & 18 deletions src/Helper/CssClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,33 @@ final class CssClass
*
* If the CSS class is already in the options, it will not be added again. If class specification at given options
* is an array, and some class placed there with the named (string) key, overriding of such a key will have no
* effect. For example:
*
* ```php
* $attributes = ['class' => ['persistent' => 'initial']];
*
* // ['class' => ['persistent' => 'initial']];
* $cssClass = new CssClass();
* $cssClass->add($attributes, ['persistent' => 'override']);
* ```
*
* {@see removeCssClass()}
* effect.
*
* @param array $attributes The attributes to be modified.
* @param string|string[] $class The CSS class(es) to be added.
* @param bool $override Whether to override existing CSS class(es) with new one.
*/
public static function add(array &$attributes, array|string $class): void
public static function add(array &$attributes, array|string $class, bool $override = false): void
{
if (empty($class)) {
return;
}

if ($override) {
$classArray = is_array($class)
? $class
: preg_split('/\s+/', $class, flags: PREG_SPLIT_NO_EMPTY);

$attributes['class'] = implode(' ', $classArray);

return;
}

if (isset($attributes['class'])) {
/** @psalm-var string[] $existingClasses */
$existingClasses = is_array($attributes['class'])
? $attributes['class']
: preg_split('/\s+/', (string) $attributes['class'], -1, PREG_SPLIT_NO_EMPTY);
: preg_split('/\s+/', (string) $attributes['class'], flags: PREG_SPLIT_NO_EMPTY);

$newClasses = is_array($class)
? $class
Expand All @@ -50,13 +55,15 @@ public static function add(array &$attributes, array|string $class): void
$mergedClasses = self::merge($existingClasses, $newClasses);

$attributes['class'] = implode(' ', $mergedClasses);
} elseif ($class !== [] && $class !== '') {
$classArray = is_array($class)
? $class
: preg_split('/\s+/', $class, -1, PREG_SPLIT_NO_EMPTY);

$attributes['class'] = implode(' ', $classArray);
return;
}

$classArray = is_array($class)
? $class
: preg_split('/\s+/', $class, flags: PREG_SPLIT_NO_EMPTY);

$attributes['class'] = implode(' ', $classArray);
}

/**
Expand Down

0 comments on commit f586de0

Please sign in to comment.