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/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/Entry.php b/src/Resource/Entry.php index e2520358..ae976e20 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,73 @@ 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') ?? []; + + // Prevent attempting to set a duplicate tag + + foreach ($tags as $tag) { + if ($tag['sys']['id'] === $tagId) { + return; + } + } + + $tags[] = [ + 'sys' => [ + 'type' => 'Link', + 'linkType' => 'Tag', + 'id' => $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. 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); } /**