From a622e7ed912e10ddd0150826dd745b24ba7cd379 Mon Sep 17 00:00:00 2001 From: Brendan Bullen <66941+bullenb@users.noreply.github.com> Date: Thu, 23 Jun 2022 17:55:36 +0100 Subject: [PATCH 1/3] Add metadata property to resource entries Signed-off-by: Brendan Bullen <66941+bullenb@users.noreply.github.com> --- .gitignore | 1 + src/Resource/Entry.php | 61 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9ebe3aee..a8900649 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ clover.xml /.phpunit.result.cache /.idea /dev-scripts +/*.code-workspace diff --git a/src/Resource/Entry.php b/src/Resource/Entry.php index e2520358..40c9f802 100644 --- a/src/Resource/Entry.php +++ b/src/Resource/Entry.php @@ -51,6 +51,11 @@ class Entry extends BaseResource implements EntryInterface, CreatableInterface */ protected $fields = []; + /** + * @var array[] + */ + protected $metadata = []; + /** * Entry constructor. */ @@ -79,10 +84,16 @@ public function jsonSerialize(): array } } - return [ + $entry = [ 'sys' => $this->sys, 'fields' => (object) $fields, ]; + + if ($this->metadata) { + $entry['metadata'] = (object) $this->metadata; + } + + return $entry; } /** @@ -214,6 +225,54 @@ public function setField(string $name, string $locale, $value) return $this; } + /** + * @param string $name + * @return array|null + */ + public function getMetadataValue(string $name) + { + return $this->metadata[$name] ?? null; + } + + /** + * @return array + */ + public function getMetadata(): array + { + return $this->metadata; + } + + /** + * @param string $name + * @param mixed $value + * @return $this + */ + public function setMetadataValue(string $name, $value) + { + $this->metadata[$name] = $value; + + return $this; + } + + /** + * @param string $tagId + * @return $this + */ + public function addTag(string $tagId) + { + $tags = $this->getMetadataValue('tags') ?? []; + + $tags[] = [ + 'sys' => [ + 'type' => 'Link', + 'linkType' => 'Tag', + 'id' => $tagId, + ], + ]; + + return $this->setMetadataValue('tags', $tags); + } + /** * Provides simple setX/getX capabilities, * without recurring to code generation. From b0816f1aa1227038a4810c9b5cfa10f8eb2c81b1 Mon Sep 17 00:00:00 2001 From: Brendan Bullen <66941+bullenb@users.noreply.github.com> Date: Tue, 2 Aug 2022 13:13:14 +0100 Subject: [PATCH 2/3] Detect duplicate tags and allow removal of tags Signed-off-by: Brendan Bullen <66941+bullenb@users.noreply.github.com> --- src/Resource/Entry.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Resource/Entry.php b/src/Resource/Entry.php index 40c9f802..ae976e20 100644 --- a/src/Resource/Entry.php +++ b/src/Resource/Entry.php @@ -262,6 +262,14 @@ public function addTag(string $tagId) { $tags = $this->getMetadataValue('tags') ?? []; + // Prevent attempting to set a duplicate tag + + foreach ($tags as $tag) { + if ($tag['sys']['id'] === $tagId) { + return; + } + } + $tags[] = [ 'sys' => [ 'type' => 'Link', @@ -273,6 +281,17 @@ public function addTag(string $tagId) return $this->setMetadataValue('tags', $tags); } + /** + * @param string $tagId + * @return $this + */ + public function removeTag(string $tagId) + { + return $this->setMetadataValue('tags', array_values(array_filter($this->getMetadataValue('tags') ?? [], function (array $tag) use ($tagId) { + return $tag['sys']['id'] !== $tagId; + }))); + } + /** * Provides simple setX/getX capabilities, * without recurring to code generation. From 69ae2bba196e41077d0515e77b7e9d0ce75edd29 Mon Sep 17 00:00:00 2001 From: Brendan Bullen <66941+bullenb@users.noreply.github.com> Date: Mon, 5 Sep 2022 16:02:03 +0100 Subject: [PATCH 3/3] Swap deprecated guzzle method and handle UTF-8 encoding with substitution Signed-off-by: Brendan Bullen <66941+bullenb@users.noreply.github.com> --- src/Resource/BaseResource.php | 3 +-- src/Resource/DeliveryApiKey.php | 3 +-- src/Resource/Environment.php | 3 +-- src/Resource/Locale.php | 3 +-- src/Resource/PersonalAccessToken.php | 3 +-- src/Resource/Space.php | 3 +-- 6 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/Resource/BaseResource.php b/src/Resource/BaseResource.php index 800b00d1..eb59ea01 100644 --- a/src/Resource/BaseResource.php +++ b/src/Resource/BaseResource.php @@ -14,7 +14,6 @@ use Contentful\Core\Api\Link; use Contentful\Core\Resource\SystemPropertiesInterface; use Contentful\Management\Client; -use function GuzzleHttp\json_encode as guzzle_json_encode; /** * BaseResource class. @@ -64,7 +63,7 @@ public function asRequestBody() unset($body['sys']); - return guzzle_json_encode((object) $body, \JSON_UNESCAPED_UNICODE); + return \GuzzleHttp\Utils::jsonEncode((object) $body, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); } /** diff --git a/src/Resource/DeliveryApiKey.php b/src/Resource/DeliveryApiKey.php index 40085a24..42f0808d 100644 --- a/src/Resource/DeliveryApiKey.php +++ b/src/Resource/DeliveryApiKey.php @@ -15,7 +15,6 @@ use Contentful\Management\Resource\Behavior\CreatableInterface; use Contentful\Management\Resource\Behavior\DeletableTrait; use Contentful\Management\Resource\Behavior\UpdatableTrait; -use function GuzzleHttp\json_encode as guzzle_json_encode; /** * DeliveryApiKey class. @@ -68,7 +67,7 @@ public function asRequestBody(): string unset($body['environments']); } - return guzzle_json_encode((object) $body, \JSON_UNESCAPED_UNICODE); + return \GuzzleHttp\Utils::jsonEncode((object) $body, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); } /** diff --git a/src/Resource/Environment.php b/src/Resource/Environment.php index 081acd4b..b382cb2b 100644 --- a/src/Resource/Environment.php +++ b/src/Resource/Environment.php @@ -16,7 +16,6 @@ use Contentful\Management\Resource\Behavior\DeletableTrait; use Contentful\Management\Resource\Behavior\UpdatableTrait; use Contentful\Management\SystemProperties\Environment as SystemProperties; -use function GuzzleHttp\json_encode as guzzle_json_encode; /** * Environment class. @@ -85,7 +84,7 @@ public function asRequestBody(): string unset($body['sys']); - return guzzle_json_encode((object) $body, \JSON_UNESCAPED_UNICODE); + return \GuzzleHttp\Utils::jsonEncode((object) $body, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); } /** diff --git a/src/Resource/Locale.php b/src/Resource/Locale.php index bb242130..73214e8c 100644 --- a/src/Resource/Locale.php +++ b/src/Resource/Locale.php @@ -15,7 +15,6 @@ use Contentful\Management\Resource\Behavior\DeletableTrait; use Contentful\Management\Resource\Behavior\UpdatableTrait; use Contentful\Management\SystemProperties\Locale as SystemProperties; -use function GuzzleHttp\json_encode as guzzle_json_encode; /** * Locale class. @@ -116,7 +115,7 @@ public function asRequestBody(): string // The property 'default' has to be omitted for the API to work. unset($body['default']); - return guzzle_json_encode((object) $body, \JSON_UNESCAPED_UNICODE); + return \GuzzleHttp\Utils::jsonEncode((object) $body, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); } /** diff --git a/src/Resource/PersonalAccessToken.php b/src/Resource/PersonalAccessToken.php index 7232619e..1a545e88 100644 --- a/src/Resource/PersonalAccessToken.php +++ b/src/Resource/PersonalAccessToken.php @@ -14,7 +14,6 @@ use Contentful\Core\Api\DateTimeImmutable; use Contentful\Management\Resource\Behavior\CreatableInterface; use Contentful\Management\SystemProperties\PersonalAccessToken as SystemProperties; -use function GuzzleHttp\json_encode as guzzle_json_encode; /** * PersonalAccessToken class. @@ -94,7 +93,7 @@ public function asRequestBody(): string unset($body['token']); unset($body['revokedAt']); - return guzzle_json_encode((object) $body, \JSON_UNESCAPED_UNICODE); + return \GuzzleHttp\Utils::jsonEncode((object) $body, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); } /** diff --git a/src/Resource/Space.php b/src/Resource/Space.php index 9d06a79e..fbecc816 100644 --- a/src/Resource/Space.php +++ b/src/Resource/Space.php @@ -16,7 +16,6 @@ use Contentful\Management\Resource\Behavior\DeletableTrait; use Contentful\Management\Resource\Behavior\UpdatableTrait; use Contentful\Management\SystemProperties\Space as SystemProperties; -use function GuzzleHttp\json_encode as guzzle_json_encode; /** * Space class. @@ -94,7 +93,7 @@ public function asRequestBody(): string $body['defaultLocale'] = $this->defaultLocale; } - return guzzle_json_encode((object) $body, \JSON_UNESCAPED_UNICODE); + return \GuzzleHttp\Utils::jsonEncode((object) $body, JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); } /**