Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ clover.xml
/.phpunit.result.cache
/.idea
/dev-scripts
/*.code-workspace
3 changes: 1 addition & 2 deletions src/Resource/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Resource/DeliveryApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down
80 changes: 79 additions & 1 deletion src/Resource/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class Entry extends BaseResource implements EntryInterface, CreatableInterface
*/
protected $fields = [];

/**
* @var array[]
*/
protected $metadata = [];

/**
* Entry constructor.
*/
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions src/Resource/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Resource/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Resource/PersonalAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Resource/Space.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}

/**
Expand Down