2017 © Pedro Peláez
 

library label

A simple way to organise constant collections and constant=>string dictionaries.

image

rsmike/label

A simple way to organise constant collections and constant=>string dictionaries.

  • Monday, September 18, 2017
  • by rsmike
  • Repository
  • 1 Watchers
  • 0 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

PHP Labels

A simple way to organise constant collections and constant=>string dictionaries., (*1)

Example label class:

/**
 * An example label class.
 *
 * @method static string|array status($item = [])
 * @method static string|array abbr($item = [])
 */

abstract class TestLabels extends rsmike\label\Label
 {
    const INACTIVE = 0;
    const ACTIVE = 1;
    const OTHER = 3;

    protected static $status = [
        self::ACTIVE => 'Active',
        self::INACTIVE => 'Inactive',
        self::OTHER => 'Other',
    ];

    protected static $abbr = [
        self::ACTIVE => 'A',
        self::INACTIVE => 'I'
    ];
 }
 ```

#### Notes:
* make sure to declare '@method static' PHPDoc for autocomplete to work
* classes should be declared abstract to avoid instantiation

### Usage:
`TestLabels::status(TestLabels::ACTIVE);` Returns: 'Active'

`TestLabels::abbr(0);` Returns: 'I'

`TestLabels::status(2);` Returns: 2 (pass-through)

`TestLabels::status(3);` Returns: 'Other'

`TestLabels::status(null);` Returns: null (pass-through)

`TestLabels::status();` Returns:  [1=>'Active', 0=>'Inactive'] (complete set of options. Useful for dropdowns etc. )

`TestLabels::status([]);` Returns: [1=>'Active', 0=>'Inactive']  (same as above)

`TestLabels::status([0]);` Returns: [1, 0] (all available keys)

`TestLabels::status([[0, 3]]);` Returns: [0=>'Inactive', 3=>'Other'] (subset)

#### Default value:

An additional parameter may be passed as a default value instead of pass-through:

`TestLabels::status(2, 'N/A');` Returns: 'N/A' (key not found)

`TestLabels::status(null, 'N/A');` Returns: 'N/A' (always returns default value on null)

#### Yes/No:

YN method is a built-in example and may be used in any sub-class
`TestLabels::YN(1)` Returns: 'Yes'

$YN is protected and may be overridden in a subclass.

## Installation

Either run
```bash
$ composer require rsmike/label:~1.1

or add, (*2)

"rsmike/label": "~1.1"

to the require section of your composer.json file., (*3)

Changelog

v1.1
  • Subset functionality
v1.0
  • YN is now builtin
  • label() method removed
  • Fallback value functionality
v0.3.4
  • changed default shortcuts to [] and [0]
v0.3.4
  • array keys shortcut
v0.3
  • dev version

TODO

  • tests

The Versions

18/09 2017

dev-master

9999999-dev

A simple way to organise constant collections and constant=>string dictionaries.

  Sources   Download

MIT

php dictionary constant label

18/09 2017

0.3.6

0.3.6.0

A simple way to organise constant collections and constant=>string dictionaries.

  Sources   Download

MIT

php dictionary constant label

14/09 2017

0.3.5

0.3.5.0

A simple way to organise constant collections and constant=>string dictionaries.

  Sources   Download

MIT

php dictionary constant label

14/09 2017

0.3.4

0.3.4.0

A simple way to organise constant collections and constant=>string dictionaries.

  Sources   Download

MIT

php dictionary constant label

12/09 2017

0.3.3

0.3.3.0

A simple way to organise constant collections and constant=>string dictionaries.

  Sources   Download

MIT

php dictionary constant label

12/09 2017

0.3.2

0.3.2.0

A simple way to organise constant collections and constant=>string dictionaries.

  Sources   Download

MIT

php font awesome fa

12/09 2017

0.3.1

0.3.1.0

A simple way to organise constant collections and constant=>string dictionaries.

  Sources   Download

MIT

php font awesome fa