monooso/base-model

此包已被废弃,不再维护。未建议替代包。

世界上最简单的基模型,支持自定义获取器和设置器。

0.1.0 2016-09-21 15:19 UTC

This package is auto-updated.

Last update: 2021-11-03 19:32:27 UTC


README

世界上最简单的基模型,支持自定义获取器和设置器。

安装

使用以下Composer安装Base Model

$ composer require monooso/base-model

使用方法

在以下示例模型中,将直接设置和获取 simple 属性,而 fancy 属性将通过自定义获取器和设置器方法设置和获取。

<?php namespace MyProject

use InvalidArgumentException;
use Monooso\BaseModel\Model;

class Example extends Model
{
    protected $fancy;
    protected $simple;

    /**
     * Prepends a string to the 'fancy' property, before returning it.
     *
     * @return string
     */
    public function getFancy()
    {
        return 'fancy-' . $this->fancy;
    }

    /**
     * Validates that the 'fancy' property is a string.
     *
     * @param mixed $val
     *
     * @throws InvalidArgumentException
     */
    public function setFancy($val)
    {
        if (!is_string($val)) {
            throw new InvalidArgumentException('Value must be a string');
        }

        $this->fancy = $val;
    }
}