2017 © Pedro Peláez
 

library php-dictionary

Static dictionary implementation for PHP

image

arodygin/php-dictionary

Static dictionary implementation for PHP

  • Tuesday, April 10, 2018
  • by webinarium
  • Repository
  • 1 Watchers
  • 2 Stars
  • 534 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

PHP Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality, (*1)

Static dictionary implementation for PHP

Requirements

PHP needs to be a minimum version of PHP 7.4., (*2)

Installation

The recommended way to install is via Composer:, (*3)

composer require webinarium/php-dictionary

Usage

To create custom dictionary you have to extend StaticDictionary class and override the $dictionary static array. After that you can use StaticDictionaryInterface to work with your dictionary., (*4)

Example dictionary:, (*5)

namespace Dictionary;

class Color extends StaticDictionary
{
    public const BLACK   = 'Black';
    public const BLUE    = 'Blue';
    public const GREEN   = 'Green';
    public const CYAN    = 'Cyan';
    public const RED     = 'Red';
    public const MAGENTA = 'Magenta';
    public const YELLOW  = 'Yellow';
    public const WHITE   = 'White';

    protected static array $dictionary = [
        self::BLACK   => '#000000',
        self::BLUE    => '#0000FF',
        self::GREEN   => '#00FF00',
        self::CYAN    => '#00FFFF',
        self::RED     => '#FF0000',
        self::MAGENTA => '#FF00FF',
        self::YELLOW  => '#FFFF00',
        self::WHITE   => '#FFFFFF',
    ];
}

Input sanitizing:, (*6)

public function setColor($color)
{
    if (Dictionary\Color::has($color)) {
        $this->color = $color;
    }
}

Symfony validation:, (*7)

use Symfony\Component\Validator\Constraints;

class Settings
{
    /**
     * @Constraints\NotNull()
     * @Constraints\Choice(callback = {"Dictionary\Color", "keys"})
     */
    public $color;
}

Symfony form:, (*8)

class ColorType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('color', ChoiceType::class, [
            'label'   => 'color',
            'choices' => array_flip(Dictionary\Color::all()),
        ]);
    }
}

Please note, if you try to get a value from your dictionary using non-existing key, you will get NULL without any failures or warnings. Sometimes it's useful to have a default fallback value to be returned instead of NULL. This can be done by defining a FALLBACK constant in your dictionary class:, (*9)

class Shell extends StaticDictionary
{
    public const FALLBACK = self::UNITY;

    public const XFCE  = 1;
    public const KDE   = 2;
    public const GNOME = 3;
    public const LXDE  = 4;
    public const UNITY = 5;
    public const MATE  = 6;

    protected static array $dictionary = [
        self::UNITY => 'Unity',
        self::GNOME => 'Gnome',
        self::KDE   => 'KDE',
        self::LXDE  => 'LXDE',
        self::XFCE  => 'Xfce',
        self::MATE  => 'MATE',
    ];
}

// This returns 'Gnome'
Shell::get(Shell::GNOME);

// This returns 'Unity'
Shell::get(Color::BLACK);

If your dictionary should be built in run-time, you may skip the $dictionary static array and overload dictionary() static function instead of that:, (*10)

class Timezone extends StaticDictionary
{
    const FALLBACK = 'UTC';

    protected static function dictionary(): array
    {
        return timezone_identifiers_list();
    }
}

Development

./bin/php-cs-fixer fix
XDEBUG_MODE=coverage ./bin/phpunit --coverage-text

The Versions

10/04 2018

dev-master

9999999-dev https://github.com/webinarium/php-dictionary

Static dictionary implementation for PHP

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

dictionary

23/03 2017

1.1.2

1.1.2.0 https://github.com/webinarium/php-dictionary

Static dictionary implementation for PHP

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

dictionary

29/07 2016

1.1.1

1.1.1.0 https://github.com/arodygin/php-dictionary

Static dictionary implementation for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

dictionary

29/07 2016

1.1.0

1.1.0.0 https://github.com/arodygin/php-dictionary

Static dictionary implementation for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

dictionary

31/05 2016

1.0.3

1.0.3.0 https://github.com/arodygin/php-dictionary

Static dictionary implementation for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

dictionary

02/05 2016

1.0.2

1.0.2.0 https://github.com/arodygin/php-dictionary

Static dictionary implementation for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

dictionary

20/04 2016

1.0.1

1.0.1.0 https://github.com/arodygin/php-dictionary

Static dictionary implementation for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

dictionary

19/04 2016

1.0.0

1.0.0.0 https://github.com/arodygin/php-dictionary

Static dictionary implementation for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

dictionary