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 #19 from 21TORR/next
Browse files Browse the repository at this point in the history
Added more integrations of fields + add more structured transformed data
  • Loading branch information
apfelbox authored Nov 25, 2021
2 parents 7e10096 + b783dd7 commit 586d985
Show file tree
Hide file tree
Showing 10 changed files with 300 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
4.0.0-beta.3
============

* (bug) Add empty extra field for slices.
* (feature) Add `ImageValue` and `VideoValue`.
* (feature) Implement `EmbedField` (it only supports videos for now).
* (bug) Fix invalid initialization of `SliceValidationCompound`.
* (feature) Add `DocumentLinkValue` and use it in `LinkField`.


4.0.0-beta.2
============

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"extra": {
"branch-alias": {
"dev-next": "0.2.x-dev"
"dev-next": "4.x-dev"
}
},
"autoload": {
Expand Down
29 changes: 29 additions & 0 deletions src/Data/Value/DocumentLinkValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace Torr\PrismicApi\Data\Value;

final class DocumentLinkValue
{
/**
*/
public function __construct (
private string $id,
private ?string $type,
)
{
}

/**
*/
public function getId () : string
{
return $this->id;
}

/**
*/
public function getType () : ?string
{
return $this->type;
}
}
56 changes: 56 additions & 0 deletions src/Data/Value/ImageValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types=1);

namespace Torr\PrismicApi\Data\Value;

/**
*
*/
final class ImageValue
{
/**
*/
public function __construct (
private string $url,
private int $width,
private int $height,
private ?string $alt = null,
private ?string $copyright = null,
)
{
}

/**
*/
public function getUrl () : string
{
return $this->url;
}

/**
*/
public function getWidth () : int
{
return $this->width;
}

/**
*/
public function getHeight () : int
{
return $this->height;
}

/**
*/
public function getAlt () : ?string
{
return $this->alt;
}

/**
*/
public function getCopyright () : ?string
{
return $this->copyright;
}
}
71 changes: 71 additions & 0 deletions src/Data/Value/VideoValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types=1);

namespace Torr\PrismicApi\Data\Value;

final class VideoValue
{
public const PROVIDER_YOUTUBE = "youtube";
public const PROVIDER_VIMEO = "vimeo";

public const PROVIDER_MAPPING = [
"Vimeo" => self::PROVIDER_VIMEO,
"YouTube" => self::PROVIDER_YOUTUBE,
];

/**
*/
public function __construct (
private string $provider,
private string $url,
private string $title,
private int $width,
private int $height,
private ImageValue $thumbnail,
)
{

}

/**
*/
public function getProvider () : string
{
return $this->provider;
}

/**
*/
public function getUrl () : string
{
return $this->url;
}

/**
*/
public function getTitle () : string
{
return $this->title;
}

/**
*/
public function getWidth () : int
{
return $this->width;
}

/**
*/
public function getHeight () : int
{
return $this->height;
}


/**
*/
public function getThumbnail () : ImageValue
{
return $this->thumbnail;
}
}
77 changes: 75 additions & 2 deletions src/Structure/Field/EmbedField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace Torr\PrismicApi\Structure\Field;

use Symfony\Component\Validator\Constraints as Assert;
use Torr\PrismicApi\Data\Value\ImageValue;
use Torr\PrismicApi\Data\Value\VideoValue;
use Torr\PrismicApi\Structure\Helper\FilterFieldsHelper;
use Torr\PrismicApi\Transform\FieldValueTransformer;

/**
* @see https://prismic.io/docs/core-concepts/embed
Expand Down Expand Up @@ -31,7 +35,76 @@ public function __construct (
*/
public function getValidationConstraints () : array
{
// @todo add validation
return [];
return [
new Assert\NotNull(),
new Assert\Type("array"),
new Assert\Collection([
"fields" => [
"provider_name" => [
new Assert\NotNull(),
new Assert\Type("string"),
new Assert\Choice(\array_keys(VideoValue::PROVIDER_MAPPING)),
],
"type" => [
new Assert\NotNull(),
// currently, only videos are supported
new Assert\IdenticalTo("video"),
],
"title" => [
new Assert\NotNull(),
new Assert\Type("string"),
],
"width" => [
new Assert\NotNull(),
new Assert\Type("int"),
new Assert\Range(min: 1),
],
"height" => [
new Assert\NotNull(),
new Assert\Type("int"),
new Assert\Range(min: 1),
],
"thumbnail_url" => [
new Assert\NotNull(),
new Assert\Type("string"),
],
"thumbnail_width" => [
new Assert\NotNull(),
new Assert\Type("int"),
new Assert\Range(min: 1),
],
"thumbnail_height" => [
new Assert\NotNull(),
new Assert\Type("int"),
new Assert\Range(min: 1),
],
"embed_url" => [
new Assert\NotNull(),
new Assert\Type("string"),
],
],
"allowMissingFields" => false,
"allowExtraFields" => true,
]),
];
}

