Giter VIP home page Giter VIP logo

api-bridge-converter's Introduction

Api Bridge Converter

Install:

composer require bigoen/api-bridge-converter

Create your converter.

<?php

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;
    }
}

Convert api values in tree.

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);

Use in custom object.

<?php

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(),
        ]);
    }
}

Important: property and deep names details > https://symfony.com/doc/current/components/property_access.html

api-bridge-converter's People

Contributors

safaksaylam avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.