germania-kg/retailers

Work with Retailers: Interfaces, Traits, FilterIterator

2.0.3 2022-03-30 10:05 UTC

This package is auto-updated.

Last update: 2024-11-29 05:20:56 UTC


README

Packagist PHP version Build Status Scrutinizer Code Quality Code Coverage Build Status

Installation with Composer

$ composer require germania-kg/retailers

Interfaces

RetailerNumberProviderInterface

public function getRetailerNumber()

RetailerNumberAwareInterface

extends RetailerNumberProviderInterface
public function setRetailerNumber( $id )

Traits

RetailerNumberProviderTrait

Objects using this trait will provide a retailer_number attribute and a getRetailerNumber getter method, as outlined here:

public $retailer_number;
public function getRetailerNumber()

RetailerNumberAwareTrait

Objects using this trait will provide anything that RetailerNumberProviderInterface provides, and additionally a setter method setRetailerNumber which accepts anything; if RetailerNumberProviderInterface given here, getRetailerNumber method will be called to obtain the ID to use. Roughly outlined:

use RetailerNumberProviderTrait;
public function setRetailerNumber( $id )

Examples

<?php
use Germania\Retailers\RetailerNumberProviderInterface;
use Germania\Retailers\RetailerNumberProviderTrait;

class Retailer implements RetailerNumberProviderInterface
{
	use RetailerNumberProviderTrait;
	
	public function __construct( $retailer_number )
	{
		$this->retailer_number = $retailer_number;
	}
}

$retailer = new Retailer( 99 );
echo $retailer->getRetailerNumber(); // 99
<?php
use Germania\Retailers\RetailerNumberAwareInterface;
use Germania\Retailers\RetailerNumberAwareTrait;

class MyOrder implements RetailerNumberAwareInterface
{
	use RetailerNumberAwareTrait;
}

$order = new MyOrder;
$order->setRetailerNumber( 34 );
echo $order->getRetailerNumber(); // 34

RetailerFilterIterator

The RetailerFilterIterator class accepts any Iterator collection and a retailer ID (or ID array) or RetailerNumberProviderInterface instance to filter for. Collection items not being an instance of RetailerNumberProviderInterface are always ignored.

Iterator:

  • instances of RetailerNumberProviderInterface

Filter values:

  • Integer or string ID
  • Array of integer or string IDs
  • One instance of RetailerNumberProviderInterface – also see issue #1

Example:

<?php
use Germania\Retailers\RetailerFilterIterator;

// Prepare some RetailerNumberProviderInterface instances:
$order1 = new MyOrder; 
$order1->setRetailerNumber( 1 );

$order2 = new MyOrder; 
$order2->setRetailerNumber( 20 );

$order3 = new MyOrder; 
$order4->setRetailerNumber( 999 );

$orders = [
	$order1,
	$order2,	
	$order3
];


// ---------------------------------------
// Filter by ID or ID array:
// ---------------------------------------

// should be '1'
$filter = new RetailerFilterIterator( new \ArrayIterator( $orders ) , 20);
echo iterator_count($filter);

// should be '2'
$filter = new RetailerFilterIterator( new \ArrayIterator( $orders ), array(20, 999));
echo iterator_count($filter);


// ---------------------------------------
// Filter by RetailerNumberProviderInterface:
// ---------------------------------------

$retailer = new Retailer( 1 );
$filter = new RetailerFilterIterator( new \ArrayIterator( $orders ), $retailer);

// should be '1'
echo iterator_count($filter);

Roadmap

Version 2.1:

  • Use Scalar type declarations; requires PHP 7.0+

Issues

  • The RetailerFilterIterator should also accept an array of RetailerNumberProviderInterface instances as filter value. See issue #1.

See full issues list.

Development

$ git clone https://github.com/GermaniaKG/Retailers.git
$ cd Retailers
$ composer install

Unit tests

Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:

$ composer test
# or
$ vendor/bin/phpunit