10quality/php-data-model

PHP 库,为任何类型的数据处理提供通用和可扩展的数据模型(抽象模型类)。

v1.0.3 2023-01-05 16:44 UTC

This package is auto-updated.

Last update: 2024-09-05 20:22:55 UTC


README

Latest Stable Version Total Downloads License

PHP 库,为任何类型的数据处理提供通用和可扩展的数据模型(抽象模型类)。

此库非常适合开发 MVC 框架、API 客户端、封装器或任何需要数据处理的类型的项目。

模型灵感来自 Laravel 和我们的 Wordpress MVC

要求

  • PHP >= 5.4

使用方法

在定义模型时,从 Model 抽象类扩展它们。

以下示例中,将创建一个扩展自 Model 抽象类的 Product 数据模型。

use TenQuality\Data\Model;

class Product extends Model
{
}

然后按照这种方式实例化模型:

$product = new Product;

添加数据

非常简单,只需像添加对象属性一样添加数据。

// 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

创建别名

别名是模型属性,通过类方法设置或获取。这些在模型中定义。

以下示例中,在模型中创建一个别名来显示带有货币的价格。

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));
    }
}

然后按照这种方式使用它:

$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

您还可以使用构造函数(以数组的形式传递属性)初始化模型。

$product = new Product(['price' => 19.99]);

// Echo property
echo $product->price; // Will echo 19.99

类型转换

在使用任何类型转换选项之前,模型需要定义哪些属性是可见的,哪些是隐藏的。这通过列出可见的属性来完成,如下所示:

use TenQuality\Data\Model;

class Product extends Model
{
    protected $properties = [
        'name',
        'price',
        'displayPrice',
    ];
}

注意,可以列出属性和别名。根据上面的示例,属性 brandName 将在类型转换中保持隐藏。

按照这种方式转换模型:

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
// https://php.ac.cn/manual/en/function.json-encode.php
echo $model->toJSON(JSON_NUMERIC_CHECK, 600);

集合

此包还提供了一个 Collection 类,将简化多个模型或数据记录的使用。

集合的行为类似于常规数组。

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
}

可以非常容易地对集合进行排序。

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
// https://php.ac.cn/manual/en/array.constants.php
print_r($collection->sortBy('price', SORT_NUMERIC));

可以非常容易地对集合中的数据进行分组。

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

按照这种方式转换集合:

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
// https://php.ac.cn/manual/en/function.json-encode.php
echo $collection->toJSON(JSON_NUMERIC_CHECK, 600);

指南

PSR-2 编码标准。

版权和许可

MIT 许可证 - (C) 2018 10 Quality