chalcedonyt/laravel-specification
Composer 安装命令:
composer require chalcedonyt/laravel-specification
包简介
Implementation of the specification pattern
README 文档
README
An adaptation of the Specification Pattern as done by https://github.com/domnikl/DesignPatternsPHP, adding an artisan command to quickly create specifications.
Install
Via Composer (please change your minimum-stability to "dev")
$ composer require chalcedonyt/laravel-specification
Then run composer update. Once composer is finished, add the service provider to the providers array in app/config/app.php:
Chalcedonyt\Specification\Providers\SpecificationServiceProvider::class
Generating Specifications
An artisan command will be added to quickly create specifications.
php artisan make:specification [NameOfSpecification]
Adding a --parameters flag will prompts for parameters to be inserted into the constructor when generated:
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
Results in
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 } }
Usage
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 );
You may also retrieve unfulfilled specifications via the remainderUnsatisfiedBy property
$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 );
Change log
- 0.4.4 You can now create a Specification inside a directory by specifying it in the classname, e.g.
php artisan make:specification MyDir\\MySpec - 0.4.2 Removed the
isSatisfiedBymethod from the abstract and interface. This allows type hinting on the $candidate. - 0.4.1 Tweaked the generated views to use camel_case on any parameters.
- 0.4 Updated console command. You may now specify constructor parameters for the specification generator by entering the
--parametersflag - 0.3 Removed functionality to type-hint the argument to isSatisfiedBy, as PHP doesn't allow overloading abstract methods.
- 0.2 Added
remainderUnsatisfiedByfunctions
Testing
Credits
- Dominic Liebler [https://github.com/domnikl/DesignPatternsPHP]
- C# Specification Framework by Firzen [https://specification.codeplex.com/]
- The original Specification Pattern document by Eric Evans and Martin Fowler [http://www.martinfowler.com/apsupp/spec.pdf]
License
The MIT License (MIT). Please see License File for more information.
chalcedonyt/laravel-specification 适用场景与选型建议
chalcedonyt/laravel-specification 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.7k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2015 年 09 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「specification」 「specification pattern」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chalcedonyt/laravel-specification 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chalcedonyt/laravel-specification 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chalcedonyt/laravel-specification 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Helper that decorates any SUS with a phpspec lazy object wrapper
Specification support for Doctrine
A Laravel package for the Repository Design Pattern.
Inbox pattern process implementation for your Laravel Applications
Specifications like JSON schemas and other things for mosparo
BDD extension for PHPUnit that allows to write tests as a specifications in a human-readable format.
统计信息
- 总下载量: 8.7k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-01