/**
* @inheritDoc
*/
public function transformValue (mixed $data, FieldValueTransformer $valueTransformer) : mixed
{
return new VideoValue(
provider: VideoValue::PROVIDER_MAPPING[$data["provider_name"]],
url: $data["embed_url"],
title: $data["title"],
width: $data["width"],
height: $data["height"],
thumbnail: new ImageValue(
url: $data["thumbnail_url"],
width: $data["thumbnail_width"],
height: $data["thumbnail_height"],
),
);
}
}
50 changes: 47 additions & 3 deletions src/Structure/Field/LinkField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace Torr\PrismicApi\Structure\Field;

use Torr\PrismicApi\Data\Value\DocumentLinkValue;
use Torr\PrismicApi\Data\Value\ImageValue;
use Torr\PrismicApi\Exception\Structure\InvalidTypeDefinitionException;
use Torr\PrismicApi\Structure\Helper\FilterFieldsHelper;
use Torr\PrismicApi\Transform\FieldValueTransformer;

/**
* @see https://prismic.io/docs/core-concepts/link-content-relationship
Expand All @@ -29,14 +32,14 @@ final class LinkField extends InputField
*/
public function __construct (
string $label,
?string $select = self::SELECT_ALL,
private ?string $select = self::SELECT_ALL,
?string $placeholder = null,
?array $customTypes = null,
?array $tags = null,
)
{
$hasDocumentFilter = !empty($customTypes) || !empty($tags);
$selectsDocuments = self::SELECT_ALL === $select || self::SELECT_DOCUMENT === $select;
$selectsDocuments = self::SELECT_ALL === $this->select || self::SELECT_DOCUMENT === $this->select;

if ($hasDocumentFilter && !$selectsDocuments)
{
Expand All @@ -47,7 +50,7 @@ public function __construct (
parent::__construct(self::TYPE_KEY, FilterFieldsHelper::filterOptionalFields([
"label" => $label,
"placeholder" => $placeholder,
"select" => $select,
"select" => $this->select,
"customtypes" => $customTypes,
"tags" => $tags,
]));
Expand All @@ -61,4 +64,45 @@ public function getValidationConstraints () : array
// @todo add validation
return [];
}


/**
* @inheritDoc
*/
public function transformValue (mixed $data, FieldValueTransformer $valueTransformer) : mixed
{
$type = $data["link_type"] ?? null;

if ("Web" === $type)
{
return $data["url"] ?? null;
}

if ("Media" === $type)
{
// return as an image, if specifically an image was asked for
// @todo always return it this way (and use ImageValue or FileValue)
if (self::SELECT_MEDIA === $this->select)
{
return new ImageValue(
$data["url"],
(int) $data["width"],
(int) $data["height"],
$data["name"],
);
}

return $data["url"] ?? null;
}

if ("Document" === $type && \is_string($data["id"]))
{
return new DocumentLinkValue(
$data["id"],
$data["type"] ?? null,
);
}

return null;
}
}
9 changes: 7 additions & 2 deletions src/Structure/Field/RichTextField.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class RichTextField extends InputField
{
private const TYPE_KEY = "StructuredText";
private const PARAGRAPH = "paragraph";
public const HEADINGS = [
"heading1",
"heading2",
Expand All @@ -27,7 +28,7 @@ class RichTextField extends InputField
"hyperlink",
];
public const PARAGRAPH_STYLE = [
"paragraph",
self::PARAGRAPH,
"preformatted",
"list-item",
"o-list-item",
Expand Down Expand Up @@ -78,10 +79,14 @@ private function getAllowedStyles () : array
*/
private function getAllowedParagraphLevelStyles () : array
{
return \array_filter(
$styles = \array_filter(
$this->getAllowedStyles(),
static fn (string $style) => \in_array($style, self::HEADINGS, true) || \in_array($style, self::PARAGRAPH_STYLE, true),
);

return !empty($styles)
? $styles
: [self::PARAGRAPH];
}


Expand Down
1 change: 1 addition & 0 deletions src/Structure/Slice/Slice.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public function transformValue (mixed $data, FieldValueTransformer $valueTransfo
$result = [
"data" => [],
"items" => [],
"extra" => [],
];

foreach ($this->fields as $key => $field)
Expand Down
Loading

0 comments on commit 586d985

Please sign in to comment.