equicolor/yii2-value-objects

在AR中轻松存储嵌套对象和类型化集合

安装: 51

依赖项: 0

建议者: 0

安全: 0

星级: 2

关注者: 2

分支: 1

开放问题: 1

类型:yii2-extension

dev-master 2017-11-30 08:56 UTC

This package is not auto-updated.

Last update: 2024-09-19 18:22:06 UTC


README

通过嵌套对象和类型化集合扩展ActiveRecord,序列化和存储在表字段中

示例

class User extends ActiveRecord
{
    // using value objects require attached behavior
    public function behaviors()
    {
        return [
            ValueObjectsBehavior::className(),
        ];
    }

    /**
     * Value objects map
     *
     * @return array
     */
    public static function valueObjects() {
        // define value objects on model attributes
        return [
            // $this->profile attribute will be an instance of defined anonymous class
            'profile' => new class extends ValueObject {
                public $github;
                public $phones = [];
            },
        ];
    }
}

$user = new User();
$user->profile->github = 'https://github.com/equicolor/';
$user->profile->phones[] = '555-55-555';
$user->save();

现在 profile 字段在 user 表中包含json

{"github":"https://github.com/equicolor/","phones":["555-55-555"]}

它将在afterFind事件后转换为对象。

带有集合的更复杂示例

<?php
use equicolor\ValueObjectList;
use equicolor\ValueObject;

use yii\db\ActiveRecord; 

use equicolor\valueObjects\ValueObjectsBehavior;

/**
 * @property integer $id
 * @property Offer $offer
 */
class Campaign extends ActiveRecord
{
    public function behaviors()
    {
        return [
            ValueObjectsBehavior::className(),
        ];
    }
    /**
     * Value objects map
     *
     * @return void
     */
    public static function valueObjects() {
        return [
            // you can define value object as simple class
            'offer' => new Offer,
        ];
    }

    // other methods ...
}

// and feel free to use any possibilites of objects and classes
class Offer extends ValueObject {
    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;
    
    public $id;
    public $title;
    public $status;

    public static function valueObjects($model) {
        return [
            // $campaign->offer->goals is array of Goal objects
            'goals' => ValueObjectList::create(Goal::className()),
            'targeting' => new class extends ValueObject {
                public $country;
                public $age;
            }
        ];
    }
}

class Goal extends ValueObject {
    public $stake;
    public $title;

    // you can define your own methods
    public function getReward(float $reward) {
        return sprintf('%.2f', ($reward / 100) * $this->stake);
    }
}

$campaign = Campaign::find()->one();
// $campaign->offer was converted to object on afterFind event

$campaign->offer->status = Offer::STATUS_ACTIVE;
$goal = $campaign->offer->goals[] = new Goal([
    'title' => 'win',
    'stake' => 50
]);
echo $goal->getReward($offer->reward);

// and store changes
$campaign->save();

路线图

  • 重构
  • 测试
  • 适当的错误处理
  • 验证
  • 独立的序列化引擎

欢迎创建问题 =)