10quality/php-data-model
Composer 安装命令:
composer require 10quality/php-data-model
包简介
PHP Library that provides generic and scalable Data Models (abstract model class) for any type of data handling.
README 文档
README
PHP Library that provides generic and scalable Data Models (abstract model class) for any type of data handling.
This perfect and suitable for when developing MVC frameworks, Api clients, wrappets and/or any type of project that requires data handling.
Models inspired by Laravel and our very own Wordpress MVC.
Requires
- PHP >= 5.4
Usage
When defining your models, extend them from the Model abstract class.
In the following example, a Product data model will be created and will extend from the Model abstract class.
use TenQuality\Data\Model; class Product extends Model { }
Then instantiate your models like this:
$product = new Product;
Adding data
Very easy, simply add the data like an object property.
// Set data $product->price = 19.99; $product->name = 'My product'; $product->brandName = '10 Quality'; // Get data echo $product->price; // Will echo 19.99 echo $product->name; // Will echo My product echo $product->brandName; // Will echo 10 Quality
Creating aliases
Aliases are model properties that are set or get by class methods. This are defined in the model.
In the following example, an alias will be created in the model to display prices with currenty.
use TenQuality\Data\Model; class Product extends Model { /** * Method for alias property `displayPrice`. * Return `price` property concatenated with the $ (dollar) symbol */ protected function getDisplayPriceAlias() { return '$'.$this->price; } /** * Method for alias property `displayPrice`. * Sets a the price value if the alias is used. */ protected function setDisplayPriceAlias($value) { $this->price = floatval(str_replace('$', '', $value)); } }
Then use it like this:
$product->price = 19.99; // Get alias property echo $product->displayPrice; // Will echo $19.99 // Set alias property $product->displayPrice = 29.99; // Echo property echo $product->price; // Will echo 29.99 echo $product->displayPrice; // Will echo $29.99
You can also init the models with the construct method (passing properties as an array):
$product = new Product(['price' => 19.99]); // Echo property echo $product->price; // Will echo 19.99
Casting
Before using any casting options, the model needs to define which properties are visible and which are hidden. This is done by listing the visible ones like this:
use TenQuality\Data\Model; class Product extends Model { protected $properties = [ 'name', 'price', 'displayPrice', ]; }
Notice that both properties and aliases can be listed. Following the samples above, property brandName will stay hidden from casting.
Cast the model like this:
var_dump($model->toArray()); // To array var_dump($model->__toArray()); // To array echo (string)$model; // To json encoded string echo $model->toJSON(); // To json encoded string echo $model->__toJSON(); // To json encoded string // You can force JSON casting to be encoded using different options and depth, as described in PHPs documentation // http://php.net/manual/en/function.json-encode.php echo $model->toJSON(JSON_NUMERIC_CHECK, 600);
Collections
This package also provides a Collection class that will facilitate the use of multiple models or data records.
Collections behave like normal arrays would:
use TenQuality\Data\Collection; $collection = new Collection; // Add your models as you would normally do with arrays $collection[] = $product; $collection[] = $product; echo count($collection); // Thrown count var_dump($collection[0]); // Dumps first model added // Loop a collection foreach ($collection as $product) { // Do your stuff }
Collections can be sorted very easily:
use TenQuality\Data\Collection; $collection = new Collection; // Add your models as you would normally do with arrays $collection[] = new Product(['price' => 99.99]); $collection[] = new Product(['price' => 2.99]); // Loop a collection foreach ($collection->sortBy('price') as $product) { echo $product->price; // 2.99 will display first and then 99.99 } // Change sorting criteria // http://php.net/manual/en/array.constants.php print_r($collection->sortBy('price', SORT_NUMERIC));
Data in a collection can be grouped very easily:
use TenQuality\Data\Collection; $collection = new Collection; // Add your models as you would normally do with arrays $collection[] = new Fruit(['name' => 'Apple', 'color' => 'red']); $collection[] = new Fruit(['name' => 'Banana', 'color' => 'yellow']); $collection[] = new Fruit(['name' => 'Strawberry', 'color' => 'red']); $collection[] = new Fruit(['name' => 'Orange', 'color' => 'orange']); // Loop a collection foreach ($collection->groupBy('color') as $color => $fruits) { // This will group the data in sub arrays based on colors echo $color; foreach ($fruits as $fruit) { echo $fruit; // Fruit in the color grouped by. } }
Cast the colleciton like this:
var_dump($collection->toArray()); // To array var_dump($collection->__toArray()); // To array echo (string)$collection; // To json encoded string echo $collection->toJSON(); // To json encoded string echo $collection->__toJSON(); // To json encoded string // You can force JSON casting to be encoded using different options and depth, as described in PHPs documentation // http://php.net/manual/en/function.json-encode.php echo $collection->toJSON(JSON_NUMERIC_CHECK, 600);
Guidelines
PSR-2 coding standards.
Copyright and License
MIT License - (C) 2018 10 Quality.
10quality/php-data-model 适用场景与选型建议
10quality/php-data-model 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.53k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2018 年 05 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「data」 「models」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 10quality/php-data-model 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 10quality/php-data-model 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 10quality/php-data-model 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Wordpress Query Builder class library for custom models and data querying.
Adds the EDTF data type to Wikibase
A simple library that allows transform any kind of data to native php data or whatever
A Laravel package for managing model drafts.
A package to better handle database operations via Eloquent Models.
统计信息
- 总下载量: 23.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 22
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-05-30