Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for restrictions parameter in MetadataField #401

Merged
merged 1 commit into from
Apr 15, 2024
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
3 changes: 2 additions & 1 deletion src/Api/Metadata/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Cloudinary\Api\Metadata;

use Cloudinary\ArrayUtils;
use Cloudinary\StringUtils;
use JsonSerializable;

Expand Down Expand Up @@ -48,7 +49,7 @@ public function jsonSerialize()
if (method_exists($value, 'jsonSerialize')) {
$snakeCaseProperties[StringUtils::camelCaseToSnakeCase($key)] = $value->jsonSerialize();
}
} elseif (is_array($value)) {
} elseif (is_array($value) && !ArrayUtils::isAssoc($value)) {
$subArray = [];
foreach ($value as $subArrayValue) {
if (method_exists($subArrayValue, 'jsonSerialize')) {
Expand Down
27 changes: 26 additions & 1 deletion src/Api/Metadata/MetadataField.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ abstract class MetadataField extends Metadata
*/
protected $validation;

/**
* @var array
*/
protected $restrictions;

/**
* The MetadataField constructor.
*
Expand All @@ -66,7 +71,7 @@ public function __construct($label)
*/
public function getPropertyKeys()
{
return ['externalId', 'label', 'mandatory', 'defaultValue', 'type', 'validation'];
return ['externalId', 'label', 'mandatory', 'defaultValue', 'type', 'validation', 'restrictions'];
}

/**
Expand Down Expand Up @@ -178,4 +183,24 @@ public function setValidation(MetadataValidation $validation)
{
$this->validation = $validation;
}

/**
* Gets the restrictions of this field.
*
* @return array
*/
public function getRestrictions()
{
return $this->restrictions;
}

/**
* Sets the restrictions of this field.
*
* @param array $restrictions
*/
public function setRestrictions($restrictions)
{
$this->restrictions = $restrictions;
}
}
4 changes: 3 additions & 1 deletion tests/Integration/Admin/MetadataFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ public function testCreateDateMetadataField()
{
$dateMetadataField = new DateMetadataField(self::$EXTERNAL_ID_DATE);
$dateMetadataField->setExternalId(self::$EXTERNAL_ID_DATE);
$dateMetadataField->setRestrictions(["readonly_ui" => true]);

$result = self::$adminApi->addMetadataField($dateMetadataField);

Expand All @@ -229,7 +230,8 @@ public function testCreateDateMetadataField()
[
'label' => self::$EXTERNAL_ID_DATE,
'external_id' => self::$EXTERNAL_ID_DATE,
'mandatory' => false
'mandatory' => false,
'restrictions' => ["readonly_ui" => true],
]
);
}
Expand Down
8 changes: 5 additions & 3 deletions tests/Unit/Admin/MetadataFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function testCreateStringMetadataField()

$stringMetadataField = new StringMetadataField(self::EXTERNAL_ID_STRING);
$stringMetadataField->setExternalId(self::EXTERNAL_ID_STRING);
$stringMetadataField->setRestrictions(["readonly_ui" => true]);

$mockAdminApi->addMetadataField($stringMetadataField);
$lastRequest = $mockAdminApi->getMockHandler()->getLastRequest();
Expand All @@ -74,9 +75,10 @@ public function testCreateStringMetadataField()
self::assertRequestFields(
$lastRequest,
[
'type' => MetadataFieldType::STRING,
'external_id' => self::EXTERNAL_ID_STRING,
'label' => self::EXTERNAL_ID_STRING
'type' => MetadataFieldType::STRING,
'external_id' => self::EXTERNAL_ID_STRING,
'label' => self::EXTERNAL_ID_STRING,
'restrictions' => ["readonly_ui" => true],
]
);
}
Expand Down