Skip to content

Commit

Permalink
Optimize all setDataFromResource() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
blankse committed Apr 1, 2024
1 parent a0f421a commit df84246
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 52 deletions.
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
20 changes: 11 additions & 9 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,17 +248,19 @@ protected function getEditableHandler(): EditableHandler

public function setDataFromResource(mixed $data): static
{
if (is_string($data)) {
$unserializedData = 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 {
$unserializedData = $data;
throw new \InvalidArgumentException('Data must be a string or an array.');
}

if (is_array($unserializedData)) {
$this->indices = $unserializedData;
} else {
$this->indices = [];
}
$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
2 changes: 2 additions & 0 deletions models/Document/Editable/Date.php
Expand Up @@ -86,6 +86,8 @@ public function setDataFromResource(mixed $data): static
{
if ($data) {
$this->setDateFromTimestamp((int)$data);
} else {
$this->date = null;
}

return $this;
Expand Down
9 changes: 6 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,13 @@ public function admin()

public function setDataFromResource(mixed $data): static
{
if (!empty($data)) {
$data = \Pimcore\Tool\Serialize::unserialize($data);
if (is_string($data)) {
$unserializedData = Serialize::unserialize($data);
} else {
$unserializedData = $data;
}

$this->url = $data['url'];
$this->url = $unserializedData['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

0 comments on commit df84246

Please sign in to comment.