定制 hansel23/strongly-typed 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hansel23/strongly-typed

最新稳定版本:v1.0.0

Composer 安装命令:

composer require hansel23/strongly-typed

包简介

Small library to encourage using stricter types in PHP

README 文档

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 );

统计信息

  • 总下载量: 118
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2015-11-02