Skip to content
This repository has been archived by the owner on Apr 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #25 from 21TORR/slice-extra-data
Browse files Browse the repository at this point in the history
Add slice extra data generator
  • Loading branch information
apfelbox authored Dec 19, 2021
2 parents dc9e018 + 99f119a commit 52a9630
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
4.0.0-beta.9
============

* (feature) Added `SliceExtraDataGenerator` to be able to append extra data to slices.


4.0.0-beta.8
============

Expand Down
3 changes: 3 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ services:
Torr\PrismicApi\Api\PrismicIntegrationFieldApi:
$repository: '%env(PRISMIC_REPOSITORY)%'
$tokens: '%env(json:PRISMIC_INTEGRATION_TOKENS)%'

Torr\PrismicApi\Transform\FieldValueTransformer:
$sliceExtraDataGenerators: !tagged_iterator 'prismic.slice.extra-data-generator'
4 changes: 4 additions & 0 deletions src/PrismicApiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Torr\BundleHelpers\Bundle\BundleExtension;
use Torr\PrismicApi\Definition\DocumentDefinition;
use Torr\PrismicApi\RichText\Link\LinkGeneratorHandler;
use Torr\PrismicApi\Transform\Slice\SliceExtraDataGeneratorInterface;

final class PrismicApiBundle extends Bundle
{
Expand All @@ -26,6 +27,9 @@ public function build (ContainerBuilder $container) : void

$container->registerForAutoconfiguration(DocumentDefinition::class)
->addTag("prismic.document.definition");

$container->registerForAutoconfiguration(SliceExtraDataGeneratorInterface::class)
->addTag("prismic.slice.extra-data-generator");
}


Expand Down
12 changes: 9 additions & 3 deletions src/Structure/Slice/Slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public function getValidationConstraints () : array
public function transformValue (mixed $data, FieldValueTransformer $valueTransformer) : mixed
{
\assert(\is_array($data));
$resultItems = [];
$resultData = [];
$result = [
"data" => [],
"items" => [],
Expand All @@ -130,7 +132,7 @@ public function transformValue (mixed $data, FieldValueTransformer $valueTransfo

foreach ($this->fields as $key => $field)
{
$result["data"][$key] = $this->transformSingleValue(
$resultData[$key] = $this->transformSingleValue(
$valueTransformer,
$this->fields,
$key,
Expand All @@ -152,10 +154,14 @@ public function transformValue (mixed $data, FieldValueTransformer $valueTransfo
);
}

$result["items"][] = $transformedItem;
$resultItems[] = $transformedItem;
}

return $result;
return [
"data" => $resultData,
"items" => $resultItems,
"extra" => $valueTransformer->generateExtraDataForSlice($this),
];
}

/**
Expand Down
23 changes: 22 additions & 1 deletion src/Transform/FieldValueTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
namespace Torr\PrismicApi\Transform;

use Torr\PrismicApi\Structure\Field\InputField;
use Torr\PrismicApi\Structure\Slice\Slice;
use Torr\PrismicApi\Structure\Slice\SliceZone;
use Torr\PrismicApi\Transform\Slice\SliceExtraDataGeneratorInterface;

final class FieldValueTransformer
{
public function __construct ()
public function __construct (
/** @var iterable<SliceExtraDataGeneratorInterface> */
private iterable $sliceExtraDataGenerators,
)
{

}
Expand All @@ -29,4 +34,20 @@ public function transformRichText (array $data) : array
{
return $data;
}

/**
* Generates the extra data for the given slice
*/
public function generateExtraDataForSlice (Slice $slice) : array
{
$result = [];

/** @var SliceExtraDataGeneratorInterface $dataGenerator */
foreach ($this->sliceExtraDataGenerators as $dataGenerator)
{
$result = $dataGenerator->appendExtraData($slice, $result);
}

return $result;
}
}
16 changes: 16 additions & 0 deletions src/Transform/Slice/SliceExtraDataGeneratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace Torr\PrismicApi\Transform\Slice;

use Torr\PrismicApi\Structure\Slice\Slice;

interface SliceExtraDataGeneratorInterface
{
/**
* @param array $extraData The pre-generated extra data, that can get appended to
*/
public function appendExtraData (
Slice $slice,
array $extraData,
) : array;
}

0 comments on commit 52a9630

Please sign in to comment.