chalcedonyt/laravel-specification

1.0.1 2018-08-06 09:42 UTC

This package is not auto-updated.

Last update: 2024-09-22 05:04:45 UTC


README

这是对 Specification Pattern 的一个改编,参考自 https://github.com/domnikl/DesignPatternsPHP,增加了一个 artisan 命令以便快速创建规范。

安装

通过 Composer(请将您的 minimum-stability 设置为 "dev")

$ composer require chalcedonyt/laravel-specification

然后运行 composer update。Composer 完成后,将服务提供者添加到 app/config/app.php 文件中的 providers 数组。

Chalcedonyt\Specification\Providers\SpecificationServiceProvider::class

生成规范

将添加一个 artisan 命令来快速创建规范。

php artisan make:specification [NameOfSpecification]

添加 --parameters 标志将提示输入要插入构造函数的参数

Enter the class or variable name for parameter 0 (Examples: \App\User or $value) [Blank to stop entering parameters] [(no_param)]:
 > \App\User

 Enter the class or variable name for parameter 1 (Examples: \App\User or $value) [Blank to stop entering parameters] [(no_param)]:
 > $my_value

结果如下

class NewSpecification extends AbstractSpecification
{

    /**
    * @var  \App\User
    */
    protected $user;

    /**
    * @var  
    */
    protected $myValue;


    /**
    *
    *  @param  \App\User $user
    *  @param   $myValue
    *
    */
    public function __construct(\App\User $user, $my_value)
    {
        $this->user = $user;
        $this->myValue = $my_value;
    }

    /**
    * Tests an object and returns a boolean value
    *
    * @var  mixed
    */

    public function isSatisfiedBy($candidate)
    {
        //return a boolean value
    }

}

使用方法

class AgeOfPersonSpecification extends \Chalcedonyt\Specification\AbstractSpecification
{

    protected $minAge;
    protected $maxAge;

    /**
    * Set properties here for a parameterized specification.
    *
    */
    public function __construct($min_age, $max_age)
    {
        $this->minAge = $min_age;
        $this->maxAge = $max_age;
    }

    /**
    * Tests an object and returns a boolean value
    *
    * @var Array $candidate
    */

    public function isSatisfiedBy($candidate)
    {
        return $this->minAge <= $candidate['age'] && $this->maxAge >= $candidate['age'];
    }

}
class MalePersonSpecification  extends \Chalcedonyt\Specification\AbstractSpecification
{
    const GENDER_MALE = 1;
    const GENDER_FEMALE = 2;

    /**
    * Tests an object and returns a boolean value
    *
    * @var Array candidate
    */

    public function isSatisfiedBy($candidate)
    {
        return $candidate['gender'] == self::GENDER_MALE;
    }
}
$adult_spec = new AgeOfPersonSpecification(21, 80);
$teenager_spec = new AgeOfPersonSpecification(13, 19);
$puberty_spec = new AgeOfPersonSpecification(15, 19);

$male_spec = new MalePersonSpecification;
$female_spec = new NotSpec($male_spec);

$young_teenage_female = ['age' => 13, 'gender' => MalePersonSpecification::GENDER_FEMALE ];
$teenage_female = ['age' => 15, 'gender' => MalePersonSpecification::GENDER_FEMALE ];
$adult_female = ['age' => 22, 'gender' => MalePersonSpecification::GENDER_FEMALE ];

$nested_female_spec =  $puberty_spec->andSpec( $teenager_spec->andSpec( $female_spec ) );
$this->assertEquals( $nested_female_spec->isSatisfiedBy( $teenage_female ), true );
$this->assertEquals( $nested_female_spec->isSatisfiedBy( $young_teenage_female ), false );

$any_young_female_spec = $female_spec->andSpec( $teenager_spec->orSpec( $puberty_spec ));
$this->assertEquals( $nested_female_spec->isSatisfiedBy( $teenage_female ), true );
$this->assertEquals( $nested_female_spec->isSatisfiedBy( $adult_female ), false );

您也可以通过 remainderUnsatisfiedBy 属性检索未满足的规范

$any_age_spec = new AgeOfPersonSpecification(1, 80);

$male_spec = new MalePersonSpecification;
$female_spec = new NotSpec($male_spec);

$male = ['age' => 16, 'gender' => MalePersonSpecification::GENDER_MALE ];

$any_young_female_spec = new AndSpec( $female_spec, $any_age_spec );
$this->assertEquals( $any_young_female_spec->isSatisfiedBy( $male ), false );

//returns the $female_spec
$unfulfilled_spec =  $any_young_female_spec->remainderUnsatisfiedBy( $male );
$inverse_female_spec = new NotSpec( $unfulfilled_spec );
$this->assertEquals( $inverse_female_spec->isSatisfiedBy( $male ), true );

变更日志

  • 0.4.4 现在您可以通过在类名中指定目录来在目录内部创建规范,例如 php artisan make:specification MyDir\\MySpec
  • 0.4.2 从抽象和接口中移除了 isSatisfiedBy 方法。这允许对 $candidate 进行类型提示。
  • 0.4.1 调整了生成的视图,以使用 camel_case 格式化任何参数。
  • 0.4 更新了控制台命令。您现在可以通过输入 --parameters 标志为规范生成器指定构造函数参数。
  • 0.3 移除了对 isSatisfiedBy 的参数进行类型提示的功能,因为 PHP 不允许重载抽象方法。
  • 0.2 添加了 remainderUnsatisfiedBy 函数

测试

致谢

许可

MIT 许可证(MIT)。有关更多信息,请参阅许可文件