jessegall/resources 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jessegall/resources

最新稳定版本:v0.4.3

Composer 安装命令:

composer require jessegall/resources

包简介

A library for representing and managing resources in your PHP application. It provides a simple, intuitive interface for storing and accessing data, and for managing relations between resources.

README 文档

README

Build codecov

A package for representing and managing resources in your PHP application. It provides a simple, intuitive interface for storing and accessing data, and for managing relations between resources. With this package, you can easily create rich, interconnected data models for your application, allowing you to quickly build complex and dynamic features.

Installation

composer require jessegall/resources

Usage

The Resource class can be used to create objects that contain data and support relationships with other Resource objects.

To create a new resource class, extend the Resource class and add any necessary data and methods for your resource. To access and modify data in the resource, use the get and set methods:

use JesseGall\Resources\Resource;

class Article extends Resource
{

    public function getTitle(): string 
    {
        return $this->get('title');
    }
    
    public function setTitle(string $title)
    {
        $this->set('title', $title);
    }
    
    public function getBody(): string
    {
        return $this->get('body');
    }
    
    public function setBody(string $body): string
    {
        return $this->string('body', $body);
    }

}

To create a new instance of a resource, you can also use the new method:

$article = new Article([
    'title' => 'Example Article',
    'body' => 'Lorem ipsum dolor sit amet...'
])

// Or

$article = Article::new([
    'title' => 'Example Article',
    'body' => 'Lorem ipsum dolor sit amet...'
]);

To create a collection of resources, use the collection method:

$articles = Article::collection([
    [
        'title' => 'Article 1',
        'body' => 'Lorem ipsum dolor sit amet...'
    ],
    [
        'title' => 'Article 2',
        'body' => 'Lorem ipsum dolor sit amet...'
    ]
]);

To work with relations between resources, use the relation method. This method allows you to map data in a resource to another resource or collection of resources:

class Article extends Resource
{
    public function author(): User
    {
        return $this->relation('author', User::class);
    }

   /**
    * @return ResourceCollection<Comment>
    */
    public function comments(): ResourceCollection
    {
        return $this->relation('comments', Comment::class, true);
    }
}

Examples

Here is an example of how you might use this Resource class to represent orders and products in a RESTful API:

namespace App\Resources;

use JesseGall\Resources\Resource;
use JesseGall\Resources\ResourceCollection;

class Order extends Resource
{
    public function __construct(array $data = [])
    {
        // Set the data for the order
        parent::__construct($data);
    }

    /**
     * Map the products of the order to the Product resource type
     *
     * @return ResourceCollection<Product>
     */
    public function products(): ResourceCollection
    {
        return $this->relation('products', Product::class, true);
    }
}

class Product extends Resource
{
    public function __construct(array $data = [])
    {
        // Set the data for the product
        parent::__construct($data);
    }

    /**
     * Get the ID of the product
     *
     * @return int|null
     */
    public function getId(): ?int
    {
        return $this->get('id');
    }

    /**
     * Set the ID of the product
     *
     * @param int $id
     * @return Product
     */
    public function setId(int $id): Product
    {
        return $this->set('id', $id);
    }

    /**
     * Get the name of the product
     *
     * @return string|null
     */
    public function getName(): ?string
    {
        return $this->get('name');
    }

    /**
     * Set the name of the product
     *
     * @param string $name
     * @return Product
     */
    public function setName(string $name): Product
    {
        return $this->set('name', $name);
    }

    /**
     * Get the price of the product
     *
     * @return float|null
     */
    public function getPrice(): ?float
    {
        return $this->get('price');
    }

    /**
     * Set the price of the product
     *
     * @param float $price
     * @return Product
     */
    public function setPrice(float $price): Product
    {
        return $this->set('price', $price);
    }
}

// Create a new order with data
$order = new Order([
    'id' => 1,
    'customer' => 'John Doe',
    'products' => [
        [
            'id' => 1,
            'name' => 'Shirt',
            'price' => 19.99
        ],
        [
            'id' => 2,
            'name' => 'Pants',
            'price' => 29.99
        ]
    ]
]);

// Get the products of the order as a ResourceCollection of Product resources
$products = $order->products();

统计信息

  • 总下载量: 459
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-09-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固