PHP code example of bigoen / api-bridge-converter

1. Go to this page and download the library: Download bigoen/api-bridge-converter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bigoen / api-bridge-converter example snippets




declare(strict_types=1);

namespace Bigoen\ApiBridgeConverter\Model;

/**
 * @author Şafak Saylam <[email protected]>
 */
class ConvertUnsetsProperty implements ConvertPropertyInterface
{
    public array $properties = [];

    public static function new(array $properties = []): self
    {
        $object = new self();
        $object->properties = $properties;

        return $object;
    }

    public function convert(array $arr): array
    {
        foreach ($this->properties as $property) {
            unset($arr[$property]);
        }

        return $arr;
    }
}

use Bigoen\ApiBridge\Bridge\ApiPlatform\HttpClient\JsonldClient;
use Bigoen\ApiBridgeConverter\Model\ConvertDateTimeProperty;
use Bigoen\ApiBridgeConverter\Model\ConvertTreeProperty;
use Bigoen\ApiBridgeConverter\Model\ConvertUnsetsProperty;
use Bigoen\ApiBridgeConverter\Model\ConvertProperty;

$categories = $this->entityManager
    ->getRepository(Category::class)
    ->findAllIndexById();
$tags = $this->entityManager
    ->getRepository(Tag::class)
    ->findAllIndexById();
// set converts.
$convertProperties = [
    new ConvertTreeProperty(
        '[category]',
        false,
        '[category][@id]',
        null,
        $this->getConvertValues('/api/categories/{id}', $categories)
    ),
    new ConvertTreeProperty(
        '[tags]',
        true,
        '[tags][][@id]',
        Tag::class,
        $this->getConvertValues('/api/tags/{id}', $tags),
        [
            new ConvertDateTimeProperty('[createdAt]'),
            new ConvertDateTimeProperty('[updatedAt]'),
        ]
    ),
    new ConvertDateTimeProperty('[createdAt]'),
    new ConvertDateTimeProperty('[updatedAt]'),
    // class property and api property.
    new ConvertProperty('[id]', '[_id]'),
];
// unsets for sending.
$sendingConvertProperties = [
    new ConvertUnsetsProperty(['id', 'name']),
];
/* @var $client JsonldClient */
$client->setConvertProperties($convertProperties)->setSendingConvertProperties($sendingConvertProperties);



declare(strict_types=1);

namespace Bigoen\Test\Model\Input;

use Bigoen\ApiBridgeConverter\Model\ConvertUnsetNullsProperty;
use Bigoen\ApiBridgeConverter\Model\Traits\ArrayObjectConverterTrait;
use Bigoen\Test\Contracts\InputInterface;

/**
 * @author Şafak Saylam <[email protected]>
 */
final class PaymentCardInput implements InputInterface
{
    use ArrayObjectConverterTrait;

    public function __construct(
        public string $cardHolderName,
        public string $cardNumber,
        public string $expireYear,
        public string $expireMonth,
        public string $cvc,
        public ?string $cardAlias = null,
        public ?bool $storeCardAfterSuccessPayment = null,
        public ?string $cardUserKey = null
    ) {
    }

    public function toArray(): array
    {
        return self::objectToArray($this, [
            new ConvertUnsetNullsProperty(),
        ]);
    }
}