Giter VIP home page Giter VIP logo

architect's Introduction

Architect

Latest Version Software License Build Status Coverage Status Total Downloads

Introduction

Architect is used for dynamically creating new structures for API resource relationships. Sounds confusing and pretentious?

Imagine you have a resource Book with a related resource Author.

Book 1-----n Author

Normal embedded mode

This is how related resources are loaded by default using embedded mode.

{
   "books":[
      {
         "id":1,
         "author_id":1,
         "title":"How to save the world from evil",
         "pages":100,
         "author":{
            "id":1,
            "name":"Optimus Prime"
         }
      },
      {
         "id":2,
         "author_id":2,
         "title":"How to take over the world",
         "pages":100,
         "author":{
            "id":2,
            "name":"Megatron"
         }
      }
   ]
}

With Architect now you can load related resources using ids mode and sideloading mode

Ids mode

Only load the IDs of the related resource. Using the primary key property of Eloquent models is in the roadmap.

{
   "books":[
      {
         "id":1,
         "author_id":1,
         "title":"How to save the world from evil",
         "pages":100,
         "author":1
      },
      {
         "id":2,
         "author_id":2,
         "title":"How to take over the world",
         "pages":100,
         "author":2
      }
   ]
}

Sideloading mode

Hoist the related resources into the global scope and leave behind the IDs using the ID mode resolver.

{
   "author":[
      {
         "id":1,
         "name":"Optimus Prime"
      },
      {
         "id":2,
         "name":"Megatron"
      }
   ],
   "books":[
      {
         "id":1,
         "author_id":1,
         "title":"How to save the world from evil",
         "pages":100,
         "author":1
      },
      {
         "id":2,
         "author_id":2,
         "title":"How to take over the world",
         "pages":100,
         "author":2
      }
   ]
}

Usage

Architect works with normal array's (collections and resources), Illuminate\Support\Collection and Illuminate\Database\Eloquent\Model.

<?php

$books = Book::with('Author')->get();

$architect = new \Optimus\Architect\Architect;
$parsed = $architect->parseData($books, [
    'author' => 'sideload' // can also be embed or ids (embed is default)
], 'books');

Optimus\LaravelController gives nice convenience methods to define the Architect relationships in query parameters.

Installation

composer require optimus/architect

Standards

This package is compliant with PSR-1, PSR-2 and PSR-4. If you notice compliance oversights, please send a patch via pull request.

Testing

$ phpunit

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

architect's People

Contributors

dracon avatar esbenp avatar pablo-bossi avatar

Watchers

 avatar  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.