2017 © Pedro Peláez
 

library enum-mapper

Component provides easy way to emulate ENUM behaviour on the PHP layer instead database and convert DB value into human representation and vise versa

image

adrenalinkin/enum-mapper

Component provides easy way to emulate ENUM behaviour on the PHP layer instead database and convert DB value into human representation and vise versa

  • Tuesday, April 10, 2018
  • by adrenalinkin
  • Repository
  • 1 Watchers
  • 1 Stars
  • 7 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Enum Mapper На Русском

Introduction

Component provides easy way to emulate ENUM behaviour on the PHP layer instead database by constant usage. Convert database value into human representation and vise versa., (*1)

Installation

Open a command console, enter your project directory and execute the following command to download the latest stable version of this component:, (*2)

    composer require adrenalinkin/enum-mapper

This command requires you to have Composer install globally., (*3)

Usage examples

Mapper creation

For start create mapper-class and extend him from AbstractEnumMapper. For second and last step - determine list of the constants with specific prefixes: * DB_ - contains database values. * HUMAN_ - contains humanized values., (*4)

Let's say we need to store user's gender. In that case we need to create mapper-class:, (*5)

<?php

use Linkin\Component\EnumMapper\Mapper\AbstractEnumMapper;

class GenderMapper extends AbstractEnumMapper
{
    const DB_UNDEFINED = 0;
    const DB_MALE      = 10;
    const DB_FEMALE    = 20;

    const HUMAN_UNDEFINED = 'Undefined';
    const HUMAN_MALE      = 'Male';
    const HUMAN_FEMALE    = 'Female';
}

Usage

fromDbToHuman

Get humanized value by received database value:, (*6)

<?php

    $mapper        = new GenderMapper();
    $dbGenderValue = GenderMapper::DB_MALE; // 10
    $humanValue    = $mapper->fromDbToHuman($dbGenderValue);

Variable $humanValue will be contain Male value. Note: When you will try to get value of the unregistered value will be throws UndefinedMapValueException., (*7)

fromHumanToDb

Get database value by received humanized value:, (*8)

<?php

    $mapper           = new GenderMapper();
    $humanGenderValue = GenderMapper::HUMAN_FEMALE; // Female
    $dbValue          = $mapper->fromHumanToDb($humanGenderValue);

Variable $dbValue will be contain 20 value. Note: When you will try to get value of the unregistered value will be throws UndefinedMapValueException., (*9)

getMap

Get full list of the available pairs of the database and humanized values:, (*10)

<?php

    $mapper = new GenderMapper();
    $map    = $mapper->getMap(); // [0 => 'Undefined', 10 => 'Male', 20 => 'Female']

Constant usage

All the time you available to use constant as is:, (*11)

<?php

    if (GenderMapper::DB_UNDEFINED === $maleFromForm) {
        throw new \Exception('Field "Gender" is required in this form');
    }

getAllowedDbValues and getAllowedHumanValues

Get list of the all available value for the database values or for the humanized values:, (*12)

<?php

    $mapper       = new GenderMapper();
    $allowedDb    = $mapper->getAllowedDbValues();    // [0, 10, 20]
    $allowedHuman = $mapper->getAllowedHumanValues(); // ['Undefined', 'Male', 'Female']

    // Exclude values from result
    $allowedDb    = $mapper->getAllowedDbValues([GenderMapper::DB_UNDEFINED]);       // [10, 20]
    $allowedHuman = $mapper->getAllowedHumanValues([GenderMapper::HUMAN_UNDEFINED]); // ['Male', 'Female']

getRandomDbValue и getRandomHumanValue

Get random database or humanized value:, (*13)

<?php

    $mapper      = new GenderMapper();
    $randomDb    = $mapper->getRandomDbValue();    // 0 || 10 || 20
    $randomHuman = $mapper->getRandomHumanValue(); // Undefined || Male || Female

    // Exclude values from result
    $randomDb    = $mapper->getRandomDbValue([GenderMapper::DB_UNDEFINED]);       // 10 || 20
    $randomHuman = $mapper->getRandomHumanValue([GenderMapper::HUMAN_UNDEFINED]); // Male || Female

License

license, (*14)

The Versions

10/04 2018

dev-master

9999999-dev

Component provides easy way to emulate ENUM behaviour on the PHP layer instead database and convert DB value into human representation and vise versa

  Sources   Download

MIT

The Requires

  • php ~5.4||~7.0

 

by Viktor Linkin

database component php enum convert humanize human-readable enum-mapper humanized-values

15/02 2018

v1.0.3

1.0.3.0

Component provides easy way to emulate ENUM behaviour on the PHP layer instead database and convert DB value into human representation and vise versa

  Sources   Download

MIT

The Requires

  • php ~5.4||~7.0

 

by Viktor Linkin

database component php enum convert humanize

02/02 2018

v1.0.2

1.0.2.0

This component provides easy way to simulate ENUM behaviour on the PHP layer instead database and convert DB value into human representation and vise versa

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Viktor Linkin

database component php enum humanize

21/11 2017

v1.0.1

1.0.1.0

This component provides easy way to simulate ENUM behaviour on the PHP layer instead database and convert DB value into human representation and vise versa

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Viktor Linkin

database component php enum humanize

26/07 2017

v1.0.0

1.0.0.0

This component provides easy way to simulate ENUM behaviour on the PHP layer instead database and convert DB value into human representation and vise versa

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Viktor Linkin

database component php enum humanize