举报投诉联系我们 手机版 热门标签 名动网
您的位置:名动网 > php标准格式 PHP 规格模式

php标准格式 PHP 规格模式

2023-06-04 02:20 PHP设计模式

php标准格式 PHP 规格模式

php标准格式 PHP 规格模式

php标准格式

目的

构建清晰的业务规则规范,在其中可以对对象进行检查。每个规范类有一个名为[isSatisfiedBy]的方法,根据给定对象是否满足规范返回true或false。

UML 图

Alt Specification UML Diagram

代码

Item.php

<?php

declare(strict_types=1);

namespace DesignPatternsBehavioralSpecification;

class Item
{
    public function __construct(private float $price)
    {
    }

    public function getPrice(): float
    {
        return $this->price;
    }
}

Specification.php

<?php

declare(strict_types=1);

namespace DesignPatternsBehavioralSpecification;

interface Specification
{
    public function isSatisfiedBy(Item $item): bool;
}

OrSpecification.php

<?php

declare(strict_types=1);

namespace DesignPatternsBehavioralSpecification;

class OrSpecification implements Specification
{
    
    private array $specifications;

    
    public function __construct(Specification ...$specifications)
    {
        $this->specifications = $specifications;
    }

    
    public function isSatisfiedBy(Item $item): bool
    {
        foreach ($this->specifications as $specification) {
            if ($specification->isSatisfiedBy($item)) {
                return true;
            }
        }

        return false;
    }
}

PriceSpecification.php

<?php

declare(strict_types=1);

namespace DesignPatternsBehavioralSpecification;

class PriceSpecification implements Specification
{
    public function __construct(private ?float $minPrice, private ?float $maxPrice)
    {
    }

    public function isSatisfiedBy(Item $item): bool
    {
        if ($this->maxPrice !== null && $item->getPrice() > $this->maxPrice) {
            return false;
        }

        if ($this->minPrice !== null && $item->getPrice() < $this->minPrice) {
            return false;
        }

        return true;
    }
}

AndSpecification.php

<?php

declare(strict_types=1);

namespace DesignPatternsBehavioralSpecification;

class AndSpecification implements Specification
{
    
    private array $specifications;

    
    public function __construct(Specification ...$specifications)
    {
        $this->specifications = $specifications;
    }

    
    public function isSatisfiedBy(Item $item): bool
    {
        foreach ($this->specifications as $specification) {
            if (!$specification->isSatisfiedBy($item)) {
                return false;
            }
        }

        return true;
    }
}

NotSpecification.php

<?php

declare(strict_types=1);

namespace DesignPatternsBehavioralSpecification;

class NotSpecification implements Specification
{
    public function __construct(private Specification $specification)
    {
    }

    public function isSatisfiedBy(Item $item): bool
    {
        return !$this->specification->isSatisfiedBy($item);
    }
}

测试

Tests/SpecificationTest.php

<?php

declare(strict_types=1);

namespace DesignPatternsBehavioralSpecificationTests;

use DesignPatternsBehavioralSpecificationItem;
use DesignPatternsBehavioralSpecificationNotSpecification;
use DesignPatternsBehavioralSpecificationOrSpecification;
use DesignPatternsBehavioralSpecificationAndSpecification;
use DesignPatternsBehavioralSpecificationPriceSpecification;
use PHPUnitFrameworkTestCase;

class SpecificationTest extends TestCase
{
    public function testCanOr()
    {
        $spec1 = new PriceSpecification(50, 99);
        $spec2 = new PriceSpecification(101, 200);

        $orSpec = new OrSpecification($spec1, $spec2);

        $this->assertFalse($orSpec->isSatisfiedBy(new Item(100)));
        $this->assertTrue($orSpec->isSatisfiedBy(new Item(51)));
        $this->assertTrue($orSpec->isSatisfiedBy(new Item(150)));
    }

    public function testCanAnd()
    {
        $spec1 = new PriceSpecification(50, 100);
        $spec2 = new PriceSpecification(80, 200);

        $andSpec = new AndSpecification($spec1, $spec2);

        $this->assertFalse($andSpec->isSatisfiedBy(new Item(150)));
        $this->assertFalse($andSpec->isSatisfiedBy(new Item(1)));
        $this->assertFalse($andSpec->isSatisfiedBy(new Item(51)));
        $this->assertTrue($andSpec->isSatisfiedBy(new Item(100)));
    }

    public function testCanNot()
    {
        $spec1 = new PriceSpecification(50, 100);
        $notSpec = new NotSpecification($spec1);

        $this->assertTrue($notSpec->isSatisfiedBy(new Item(150)));
        $this->assertFalse($notSpec->isSatisfiedBy(new Item(50)));
    }
}


阅读全文
以上是名动网为你收集整理的php标准格式 PHP 规格模式全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 名动网 mdwl.vip 版权所有 联系我们