pbmedia/specifications

Specify objects using attributes and scores

1.0.0 2016-10-11 09:52 UTC

README

Latest Version on Packagist Software License Build Status Quality Score Total Downloads

Install

Via Composer

$ composer require pbmedia/specifications

Requirements

  • PHP 7.0 and 7.1.

Implementations

Usage

This package lets you specify objects, for example products.

use Pbmedia\Specifications\HasSpecifications;
use Pbmedia\Specifications\Interfaces\CanBeSpecified;

class NotebookProduct implements CanBeSpecified {

    use HasSpecifications;

}

Set up Attribute and Score objects:

use Pbmedia\Specifications\Interfaces\Attribute;
use Pbmedia\Specifications\Interfaces\Score;

class DiskCapacityInGB implements Attribute
{
    public function getIdentifier()
    {
        return 'DiskCapacityInGB';
    }
}

class SizeInGB implements Score
{
    private $sizeInGB;

    public function __construct($sizeInGB)
    {
        $this->sizeInGB = $sizeInGB;
    }

    public function getValue()
    {
        return $this->sizeInGB;
    }
}

Now you can 'specify' you NotebookProduct like this:

$macbookAir = new NotebookProduct;

$attribute = new DiskCapacityInGB;
$score = new SizeInGB(256);

// returns an instance of \Pbmedia\Specifications\Specifications
$specifications = $macbookAir->specifications();
$specifications->set($attribute, $score);

Take a look at the docblocks of Specifications.php to see what else you can do with it. This package also comes with a Matcher service which can sort collections of specificable objects based on 'criteria' you provide.

$matcher = new \Pbmedia\Specifications\Matcher;

$matcher->addCandidates([
    $macbookAir, $macbookPro
]);

// just as a product itself, add 'criteria' to compare against:
$matcher->specifications()->set(
    new DiskCapacityInGB, new SizeInGB(512)
);

// get a collection of notebooks sorted based on which ones are most closely to the criteria.
$matcher->get();

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email pascal@pascalbaljetmedia.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.