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

Areablock: Accept array for setDataFromResource() #16773

Open
wants to merge 2 commits into
base: 11.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions models/Document/Editable/Area.php
Expand Up @@ -208,14 +208,20 @@ public function frontend(): void

public function setDataFromResource(mixed $data): static
{
if (is_string($data) && strlen($data) > 2) {
$data = Serialize::unserialize($data);
}

if (is_array($data)) {
$this->type = $data['type'] ?? null;
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
$processedData = $unserializedData;
} else {
throw new \InvalidArgumentException('Data must be a string or an array.');
}

$this->type = $processedData['type'] ?? null;

return $this;
}

Expand Down
18 changes: 12 additions & 6 deletions models/Document/Editable/Areablock.php
Expand Up @@ -22,8 +22,8 @@
use Pimcore\Model;
use Pimcore\Model\Document;
use Pimcore\Templating\Renderer\EditableRenderer;
use Pimcore\Tool;
use Pimcore\Tool\HtmlUtils;
use Pimcore\Tool\Serialize;

/**
* @method \Pimcore\Model\Document\Editable\Dao getDao()
Expand Down Expand Up @@ -248,14 +248,20 @@ protected function getEditableHandler(): EditableHandler

public function setDataFromResource(mixed $data): static
{
$unserializedData = Tool\Serialize::unserialize($data);

if (is_array($unserializedData)) {
$this->indices = $unserializedData;
if (is_array($data)) {
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
$processedData = $unserializedData;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could remove the else and just set $unserializedData = $data as default no?

Copy link
Contributor Author

@blankse blankse Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattamon This was possible, but I made it more type safe. It was possible to put a wrong type and nothing happens. I think it should be throw a InvalidArgumentException() like in the renderlet editable.

$this->indices = [];
throw new \InvalidArgumentException('Data must be a string or an array.');
}

$this->indices = $processedData;

return $this;
}

Expand Down
15 changes: 14 additions & 1 deletion models/Document/Editable/Block.php
Expand Up @@ -19,6 +19,7 @@
use Pimcore\Document\Editable\Block\BlockName;
use Pimcore\Model;
use Pimcore\Tool\HtmlUtils;
use Pimcore\Tool\Serialize;

/**
* @method \Pimcore\Model\Document\Editable\Dao getDao()
Expand Down Expand Up @@ -70,7 +71,19 @@ public function frontend()

public function setDataFromResource(mixed $data): static
{
$this->indices = \Pimcore\Tool\Serialize::unserialize($data);
if (is_array($data)) {
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
$processedData = $unserializedData;
} else {
throw new \InvalidArgumentException('Data must be a string or an array.');
}

$this->indices = $processedData;

return $this;
}
Expand Down
15 changes: 12 additions & 3 deletions models/Document/Editable/Embed.php
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Model\Document\Editable;

use Pimcore\Model;
use Pimcore\Tool\Serialize;

/**
* @method \Pimcore\Model\Document\Editable\Dao getDao()
Expand Down Expand Up @@ -94,11 +95,19 @@ public function admin()

public function setDataFromResource(mixed $data): static
{
if (!empty($data)) {
$data = \Pimcore\Tool\Serialize::unserialize($data);
if (is_array($data)) {
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
$processedData = $unserializedData;
} else {
throw new \InvalidArgumentException('Data must be a string or an array.');
}

$this->url = $data['url'];
$this->url = $processedData['url'] ?? null;

return $this;
}
Expand Down
23 changes: 13 additions & 10 deletions models/Document/Editable/Image.php
Expand Up @@ -268,8 +268,16 @@ public function frontend()

public function setDataFromResource(mixed $data): static
{
if (strlen($data) > 2) {
$data = Serialize::unserialize($data);
if (is_array($data)) {
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
$processedData = $unserializedData;
} else {
throw new \InvalidArgumentException('Data must be a string or an array.');
}

$rewritePath = function ($data) {
Expand All @@ -291,15 +299,10 @@ public function setDataFromResource(mixed $data): static
return $data;
};

if (array_key_exists('marker', $data) && is_array($data['marker']) && count($data['marker']) > 0) {
$data['marker'] = $rewritePath($data['marker']);
}

if (array_key_exists('hotspots', $data) && is_array($data['hotspots']) && count($data['hotspots']) > 0) {
$data['hotspots'] = $rewritePath($data['hotspots']);
}
$processedData['marker'] = $rewritePath($processedData['marker'] ?? []);
$processedData['hotspots'] = $rewritePath($processedData['hotspots'] ?? []);

$this->setData($data);
$this->setData($processedData);

return $this;
}
Expand Down
15 changes: 11 additions & 4 deletions models/Document/Editable/Link.php
Expand Up @@ -290,13 +290,20 @@ public function getClass(): mixed

public function setDataFromResource(mixed $data): static
{
if (is_string($data)) {
$data = Serialize::unserialize($data);
}
if (is_array($data) || is_null($data)) {
$this->data = $data;
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData) && !is_null($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array or null.');
}
$processedData = $unserializedData;
} else {
throw new \InvalidArgumentException('Data must be a string, an array or null.');
}

$this->data = $processedData;

return $this;
}

Expand Down
15 changes: 14 additions & 1 deletion models/Document/Editable/Multiselect.php
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Model\Document\Editable;

use Pimcore\Model;
use Pimcore\Tool\Serialize;

/**
* @method \Pimcore\Model\Document\Editable\Dao getDao()
Expand Down Expand Up @@ -58,7 +59,19 @@ public function getDataEditmode(): array

public function setDataFromResource(mixed $data): static
{
$this->values = \Pimcore\Tool\Serialize::unserialize($data);
if (is_array($data)) {
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
$processedData = $unserializedData;
} else {
throw new \InvalidArgumentException('Data must be a string or an array.');
}

$this->values = $processedData;

return $this;
}
Expand Down
15 changes: 12 additions & 3 deletions models/Document/Editable/Pdf.php
Expand Up @@ -19,6 +19,7 @@
use Pimcore\Logger;
use Pimcore\Model;
use Pimcore\Model\Asset;
use Pimcore\Tool\Serialize;

/**
* @method \Pimcore\Model\Document\Editable\Dao getDao()
Expand Down Expand Up @@ -109,11 +110,19 @@ public function checkValidity(): bool

public function setDataFromResource(mixed $data): static
{
if (!empty($data)) {
$data = \Pimcore\Tool\Serialize::unserialize($data);
if (is_array($data)) {
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
$processedData = $unserializedData;
} else {
throw new \InvalidArgumentException('Data must be a string or an array.');
}

$this->id = $data['id'];
$this->id = $processedData['id'] ?? null;

return $this;
}
Expand Down
19 changes: 14 additions & 5 deletions models/Document/Editable/Relation.php
Expand Up @@ -21,6 +21,7 @@
use Pimcore\Model\Asset;
use Pimcore\Model\Document;
use Pimcore\Model\Element;
use Pimcore\Tool\Serialize;

/**
* @method \Pimcore\Model\Document\Editable\Dao getDao()
Expand Down Expand Up @@ -108,13 +109,21 @@ public function frontend()

public function setDataFromResource(mixed $data): static
{
if (!empty($data)) {
$data = \Pimcore\Tool\Serialize::unserialize($data);
if (is_array($data)) {
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
$processedData = $unserializedData;
} else {
throw new \InvalidArgumentException('Data must be a string or an array.');
}

$this->id = $data['id'] ?? null;
$this->type = $data['type'] ?? null;
$this->subtype = $data['subtype'] ?? null;
$this->id = $processedData['id'] ?? null;
$this->type = $processedData['type'] ?? null;
$this->subtype = $processedData['subtype'] ?? null;

$this->setElement();

Expand Down
15 changes: 13 additions & 2 deletions models/Document/Editable/Relations.php
Expand Up @@ -21,6 +21,7 @@
use Pimcore\Model\DataObject;
use Pimcore\Model\Document;
use Pimcore\Model\Element;
use Pimcore\Tool\Serialize;

/**
* @method \Pimcore\Model\Document\Editable\Dao getDao()
Expand Down Expand Up @@ -113,10 +114,20 @@ public function frontend()

public function setDataFromResource(mixed $data): static
{
if ($data = \Pimcore\Tool\Serialize::unserialize($data)) {
$this->setDataFromEditmode($data);
if (is_array($data)) {
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
$processedData = $unserializedData;
} else {
throw new \InvalidArgumentException('Data must be a string or an array.');
}

$this->setDataFromEditmode($processedData);

return $this;
}

Expand Down
3 changes: 2 additions & 1 deletion models/Document/Editable/Renderlet.php
Expand Up @@ -25,6 +25,7 @@
use Pimcore\Model\DataObject;
use Pimcore\Model\Document;
use Pimcore\Model\Element;
use Pimcore\Tool\Serialize;

/**
* @method \Pimcore\Model\Document\Editable\Dao getDao()
Expand Down Expand Up @@ -158,7 +159,7 @@ public function setDataFromResource(mixed $data): static
if (is_array($data)) {
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = \Pimcore\Tool\Serialize::unserialize($data);
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
Expand Down
15 changes: 14 additions & 1 deletion models/Document/Editable/Table.php
Expand Up @@ -17,6 +17,7 @@
namespace Pimcore\Model\Document\Editable;

use Pimcore\Model;
use Pimcore\Tool\Serialize;

/**
* @method \Pimcore\Model\Document\Editable\Dao getDao()
Expand Down Expand Up @@ -65,7 +66,19 @@ public function frontend()

public function setDataFromResource(mixed $data): static
{
$this->data = \Pimcore\Tool\Serialize::unserialize($data);
if (is_array($data)) {
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
$processedData = $unserializedData;
} else {
throw new \InvalidArgumentException('Data must be a string or an array.');
}

$this->data = $processedData;

return $this;
}
Expand Down
22 changes: 15 additions & 7 deletions models/Document/Editable/Video.php
Expand Up @@ -330,15 +330,23 @@ public function admin()

public function setDataFromResource(mixed $data): static
{
if (is_string($data) && $data) {
$data = Serialize::unserialize($data);
if (is_array($data)) {
$processedData = $data;
} elseif (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
if (!is_array($unserializedData)) {
throw new \InvalidArgumentException('Unserialized data must be an array.');
}
$processedData = $unserializedData;
} else {
throw new \InvalidArgumentException('Data must be a string or an array.');
}

$this->id = $data['id'] ?? null;
$this->type = $data['type'] ?? null;
$this->poster = $data['poster'] ?? null;
$this->title = $data['title'] ?? '';
$this->description = $data['description'] ?? '';
$this->id = $processedData['id'] ?? null;
$this->type = $processedData['type'] ?? null;
$this->poster = $processedData['poster'] ?? null;
$this->title = $processedData['title'] ?? '';
$this->description = $processedData['description'] ?? '';

return $this;
}
Expand Down