germania-kg/salesmen

Work with Salesman IDs: Interfaces, Traits, FilterIterator

2.2.2 2022-03-30 10:16 UTC

README

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

Installation

$ composer require germania-kg/salesmen

Interfaces

SalesmanIdProviderInterface

public function getSalesmanId()

SalesmanIdAwareInterface

extends SalesmanIdProviderInterface
public function setSalesmanId( $id )

Traits

SalesmanIdProviderTrait

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

public $salesman_id;
public function getSalesmanId()

SalesmanIdAwareTrait

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

use SalesmanIdProviderTrait;
public function setSalesmanId( $salesman )

Examples

<?php
use Germania\Salesmen\SalesmanIdProviderInterface;
use Germania\Salesmen\SalesmanIdProviderTrait;

class Salesman implements SalesmanIdProviderInterface
{
	use SalesmanIdProviderTrait;
	
	public function __construct( $salesman_id )
	{
		$this->salesman_id = $salesman_id;
	}
}

$salesman = new Salesman( 99 );
echo $salesman->getSalesmanId(); // 99
<?php
use Germania\Salesmen\ SalesmanIdAwareInterface;
use Germania\Salesmen\SalesmanIdAwareTrait;

class MyOrder implements SalesmanIdAwareInterface
{
	use SalesmanIdAwareTrait;
}

$order  = new MyOrder;
$order->setSalesmanId( 34 );
echo $order->getSalesmanId(); // 34

SalesmanFilterIterator

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

Iterator:

  • instances of SalesmanIdProviderInterface

Filter values:

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

Example:

<?php
use Germania\Salesmen\SalesmanFilterIterator;

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

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

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

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


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

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

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


// ---------------------------------------
// Filter by SalesmanIdProviderInterface:
// ---------------------------------------

$salesman = new Salesman( 1 );
$filter = new SalesmanFilterIterator( new \ArrayIterator( $orders ), $salesman);

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

Issues

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

Also see full issues list.

Development

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

Unit tests

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

$ composer test
# or
$ vendor/bin/phpunit