jessegall / resources
这是一个用于在PHP应用程序中表示和管理资源的库。它提供了一个简单直观的界面来存储和访问数据,以及管理资源之间的关系。
该软件包的规范仓库似乎已消失,因此该软件包已被冻结。
v0.4.3
2023-02-14 14:31 UTC
Requires
- php: >=8.1
- jessegall/php-data-container: ^2.1
- jessegall/php-events: ^0.1.0
- jessegall/php-hooks: ^0.1.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^6.1
README
这是一个用于在PHP应用程序中表示和管理资源的软件包。它提供了一个简单直观的界面来存储和访问数据,以及管理资源之间的关系。使用此软件包,您可以轻松创建丰富的、相互关联的数据模型,从而快速构建复杂和动态的功能。
安装
composer require jessegall/resources
使用
Resource类可以用来创建包含数据和与其他Resource对象支持关系的对象。
要创建一个新的资源类,请扩展Resource类并添加任何必要的资源数据和功能。要访问和修改资源中的数据,请使用get
和set
方法。
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); } }
要创建资源的新实例,也可以使用new
方法。
$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...' ]);
要创建资源集合,请使用collection
方法。
$articles = Article::collection([ [ 'title' => 'Article 1', 'body' => 'Lorem ipsum dolor sit amet...' ], [ 'title' => 'Article 2', 'body' => 'Lorem ipsum dolor sit amet...' ] ]);
要处理资源之间的关系,请使用relation
方法。此方法允许您将资源中的数据映射到另一个资源或资源集合。
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); } }
示例
以下是一个示例,说明如何使用此Resource类在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();