Skip to content

Commit

Permalink
deep clone
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Sep 13, 2024
1 parent dbfbdcd commit 6ab58ec
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/OpenGraph/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Elegantly\Seo\Contracts\Taggable;
use Elegantly\Seo\SeoTags;
use Elegantly\Seo\Tags\Meta;
use Elegantly\Seo\Traits\DeepClone;

class Locale implements Taggable
{
use DeepClone;

/**
* @param string[] $alternate
*/
Expand Down
3 changes: 3 additions & 0 deletions src/OpenGraph/OpenGraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
use Elegantly\Seo\OpenGraph\Verticals\Website;
use Elegantly\Seo\SeoTags;
use Elegantly\Seo\Tags\Meta;
use Elegantly\Seo\Traits\DeepClone;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Request;

class OpenGraph implements Taggable
{
use DeepClone;

public function __construct(
public string $title,
public string $url,
Expand Down
3 changes: 3 additions & 0 deletions src/OpenGraph/Verticals/Vertical.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
use Elegantly\Seo\Contracts\Taggable;
use Elegantly\Seo\SeoTags;
use Elegantly\Seo\Tags\Meta;
use Elegantly\Seo\Traits\DeepClone;
use Illuminate\Support\Arr;

abstract class Vertical implements Taggable
{
use DeepClone;

abstract public function getType(): string;

public function getNamespace(): string
Expand Down
3 changes: 3 additions & 0 deletions src/Schemas/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
use Elegantly\Seo\Contracts\Taggable;
use Elegantly\Seo\SeoTags;
use Elegantly\Seo\Tags\Script;
use Elegantly\Seo\Traits\DeepClone;
use Illuminate\Support\Collection;

/**
* @extends Collection<int|string, mixed>
*/
class Schema extends Collection implements Taggable
{
use DeepClone;

public function toTags(): SeoTags
{
return new SeoTags([
Expand Down
2 changes: 2 additions & 0 deletions src/SeoManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Elegantly\Seo\Schemas\WebPage;
use Elegantly\Seo\Standard\Alternate;
use Elegantly\Seo\Standard\Standard;
use Elegantly\Seo\Traits\DeepClone;
use Elegantly\Seo\Twitter\Cards\Card;
use Elegantly\Seo\Twitter\Cards\Summary;
use Illuminate\Contracts\Support\Htmlable;
Expand All @@ -20,6 +21,7 @@
class SeoManager implements Htmlable, Stringable, Taggable
{
use Conditionable;
use DeepClone;

/**
* @param null|Schema[] $schemas
Expand Down
3 changes: 3 additions & 0 deletions src/SeoTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Elegantly\Seo;

use Elegantly\Seo\Tags\TagVoid;
use Elegantly\Seo\Traits\DeepClone;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Collection;

Expand All @@ -11,6 +12,8 @@
*/
class SeoTags extends Collection implements Htmlable
{
use DeepClone;

public function toHtml(): string
{
return $this->map(fn (TagVoid $tag) => $tag->toHtml())->join("\n");
Expand Down
3 changes: 3 additions & 0 deletions src/Standard/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
use Elegantly\Seo\Tags\Link;
use Elegantly\Seo\Tags\Meta;
use Elegantly\Seo\Tags\Title;
use Elegantly\Seo\Traits\DeepClone;
use Illuminate\Support\Facades\Request;

/**
* @see https://developer.mozilla.org/fr/docs/Web/HTML/Element/meta/name
*/
class Standard implements Taggable
{
use DeepClone;

/**
* @param null|string|string[] $keywords
* @param null|Alternate[] $alternates
Expand Down
3 changes: 3 additions & 0 deletions src/Tags/TagVoid.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Elegantly\Seo\Tags;

use Elegantly\Seo\Traits\DeepClone;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Collection;

abstract class TagVoid implements Htmlable
{
use DeepClone;

public string $tag;

/**
Expand Down
25 changes: 25 additions & 0 deletions src/Traits/DeepClone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Elegantly\Seo\Traits;

trait DeepClone
{
public function __clone()
{
foreach (get_object_vars($this) as $property => $value) {
if (is_object($value)) {
$this->{$property} = clone $value;
}

if (is_array($value)) {
$this->{$property} = array_map(function ($item) {
if (is_object($item)) {
return clone $item;
}

return $item;
}, $value);
}
}
}
}
3 changes: 3 additions & 0 deletions src/Twitter/Cards/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
use Elegantly\Seo\Contracts\Taggable;
use Elegantly\Seo\SeoTags;
use Elegantly\Seo\Tags\Meta;
use Elegantly\Seo\Traits\DeepClone;
use Elegantly\Seo\Twitter\Image;

abstract class Card implements Taggable
{
use DeepClone;

public function toTags(): SeoTags
{
$tags = new SeoTags;
Expand Down
53 changes: 53 additions & 0 deletions tests/Units/SeoManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
use Elegantly\Seo\OpenGraph\Locale;
use Elegantly\Seo\OpenGraph\OpenGraph;
use Elegantly\Seo\OpenGraph\Verticals\Website;
use Elegantly\Seo\Schemas\Schema;
use Elegantly\Seo\Schemas\WebPage;
use Elegantly\Seo\SeoManager;
use Elegantly\Seo\SeoTags;
use Elegantly\Seo\Standard\Alternate;
use Elegantly\Seo\Standard\Standard;
use Elegantly\Seo\Tags\Meta;
use Elegantly\Seo\Twitter\Cards\Summary;
use Elegantly\Seo\Twitter\Image as TwitterImage;

Expand Down Expand Up @@ -80,3 +84,52 @@
'<meta name="twitter:image:alt" content="Twitter example image" />',
]));
});

it('can clone itself with new references', function () {
$base = new SeoManager(
standard: new Standard(
title: 'foo',
canonical: 'bar',
alternates: [
new Alternate(
hreflang: 'foo',
href: 'bar'
),
]
),
opengraph: new OpenGraph(
title: 'foo',
url: 'bar',
image: new Image(
url: 'foo',
)
),
twitter: new Summary(
title: 'foo',
),
webpage: new WebPage,
schemas: [
new Schema,
],
customTags: new SeoTags([
new Meta(
name: 'foo',
content: 'bar'
),
])
);

$clone = clone $base;

expect($clone)->not->toBe($base);

expect($clone->standard)->not->toBe($base->standard);
expect($clone->opengraph)->not->toBe($base->opengraph);
expect($clone->twitter)->not->toBe($base->twitter);
expect($clone->webpage)->not->toBe($base->webpage);
expect($clone->schemas)->not->toBe($base->schemas);
expect($clone->schemas[0])->not->toBe($base->schemas[0]);
expect($clone->customTags)->not->toBe($base->customTags);
expect($clone->customTags[0])->not->toBe($base->customTags[0]);
expect($clone->customTags[0]->properties)->not->toBe($base->customTags[0]->properties);
});

0 comments on commit 6ab58ec

Please sign in to comment.