Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone attributes #110

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions src/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,29 +365,19 @@ public function setPrefix($prefix)
/**
* Register callback for an attribute
*
* @param string $name Name of the attribute to register the callback for
* @param callable $callback Callback to call when retrieving the attribute
* @param callable $setterCallback Callback to call when setting the attribute
* @param string $name Name of the attribute to register the callback for
* @param ?callable $callback Callback to call when retrieving the attribute
* @param ?callable $setterCallback Callback to call when setting the attribute
*
* @return $this
*
* @throws InvalidArgumentException If $callback is not callable or if $setterCallback is set and not callable
*/
public function registerAttributeCallback($name, $callback, $setterCallback = null)
public function registerAttributeCallback(string $name, ?callable $callback, ?callable $setterCallback = null): self
{
if ($callback !== null) {
if (! is_callable($callback)) {
throw new InvalidArgumentException(__METHOD__ . ' expects a callable callback');
}

$this->callbacks[$name] = $callback;
}

if ($setterCallback !== null) {
if (! is_callable($setterCallback)) {
throw new InvalidArgumentException(__METHOD__ . ' expects a callable setterCallback');
}

$this->setterCallbacks[$name] = $setterCallback;
}

Expand Down Expand Up @@ -518,4 +508,11 @@ public function getIterator(): Traversable
{
return new ArrayIterator($this->attributes);
}

public function __clone()
{
foreach ($this->attributes as &$attribute) {
$attribute = clone $attribute;
}
}
}
9 changes: 9 additions & 0 deletions src/BaseHtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,13 @@ public function renderUnwrapped()
$tag
);
}

public function __clone()
{
parent::__clone();

if ($this->attributes !== null) {
$this->attributes = clone $this->attributes;
}
}
}
45 changes: 45 additions & 0 deletions tests/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace ipl\Tests\Html;

use ipl\Html\Attribute;
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\HtmlString;
use ipl\Html\ValidHtml;

class AttributesTest extends TestCase
{
Expand Down Expand Up @@ -113,4 +116,46 @@ function ($v) use (&$value) {

$this->assertEquals(' foo="bar rab" bar="foo"', $attributes->render());
}

public function testClone(): void
{
$original = Attributes::create([
'class' => 'original-class',
'value' => 'original-value'
]);

$clone = clone $original;
$clone->get('class')->setValue('clone-class');
$clone->get('name')->setValue('clone-name');
$clone->remove('value');

$cloneCone = clone $clone;
$cloneCone->get('class')->addValue('clone-clone-class');
$cloneCone->get('name')->setValue('clone-clone-name');
$cloneCone->get('value')->setValue('clone-clone-value');

$this->assertSame('original-class', $original->get('class')->getValue());
$this->assertSame('original-value', $original->get('value')->getValue());
$this->assertNull($original->get('name')->getValue());
$this->assertSame(
' class="original-class" value="original-value"',
$original->render()
);

$this->assertSame('clone-class', $clone->get('class')->getValue());
$this->assertNull($clone->get('value')->getValue());
$this->assertSame('clone-name', $clone->get('name')->getValue());
$this->assertSame(
' class="clone-class" name="clone-name"',
$clone->render()
);

$this->assertSame(['clone-class', 'clone-clone-class'], $cloneCone->get('class')->getValue());
$this->assertSame('clone-clone-name', $cloneCone->get('name')->getValue());
$this->assertSame('clone-clone-value', $cloneCone->get('value')->getValue());
$this->assertSame(
' class="clone-class clone-clone-class" name="clone-clone-name" value="clone-clone-value"',
$cloneCone->render()
);
}
}