100 lines
2.3 KiB
PHP
100 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace KupShop\FeedGeneratorBundle\Wrapper;
|
|
|
|
use KupShop\FeedGeneratorBundle\Utils\ExternalFeedCache;
|
|
use KupShop\FeedGeneratorBundle\XSD;
|
|
use KupShop\FeedsBundle\Wrapper\BaseWrapper;
|
|
|
|
class ExternalFeedObjectWrapper extends BaseWrapper
|
|
{
|
|
private array $feedRow;
|
|
private XSD\Parser $parser;
|
|
private array $serialized = [];
|
|
|
|
public function __construct(XSD\Parser $parser)
|
|
{
|
|
$this->parser = $parser;
|
|
}
|
|
|
|
/**
|
|
* Override without type check.
|
|
*
|
|
* @return ExternalFeedObjectWrapper
|
|
*/
|
|
public function setObject($object)
|
|
{
|
|
$this->object = $object;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setFeedRow(array $feedRow)
|
|
{
|
|
$this->feedRow = $feedRow;
|
|
$this->serialize();
|
|
}
|
|
|
|
private function serialize(): void
|
|
{
|
|
$cache = new ExternalFeedCache($this->feedRow);
|
|
$this->parser->loadFile($cache->getDescriptionUrl());
|
|
$this->serialized = $this->prepareForPrint(
|
|
$this->parser->getFeedRootElement()->serialize()
|
|
);
|
|
$this->serialized['__search'] = '';
|
|
}
|
|
|
|
private function prepareForPrint(array $array): array
|
|
{
|
|
$flattened = [];
|
|
foreach ($array['children'] ?? [] as $child) {
|
|
if (!empty($child['children'])) {
|
|
$flattened[$child['name']] = $this->prepareForPrint($child);
|
|
} else {
|
|
$flattened[$child['name']] = '';
|
|
}
|
|
}
|
|
|
|
return $flattened;
|
|
}
|
|
|
|
public function offsetExists($offset): bool
|
|
{
|
|
return isset($this->object[$offset]);
|
|
}
|
|
|
|
public function offsetGet($offset): mixed
|
|
{
|
|
return $this->object[$offset] ?? null;
|
|
}
|
|
|
|
public function offsetSet($offset, $value): void
|
|
{
|
|
$this->object[$offset] = $value;
|
|
}
|
|
|
|
public function offsetUnset($offset): void
|
|
{
|
|
unset($this->object[$offset]);
|
|
}
|
|
|
|
/**
|
|
* Loads field names into JS global context.
|
|
*/
|
|
public function getFieldsAndMethodsNames(): array
|
|
{
|
|
return array_keys($this->serialized);
|
|
}
|
|
|
|
public function getRawFieldsAndMethods(): array
|
|
{
|
|
return array_map(fn ($f) => ['name' => $f, 'method' => false], $this->getFieldsAndMethodsNames());
|
|
}
|
|
|
|
public function getFieldsAndMethods(): array
|
|
{
|
|
return [];
|
|
}
|
|
}
|