youknowriad/OREM

语言: PHP

git: https://github.com/youknowriad/OREM

宁静的API抽象层
Restfull API Abstraction Layer
README.md (中文)

Rizeway OREM

Build Status

Rizeway OREM是一个Restful API抽象层。宁静的API是什么原则对数据库。

入门

假设您有以下JSON HTTP API在名为“status”的对象上进行CRUD

GET /status # Return a list of statuses
GET /status/1 # Return the status of id 1
POST /status # Create a status (the body of the request contain the hash of the status)
PUT /status/1 # Update the status of id 1 (the body of the request contain the hash of the status)
DELETE /status/1 # Delete the status of id 1

1-创建一个用于存储映射的文件夹。

2-在此文件夹中创建映射文件。映射文件应该命名为status.orem.yml,看起来像这样。

class: MyNamespace/Status
fields:
    id:
        primaryKey: true
    message:
        type: string
    author:
        type: string
    count_likes
        type: integer    

3-创建一个简单的实体类

<?php

namespace MyNamespace 

class Status 
{
    protected $id;
    protected $message;
    protected $author;
    protected $count_likes = 0;

    public function getId()
    {
        return $this->id;
    }

    public function getMessage()
    {
        return $this->message;
    }

    public function setMessage($message)
    {
        $this->message = $message;
    }

    public function getAuthor()
    {
        return $this->author;
    }

    public function setAuthor($author)
    {
        $this->author = $author;
    }

    public function getCountLikes()
    {
        return $this->count_likes;
    }

    public function addLike()
    {
        $this->count_likes++;
    }
}

4-获取OREM经理

$factory = new \Rizeway\OREM\Config\Factory($directory, $apiBaseUrl);
$manager = $factory->getManager();

5-如何使用管理器进行api调用

$status = new \MyNamespace\Status();
$status->setMessage('my message');
$status->setAuthor('author');
$manager->persist($status); // Call POST API

$status->addLike();
$manager->update($status); // Call PUT API

$manager->remove($status); // Call DELETE API

$statuses = $manager->getRepository('status')->findAll(); // Call GET api and return an array of \MyNamespace\Status

$status = $manager->getRepository('status')->find(1); // GET api with primary key, return an object \MyNamespace\Status

安装

使用composer安装

{
    "require": {
        "rizeway/orem": "0.1.*@dev"
    }
}

路线图

  • 网址自定义
  • 处理HasMany和HasOne延迟加载
  • 处理级联删除和选项以禁用级联更新
  • 自定义api功能
  • 更多字段类型
  • URL中的额外参数(如CAS票证或其他身份验证令牌)

有助于

使用composer安装依赖项,您就可以开始了

git clone https://github.com/youknowriad/OREM.git && cd OREM
curl -s http://getcomposer.org/installer | php
./composer.phar install --dev

测试

OREM使用atoum进行测试

./bin/atoum --test-all

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

Rizeway OREM

Build Status

Rizeway OREM is a Restful API Abstraction Layer. It is to Restful APIs what doctrine is to databases.

Getting Started

Say you have the following JSON HTTP API to make CRUD on an object named "status"

GET /status # Return a list of statuses
GET /status/1 # Return the status of id 1
POST /status # Create a status (the body of the request contain the hash of the status)
PUT /status/1 # Update the status of id 1 (the body of the request contain the hash of the status)
DELETE /status/1 # Delete the status of id 1

1- Create a folder to store your mappings.

2- Create a mapping file in this folder. The mapping file sould be named status.orem.yml and will look like this.

class: MyNamespace/Status
fields:
    id:
        primaryKey: true
    message:
        type: string
    author:
        type: string
    count_likes
        type: integer    

3- Create a simple entity class

<?php

namespace MyNamespace 

class Status 
{
    protected $id;
    protected $message;
    protected $author;
    protected $count_likes = 0;

    public function getId()
    {
        return $this->id;
    }

    public function getMessage()
    {
        return $this->message;
    }

    public function setMessage($message)
    {
        $this->message = $message;
    }

    public function getAuthor()
    {
        return $this->author;
    }

    public function setAuthor($author)
    {
        $this->author = $author;
    }

    public function getCountLikes()
    {
        return $this->count_likes;
    }

    public function addLike()
    {
        $this->count_likes++;
    }
}

4- Get an OREM Manager

$factory = new \Rizeway\OREM\Config\Factory($directory, $apiBaseUrl);
$manager = $factory->getManager();

5- How to use the manager to make api calls

$status = new \MyNamespace\Status();
$status->setMessage('my message');
$status->setAuthor('author');
$manager->persist($status); // Call POST API

$status->addLike();
$manager->update($status); // Call PUT API

$manager->remove($status); // Call DELETE API

$statuses = $manager->getRepository('status')->findAll(); // Call GET api and return an array of \MyNamespace\Status

$status = $manager->getRepository('status')->find(1); // GET api with primary key, return an object \MyNamespace\Status

Installation

Install using composer

{
    "require": {
        "rizeway/orem": "0.1.*@dev"
    }
}

Roadmap

  • Url Customisation
  • Handling HasMany and HasOne lazy loading
  • Handling Cascade Delete And Options to disable Cascade update
  • Custom api functions
  • More Field Types
  • Extra parameters in URL (like CAS ticket or Other auth token)

Contribute

Install the dependencies using composer and you're ready to go

git clone https://github.com/youknowriad/OREM.git && cd OREM
curl -s http://getcomposer.org/installer | php
./composer.phar install --dev

Tests

OREM is tested using atoum

./bin/atoum --test-all