Giter VIP home page Giter VIP logo

apijsonparamconvertercomponent's Introduction

API Json ParamConverter for Symfony4

Build Status FOSSA Status Scrutinizer Code Quality Code Coverage Code Intelligence Status FOSSA Status

requirements

You need PHP (7.x), composer, and Symfony 4

To get code coverage, don't forget that you need xDebug when you run PHPUnit or you will get this message: Error: No code coverage driver is available If xDebug is not in the php configuration registered as your default php, you can run it manually:

PathToYouPHPWithXDebug vendor\phpunit\phpunit\phpunit

explanation

Working with ApiPlatform, i wanted to use custom POST route where i could send complex json data which represents nested entities. To realize this i choose to use the ParamConverters. So with little convention (json props must be the same as php entity props) and few ParamConverters (one per entity) extending the Rebolon/Request/ItemAbstractConverter (for one entity) or ListAbstractConverter (for collection of entities), it works !

Here are some samples of json sent to the custom routes:

// The most complete sample, with de-duplication of editor (only once will be created)
{
    "book": {
        "title": "Zombies in western culture",
        "editors": [{
            "publication_date": "1519664915", 
            "collection": "printed version", 
            "isbn": "9781783743230", 
            "editor": {
                "name": "Open Book Publishers"
            }
        }, {
            "publication_date": "1519747464", 
            "collection": "ebooks", 
            "isbn": "9791036500824", 
            "editor": {
                "name": "Open Book Publishers"
            }
        }],
        "authors": [{
            "role": {
                "translation_key": "WRITER"
            }, 
            "author": {
                "firstname": "Marc", 
                "lastname": "O'Brien"
            }
        }, {
            "role": {
                "translation_key": "WRITER"
            }, 
            "author": {
                "firstname": "Paul", 
                "lastname": "Kyprianou"
            }
        }],
        "serie": {
            "name": "Open Reports Series"
        }
    }
}

// This one re-use database information for editor / author / job / serie
{
    "book": {
        "title": "Oh my god, how simple it is !",
        "editors": [{
            "publication_date": "1519664915", 
            "collection": "from my head", 
            "isbn": "9781783742530", 
            "editor": 1
        }, {
            "publication_date": "1519747464", 
            "collection": "ebooks", 
            "isbn": "9782821883963", 
            "editor": {
                "name": "Open Book Publishers"
            }
        }],
        "authors": [{
            "role": 2, 
            "author": 3
        }, {
            "role": {
                "translation_key": "WRITER"
            }, 
            "author": {
                "firstname": "Paul", 
                "lastname": "Kyprianou"
            }
        }],
        "serie": {
            "id": 4,
            "name": "whatever because the paramConverte will only take care of the id property"
    }
}

The AbstractConverter is able to deduplicate entity (if there is more than one the same entity in the json). It's also able to retreive information from database if:

  • you put ID instead of object inside the json (2nd sample above with editors[0].editor or authors[0].role)
  • you send an object that contains the id field (or any other id prop name that you define in the specific Converter)

You can have a look at the tests to get more informations about how to use this component. I have wrote those test with sample Entities and Converter to make it more understandable.

configuration

Add all your ParamConverters in the config/services.yaml file, like this:

services:
    ...
    
    App\Request\ParamConverter\Library\BookConverter:
        public: true
        arguments:
            - '@validator'
        tags:
            - { name: request.param_converter, priority: -2, converter: book }

License

FOSSA Status

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.