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

Implement NodeParser and ArticleParser #2

Merged
merged 17 commits into from
Aug 26, 2024
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
8 changes: 7 additions & 1 deletion .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ jobs:
env:
SYMFONY_DEPRECATIONS_HELPER: weak

- php-version: '8.4'
dependency-versions: 'highest'
composer-options: '--ignore-platform-reqs'
env:
SYMFONY_DEPRECATIONS_HELPER: weak

services:
mysql:
image: mysql:8.0
Expand Down Expand Up @@ -77,7 +83,7 @@ jobs:
- name: Install and configure PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
php-version: 8.3
extensions: ctype, iconv, mysql

- name: Install composer dependencies
Expand Down
20 changes: 20 additions & 0 deletions PhpcrMigration/Application/Exception/InvalidPathException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\PhpcrMigrationBundle\PhpcrMigration\Application\Exception;

class InvalidPathException extends \RuntimeException
{
public function __construct(string $attributeName)
{
parent::__construct('Path not found. Invalid path attribute name "' . $attributeName . '" provided.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\PhpcrMigrationBundle\PhpcrMigration\Application\Exception;

class PersisterNotFoundException extends \RuntimeException
{
public function __construct(string $type)
{
parent::__construct('Persister for type "' . $type . '" not found.');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\PhpcrMigrationBundle\PhpcrMigration\Application\Exception;

class RoutePathNameNotFoundException extends \RuntimeException
{
public function __construct(string $uuid, string $locale)
{
parent::__construct(
\sprintf(
'RoutePathName not found for uuid "%s" and locale "%s". Before migrating to the ArticleBundle 3.0 you must update to ArticleBundle 2.6 and execute all phpcr migrations.',
$uuid,
$locale
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\PhpcrMigrationBundle\PhpcrMigration\Application\Exception;

class UnsupportedDocumentTypeException extends \RuntimeException
{
/**
* @param array<string> $types
*/
public function __construct(array $types)
{
parent::__construct(\sprintf('Unsupported document type(s) "%s"', \implode('", "', $types)));
}
}
119 changes: 119 additions & 0 deletions PhpcrMigration/Application/Parser/NodeParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\PhpcrMigrationBundle\PhpcrMigration\Application\Parser;

use Jackalope\Property;
use PHPCR\NodeInterface;
use PHPCR\PropertyInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

class NodeParser
{
public function __construct(
private readonly PropertyAccessorInterface $propertyAccessor,
) {
}

/**
* @return mixed[]
*/
public function parse(NodeInterface $node): array
{
$document = [
'localizations' => [
'null' => [], // required to always create the unlocalized dimension
],
];
foreach ($node->getProperties() as $property) {
$document = $this->parseProperty($property, $document);
}

return $document;
}

/**
* @param mixed[] $document
*
* @return mixed[]
*/
private function parseProperty(PropertyInterface $property, array $document): array
{
$name = $property->getName();
$value = $this->resolvePropertyValue($property);
$propertyPath = $this->getLocalizedPath($name);
$propertyPath = $this->getPropertyPath($propertyPath, $name);
$this->propertyAccessor->setValue(
$document,
$propertyPath,
$value
);

return $document;
}

private function isUnLocalizedProperty(string $name): bool
{
return !\str_contains($name, ':');
}

private function resolvePropertyValue(PropertyInterface $property): mixed
{
$value = $property instanceof Property ? $property->getValueForStorage() : $property->getValue();
if (\is_string($value) && \json_validate($value) && ('' !== $value && '0' !== $value)) {
return \json_decode($value, true);
}

return $value;
}

private function getLocalizedPath(string &$name): string
{
$propertyPath = '';
if (\str_starts_with($name, 'i18n:')) {
$localizationOffset = 5;
$firstDashPosition = \strpos($name, '-', $localizationOffset);
$locale = \substr($name, 5, $firstDashPosition - $localizationOffset);
$propertyPath .= '[localizations][' . $locale . ']';
$name = \substr($name, $firstDashPosition + 1);
} elseif ($this->isUnLocalizedProperty($name)) {
$propertyPath .= '[localizations][null]';
}

return $propertyPath;
}

private function getPropertyPath(string $propertyPath, string $name): string
{
if (\str_starts_with($name, 'jcr:')) {
$name = \substr($name, 4);
$propertyPath .= '[jcr][' . $name . ']';
} elseif (\str_starts_with($name, 'sulu:')) {
$name = \substr($name, 5);
$propertyPath .= '[sulu][' . $name . ']';
} elseif (\str_starts_with($name, 'seo-')) {
$name = \substr($name, 4);
$propertyPath .= '[seo][' . $name . ']';
} elseif (\str_starts_with($name, 'excerpt-')) {
$name = \substr($name, 8);
$propertyPath .= '[excerpt][' . $name . ']';
} elseif (\preg_match('/^(.+)#(\d+)$/', $name, $matches)) {
$name = $matches[1];
$index = (int) $matches[2];
[$blocksKey, $type] = \explode('-', $name);
$propertyPath .= '[' . $blocksKey . '][' . $index . '][' . $type . ']';
} else {
$propertyPath .= '[' . $name . ']';
}

return $propertyPath;
}
}
Loading
Loading