Skip to content

Commit

Permalink
Position dimensions
Browse files Browse the repository at this point in the history
- added integration of the new AMP API field `dimensions`
- fixed and added tests
  • Loading branch information
tg666 committed Apr 4, 2024
1 parent 92ccbfb commit 0d8b652
Show file tree
Hide file tree
Showing 52 changed files with 651 additions and 87 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]
### Added
- Added integration of the new AMP API field `mode`.
- Added integration of the new AMP API fields `mode` and `dimensions`.
- Added possibility to render embed banners.
- Added new rendering mode `embed` (`EmbedRenderingMode`) for the Latte bridge.

Expand Down
4 changes: 4 additions & 0 deletions src/Renderer/AmpBannerExternalAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function __toString(): string
'rotationSeconds' => $this->position->getRotationSeconds(),
'displayType' => $this->position->getDisplayType(),
'breakpointType' => $this->position->getBreakpointType(),
'dimensions' => [
'width' => $this->position->getDimensions()->getWidth(),
'height' => $this->position->getDimensions()->getHeight(),
],
],
'state' => [
'value' => $this->state,
Expand Down
7 changes: 6 additions & 1 deletion src/Renderer/Latte/Templates/contents.fragment.latte
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
src="{$content->getSrc()}"
sizes="{$content->getSizes()}"
alt="{$content->getAlt()}"
n:attr="title => '' !== $content->getTitle() ? $content->getTitle() : null, loading => $options['loading'] ?? null">
n:attr="
width => null !== $position->getDimensions()->getWidth() ? $position->getDimensions()->getWidth() : null,
height => null !== $position->getDimensions()->getHeight() ? $position->getDimensions()->getHeight() : null,
title => '' !== $content->getTitle() ? $content->getTitle() : null,
loading => $options['loading'] ?? null
">
</picture>
</a>
{elseif $content instanceof SixtyEightPublishers\AmpClient\Response\ValueObject\HtmlContent}
Expand Down
4 changes: 3 additions & 1 deletion src/Renderer/Phtml/Templates/contents.fragment.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use SixtyEightPublishers\AmpClient\Renderer\BreakpointStyle\BreakpointStyle;
/** @var ?Banner $banner */
/** @var array<string, scalar> $options */
?>
<?php foreach ($banner->getContents() as $index => $content) : ?>
<?php foreach ($banner->getContents() as $content) : ?>
<?php if ($content instanceof ImageContent) : ?>
<a data-amp-content-breakpoint="<?= Helpers::escapeHtmlAttr($content->getBreakpoint() ?? 'default') ?>"
class="amp-banner__content amp-banner__content--img"
Expand All @@ -30,6 +30,8 @@ use SixtyEightPublishers\AmpClient\Renderer\BreakpointStyle\BreakpointStyle;
src="<?= Helpers::escapeHtmlAttr($content->getSrc()) ?>"
sizes="<?= Helpers::escapeHtmlAttr($content->getSizes()) ?>"
alt="<?= Helpers::escapeHtmlAttr($content->getAlt()) ?>"
<?php if (null !== $position->getDimensions()->getWidth()) : ?>width="<?= Helpers::escapeHtmlAttr($position->getDimensions()->getWidth()) ?>"<?php endif ?>
<?php if (null !== $position->getDimensions()->getHeight()) : ?>height="<?= Helpers::escapeHtmlAttr($position->getDimensions()->getHeight()) ?>"<?php endif ?>
<?php if ('' !== $content->getTitle()) : ?>title="<?= Helpers::escapeHtmlAttr($content->getTitle()) ?>"<?php endif ?>
<?php if (isset($options['loading'])) : ?>loading="<?= Helpers::escapeHtmlAttr($options['loading']) ?>"<?php endif ?>>
</picture>
Expand Down
23 changes: 23 additions & 0 deletions src/Response/Hydrator/BannersResponseHydratorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SixtyEightPublishers\AmpClient\Response\BannersResponse;
use SixtyEightPublishers\AmpClient\Response\ValueObject\Banner;
use SixtyEightPublishers\AmpClient\Response\ValueObject\ContentInterface;
use SixtyEightPublishers\AmpClient\Response\ValueObject\Dimensions;
use SixtyEightPublishers\AmpClient\Response\ValueObject\HtmlContent;
use SixtyEightPublishers\AmpClient\Response\ValueObject\ImageContent;
use SixtyEightPublishers\AmpClient\Response\ValueObject\Position;
Expand Down Expand Up @@ -46,13 +47,19 @@
* contents: array<int, HtmlContentData|ImageContentData>,
* }
*
* @phpstan-type DimensionsData = array{
* width: int|null,
* height: int|null,
* }
*
* @phpstan-type PositionData = array{
* position_id?: string|null,
* position_name?: string|null,
* rotation_seconds: int,
* display_type: string|null,
* breakpoint_type: string,
* mode?: string,
* dimensions?: DimensionsData,
* banners: array<int, BannerData>,
* }
*
Expand Down Expand Up @@ -85,6 +92,7 @@ public function hydrate($responseBody): BannersResponse
$positionData['display_type'] ?? null,
$positionData['breakpoint_type'],
$positionData['mode'] ?? Position::ModeManaged,
$this->hydrateDimensions($positionData['dimensions'] ?? null),
$this->hydrateBanners($positionData['banners']),
);
}
Expand Down Expand Up @@ -161,4 +169,19 @@ private function hydrateContents(array $contentsData): array

return $contents;
}

/**
* @param DimensionsData|null $dimensions
*/
private function hydrateDimensions(?array $dimensions): Dimensions
{
if (null === $dimensions) {
return new Dimensions(null, null);
}

return new Dimensions(
$dimensions['width'] ?? null,
$dimensions['height'] ?? null,
);
}
}
30 changes: 30 additions & 0 deletions src/Response/ValueObject/Dimensions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\AmpClient\Response\ValueObject;

final class Dimensions
{
private ?int $width;

private ?int $height;

public function __construct(
?int $width,
?int $height
) {
$this->width = $width;
$this->height = $height;
}

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

public function getHeight(): ?int
{
return $this->height;
}
}
9 changes: 9 additions & 0 deletions src/Response/ValueObject/Position.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ final class Position

private string $mode;

private Dimensions $dimensions;

/** @var array<int, Banner> */
private array $banners;

Expand All @@ -44,6 +46,7 @@ public function __construct(
?string $displayType,
string $breakpointType,
string $mode,
Dimensions $dimensions,
array $banners
) {
$this->id = $id;
Expand All @@ -53,6 +56,7 @@ public function __construct(
$this->displayType = $displayType;
$this->breakpointType = $breakpointType;
$this->mode = $mode;
$this->dimensions = $dimensions;
$this->banners = $banners;
}

Expand Down Expand Up @@ -91,6 +95,11 @@ public function getMode(): string
return $this->mode;
}

public function getDimensions(): Dimensions
{
return $this->dimensions;
}

/**
* @return array<int, Banner>
*/
Expand Down
Loading

0 comments on commit 0d8b652

Please sign in to comment.