From a047729d48366377d966de4419eae70636df1382 Mon Sep 17 00:00:00 2001 From: Constantine Nathanson Date: Mon, 15 Apr 2024 17:33:40 +0300 Subject: [PATCH] Add support for `restrictions` parameter in `MetadataField` --- src/Api/Metadata/Metadata.php | 3 ++- src/Api/Metadata/MetadataField.php | 27 ++++++++++++++++++- .../Integration/Admin/MetadataFieldsTest.php | 4 ++- tests/Unit/Admin/MetadataFieldsTest.php | 8 +++--- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Api/Metadata/Metadata.php b/src/Api/Metadata/Metadata.php index 6cc977e9..a2b233f6 100644 --- a/src/Api/Metadata/Metadata.php +++ b/src/Api/Metadata/Metadata.php @@ -10,6 +10,7 @@ namespace Cloudinary\Api\Metadata; +use Cloudinary\ArrayUtils; use Cloudinary\StringUtils; use JsonSerializable; @@ -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')) { diff --git a/src/Api/Metadata/MetadataField.php b/src/Api/Metadata/MetadataField.php index 81393c8d..9dac0113 100644 --- a/src/Api/Metadata/MetadataField.php +++ b/src/Api/Metadata/MetadataField.php @@ -49,6 +49,11 @@ abstract class MetadataField extends Metadata */ protected $validation; + /** + * @var array + */ + protected $restrictions; + /** * The MetadataField constructor. * @@ -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']; } /** @@ -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; + } } diff --git a/tests/Integration/Admin/MetadataFieldsTest.php b/tests/Integration/Admin/MetadataFieldsTest.php index a9ff1fec..2f3cc094 100644 --- a/tests/Integration/Admin/MetadataFieldsTest.php +++ b/tests/Integration/Admin/MetadataFieldsTest.php @@ -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); @@ -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], ] ); } diff --git a/tests/Unit/Admin/MetadataFieldsTest.php b/tests/Unit/Admin/MetadataFieldsTest.php index f32fb98a..1a160f3e 100644 --- a/tests/Unit/Admin/MetadataFieldsTest.php +++ b/tests/Unit/Admin/MetadataFieldsTest.php @@ -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(); @@ -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], ] ); }