hansel23/strongly-typed

This package is abandoned and no longer maintained. The author suggests using the generic-lists package instead.

Small library to encourage using stricter types in PHP

v1.0.0 2016-05-09 11:47 UTC

This package is not auto-updated.

Last update: 2017-08-16 12:41:11 UTC


README

Coverage Status

My Motivation

I like strong typing. I think it makes the code more stable and easier to understand.

Generic lists

My Motivation

I want to be sure to have certain objects in the list and not only trust to have the right object in the array.

Also I want to have the most important list-functions in one object.

Usage

You can build an instance of the GenericList on the fly: new GenericList( YourOwnType::class )

Recommended usage:

  1. create a new type, e.g. YourOwnTypeList
  2. extend this type from GenericList
  3. override the constructor: public function __construct() { parent::__construct( YourOwnType::class ) }

Now you can use typehints for this list.

List Sorters

To sort a list, you have to create a sorter, that implements the SortsLists interface.

The implementation of the method compare( $object1, $object2 ) must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Pass an instance of your sorter implementation to the method sortBy of the list.

List Filters

To filter a list, you have to create a filter, that implements the FindsItems interface with the isValid( $object ) method. This method must return a boolean value to indicate whether the object matches to the filter and should be returned or not.

Pass an instance of your filter implementation to the method find, findLast or findAll of the list. Find and findLast will return one object (first or last) that matches your filter method and findAll will return a new list of the same type that contains all objects filtered by your filter.

Example

creating the list type

<?php  
	class AddressList 
		extends GenericList  
	{
		public function __construct()  
		{  
			parent::__construct( Address::class ); 
		}  
	}

creating a sorter

<?php  
class AddressSorter 
	implements SortsLists  
{
	protected $sortDirection;

	/**
	 * @param int $sortDirection
	 */
	public function __construct( $sortDirection = SORT_ASC )
	{
		$this->sortDirection = $sortDirection;
	}

	/**
	 * @param $object1
	 * @param $object2
	 *
	 * @return int
	 */
	public function compare( $address1, $address2 )
	{
		/**
		 * @var Address $address1
		 * @var Address $address2
		 */
		if( $address1->getCity() == $address2->getCity() )
		{
			if( $address1->getStreet() == $address2->getStreet() )
			{
				return 0;
			}	
			else
			{
				$result = strcmp( $address1->getStreet(), $address2->getStreet() );
			} 
		}
		else
		{
			$result = strcmp( $address1->getCity(), $address2->getCity() );
		}

		if( $this->sortDirection === SORT_DESC )
		{
			return - ( $result );
		}

		return $result;
	}
}

using typehint for the list and sort with the created sorter

<?php
class AnnoyingHandler
{		
	public function sortAddresses( AddressList $addresses )
	{
		$addressSorter = new AddressSorter( SORT_DESC );
		$addresses->sortBy( $addressSorter );			
	}
}

creating a filter

<?php
class BeginningStreetNameFilter 
	implements FindsItems
{
	private $beginningStreetName;

	public function __construct( $beginningStreetName )
	{
		if( !is_string( $beginningStreetName ) )
		{
			throw new Exception( 'Street name must be a string' );
		}
		
		$this->beginningStreetName = $beginningStreetName;
	}

	public function isValid( $address )
	{
		return ( preg_match( sprintf( '!^%s!', $this->beginningStreetName ), $address->getStreet() ) );
	}
}

using the filter

<?php
$beginningStreetNameFilter 	= new BeginningStreetNameFilter( 'Arlington' );

$firstAddressFound 			= $addresses->find( $beginningStreetNameFilter );
$lastAddressFound 			= $addresses->findLast( $beginningStreetNameFilter );
$foundAddresses				= $addresses->findAll( $beginningStreetNameFilter );