hansel23/dictionaries

This package is abandoned and no longer maintained. No replacement package was suggested.

Dictionary with interface. It's losely based on the Dictionary type of other famous languages. Strict typed.

v1.0.8 2016-06-10 07:22 UTC

This package is auto-updated.

Last update: 2023-10-23 21:13:44 UTC


README

Build Status Coverage Status Latest Stable Version Latest Unstable Version Total Downloads License

Dictionary

Dictionary Type. Keys and values are strongly typed, so you will know what you get.

Usage

Immagine you have a class with member data called Member. A member has a firstnamer, lastname and an age. To identify a member you have the type MemberId.

Now you want to have an array where you can put the members into. Now it's a nice idea to get the member information of a specific member by accessing the array with the MemberId. But you can't set the MemberId as the key.

With the Dictionary type this problem is history!

You simply create a Dictionary with the MemberId as the key and the Member as the value like this:

<?php
$members = new Dictionary( MemberId::class, Member::class );
?>

Now you can add your members to the dictionary like this:

<?php	
$members->add($memberId, $member);
?>

Where $memberId is of the type MemberId and $member of the type Member.

But you can also add a member with the identifier like you can add a normal key-value-pair (string/int) in a normal array.

<?php	
$members[$memberId] = $member;
?>

Notice: Your IDE may mark it as an error, but the code will run properly!

Now if you filled your dictionary with members you can do following:

<?php
foreach( $members as $memberId => $member )
{
    printf( "Id: %s, Name: %s %s, $memberId->toString(), $member->getFirstName(), $member->getLastName() );
}
?>

Or you want to get a specific member of the dictionary by member id:

<?php
print_r( $members[$memberId] );
?>

Another nice thing is, that you can now better type hint your arrays. Imagine you create an interface with a public method that needs an array as argument. But you are expecting a specific type of objects in the array. Sure you can type hint arrays, but do you really want to hope, that everyone is giving you the right array? Or do you want to validate the right type of objects in the array in your implementations?

Better this way:

  1. Create a new Dictionary class, e.g. MemberDictionary
  2. Extend AbstractDictionary.
  3. Add typehints for the key and value in your IDE
  4. Use your own dictionary as the typehint!

Like this:

<?php
use Hansel23/Dictionaries/Dictionary;
/**
 * @method MemberId key()
 * @method Member current()
 **/
final class MemberDictionary extends AbstractDictionary
{
   	protected function getNameOfKeyType()
   	{
   		return MemberId::class;
   	}
   
   	protected function getNameOfValueType()
   	{
   		return Member::class;
   	}
}
?>

Hint: You can also use scalar types in your dictionary! Look at the following example:

<?php
final class StringIntegerDictionary extends Dictionary
{
    private $keyType;
    private $valueType
    public function __construct()
    {
        parent::__construct();
        
        $this->keyType   = gettype( 1 );
        $this->valueType = gettype( '' );
    }
    
    protected function getNameOfKeyType()
    {
        return $this->keyType;
    }
   
    protected function getNameOfValueType()
    {
        return $this->valueType;
    }
}