Skip to content

Commit

Permalink
Add support for restrictions parameter in MetadataField
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary committed Apr 15, 2024
1 parent e4b9582 commit a047729
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Api/Metadata/Metadata.php
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
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
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
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

0 comments on commit a047729

Please sign in to comment.