Skip to content
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
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"ext-json": "*",
"ext-mbstring": "*",
"ext-pdo": "*",
"api-platform/core": "v2.4.0-beta.1",
"api-platform/core": "^2.4",
"bolt/common": "^2.0.2",
"cocur/slugify": "^3.1",
"doctrine/annotations": "^1.0",
Expand Down Expand Up @@ -129,8 +129,7 @@
]
},
"conflict": {
"symfony/symfony": "*",
"api-platform/core": "v2.4.0-beta.2"
"symfony/symfony": "*"
},
"extra": {
"symfony": {
Expand Down
2 changes: 1 addition & 1 deletion src/Content/MediaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private function updateImageDimensions(Media $media, SplFileInfo $file): void
return;
}

$size = getimagesize($file->getRealpath());
$size = @getimagesize($file->getRealpath());

if ($size !== false) {
$media->setWidth($size[0])
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* })
* @ORM\HasLifecycleCallbacks
*/
class Content implements \JsonSerializable
class Content
{
use ContentLocalizeTrait;
use ContentExtrasTrait;
Expand Down
31 changes: 0 additions & 31 deletions src/Entity/ContentExtrasTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,4 @@ public function getExtras(): array
'editLink' => $this->contentExtension->getEditLink($content),
];
}

public function jsonSerialize(): array
{
/** @var Content $content */
$content = $this;

if ($this->getDefinition() === null) {
return [];
}

return [
'id' => $content->getId(),
'contentType' => $content->getContentType(),
'slug' => $content->getSlug(),
'author' => [
'id' => $content->getAuthor()->getId(),
'displayName' => $content->getAuthor()->getDisplayName(),
'username' => $content->getAuthor()->getUsername(),
'email' => $content->getAuthor()->getEmail(),
],
'fields' => $content->getFieldValues(),
'taxonomies' => $content->getTaxonomyValues(),
'extras' => $this->getExtras(),
'status' => $content->getStatus(),
'icon' => $content->getIcon(),
'createdAt' => $content->getCreatedAt(),
'modifiedAt' => $content->getModifiedAt(),
'publishedAt' => $content->getPublishedAt(),
'depublishedAt' => $content->getDepublishedAt(),
];
}
}
2 changes: 1 addition & 1 deletion src/Repository/ContentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function findOneBySlug(string $slug): ?Content
'field.id = slug.id'
)
->andWhere('slug.value = :slug')
->setParameter('slug', json_encode([$slug]))
->setParameter('slug', \GuzzleHttp\json_encode([$slug]))
->getQuery()
->getOneOrNullResult();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/FieldRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private function getQueryBuilder(?QueryBuilder $qb = null)
return $qb ?: $this->createQueryBuilder('field');
}

public function findOneBySlug($slug): ?Field
public function findOneBySlug(string $slug): ?Field
{
return $this->getQueryBuilder()
->andWhere('field.value = :slug')
Expand Down
64 changes: 64 additions & 0 deletions src/Twig/JsonExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Bolt\Twig;

use Bolt\Entity\Content;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class JsonExtension extends AbstractExtension
{
private const SERIALIZE_GROUP = 'get_content';

/**
* @var NormalizerInterface
*/
private $normalizer;

public function __construct(NormalizerInterface $normalizer)
{
$this->normalizer = $normalizer;
}

/**
* {@inheritdoc}
*/
public function getFilters()
{
return [
new TwigFilter('normalize_records', [$this, 'normalizeRecords']),
new TwigFilter('json_records', [$this, 'jsonRecords']),
];
}

public function jsonRecords($records): string
{
return \GuzzleHttp\json_encode($this->normalizeRecords($records));
}

public function normalizeRecords($records): array
{
if ($records instanceof Content) {
return $this->contentToArray($records);
}

if (is_array($records)) {
$normalizedRecords = $records;
} elseif (is_iterable($records)) {
$normalizedRecords = iterator_to_array($records);
} else {
throw new \InvalidArgumentException();
}

return array_map([$this, 'contentToArray'], $normalizedRecords);
}

private function contentToArray(Content $content): array
{
// we do it that way because in current API Platform version a Resource can't implement \JsonSerializable
return $this->normalizer->normalize($content, null, ['group' => [self::SERIALIZE_GROUP]]);
}
}
2 changes: 1 addition & 1 deletion templates/_partials/_content_listing.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<!-- listing records -->
<listing-records
type="{{ type }}"
:data="{{ records|json_encode }}"
:data="{{ records|json_records }}"
>
</listing-records>
<!-- end listing records -->
Expand Down