cloudinary / cloudinary_php

PHP extension for Cloudinary
https://cloudinary.com/documentation/php_integration
MIT License
388 stars 150 forks source link

Error using defaultvalues in class SetMetadataField #369

Closed jjdiaz closed 1 year ago

jjdiaz commented 1 year ago

When I try to send an array as default values with the method setDefaultValue I got an "Array to string conversion in..."

I fix it overriding this class as follow

<?php
/**
 * This file is part of the Cloudinary PHP package.
 *
 * (c) Cloudinary
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Cloudinary\Api\Metadata;

use App\Roca\ExternalConnectorBundle\Services\ArrayValue;

/**
 * Represents a metadata field with 'Set' (multi-selection list) type.
 *
 * @api
 *
 * JJD
 * Clase reescrita porque la original no admite que se le pase un array y da error al intentar convertir el array
 * en string.
 * Se agrega método setDefaultValue que TIENE que recibir un array encaptulado en la clase ArrayValue que
 * permite usar el método jsonSerialize. Si se manda un array no funciona.
 */
class SetMetadataField extends MetadataFieldList
{
    /**
     * The SetMetadataField constructor.
     *
     * @param string                   $label
     * @param array|MetadataDataSource $dataSource
     */
    public function __construct($label, $dataSource = [])
    {
        parent::__construct($label, $dataSource);
        $this->type = MetadataFieldType::SET;
    }

    /**
     * Sets the default value of this field.
     *
     * @param ArrayValue $defaultValue
     */
    public function setDefaultValue(ArrayValue $defaultValue)
    {
        $this->defaultValue = $defaultValue;
    }
}

In addition, in mandatory to use and add a new ArrayValue class as follow.

<?php

namespace App\Roca\ExternalConnectorBundle\Services;

/**
 * ClassName ArrayValue.php
 *
 * @author: Joaquin Jimenez <jjimenez@asmws.com> *
 * @copyright 2022 ASM WebServices Spain (http://asmws.com)
 *
 * Features included
 */
class ArrayValue implements \JsonSerializable {
    public function __construct(array $array) {
        $this->array = $array;
    }

    public function jsonSerialize() {
        return $this->array;
    }
}
const-cloudinary commented 1 year ago

Hello @jjdiaz, thank you for reporting the issue! It is fixed and merged. Will be released in the next version.