2017 © Pedro Peláez
 

library static-entity

Provide some kind of static entities

image

byscripts/static-entity

Provide some kind of static entities

  • Tuesday, February 2, 2016
  • by Byscripts
  • Repository
  • 1 Watchers
  • 2 Stars
  • 367 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 0 % Grown

The README.md

Byscripts Static Entity

Provides an easy way to get some entity/model behavior with static data, (*1)

Build Status Latest Stable Version License Scrutinizer Code Quality Code Coverage Codeship Status for ByScripts/static-entity, (*2)

Installation

Add the package in your composer.json

At command line, run composer require byscripts/static-entity:~3.0, (*3)

Usage

Create your static entity

<?php
use Byscripts\StaticEntity\AbstractStaticEntity;

class WebBrowser extends AbstractStaticEntity
{
    const CHROMIUM = 1;
    const FIREFOX  = 2;
    const IE       = 3;
    const OPERA    = 4;
    const SAFARI   = 5;

    private $name;
    private $brand;
    private $engine;
    private $license;

    public function getName()
    {
        return $this->name;
    }

    public function getBrand()
    {
        return $this->brand;
    }

    public function getEngine()
    {
        return $this->engine;
    }

    public function getLicense()
    {
        return $this->license;
    }

    static public function getDataSet(): array
    {
        return [
            self::CHROMIUM => [
                'name'    => 'Chromium',
                'brand'   => 'Google',
                'engine'  => 'Blink',
                'license' => 'BSD'
            ],
            self::FIREFOX => [
                'name'    => 'Firefox',
                'brand'   => 'Mozilla',
                'engine'  => 'Gecko',
                'license' => 'MPL'
            ],
            self::IE => [
                'name'    => 'Internet Explorer',
                'brand'   => 'Microsoft',
                'engine'  => 'Trident',
                'license' => 'Proprietary'
            ],
            self::OPERA => [
                'name'    => 'Opera',
                'brand'   => 'Opera Software',
                'engine'  => 'Blink',
                'license' => 'Proprietary'
            ],
            self::SAFARI => [
                'name'    => 'Safari',
                'brand'   => 'Apple',
                'engine'  => 'WebKit',
                'license' => 'Proprietary'
            ]
        ];
    }
}

Play with it

<?php

// Get an instance of WebBrowser, hydrated with Firefox data
$firefox = WebBrowser::get(WebBrowser::FIREFOX);

// Instanciated objects are singleton
WebBrowser::get(WebBrowser::FIREFOX) === WebBrowser::get(WebBrowser::FIREFOX); // true

// The getId() method is always available.
// It returns the key used in the getDataSet() method;
$firefox->getId(); // 2

// Other methods are ones implemented in the static entity
$firefox->getName(); // Firefox

// The toId() method transform an entity to ID.
// If an id is passed, it is returned as is, after checking it exists.
// The method is mainly intended for a setter method to accept both type.
WebBrowser::toId($firefox); // 2
WebBrowser::toId(2);        // 2

// The getIds() method returns an array of all ids present in data set
WebBrowser::getIds(); // [1, 2, 3, 4, 5]

// The getAssoc() returns an associative array with `id` as key and `name` as value
WebBrowser::getAssociative(); // [1 => 'Chromium', 2 => 'Firefox', ...]

// You can also pass the name of an argument you want to use as value
WebBrowser::getAssociative('brand'); // [1 => 'Google', 2 => 'Mozilla', 3 => 'Microsoft', ...]

// The getAll() method returns an array containing all instances of entities
WebBrowser::getAll(); // [Object, Object, ...]

// The exists() method check whether the passed ID exists in data set
WebBrowser::hasId(3); // true
WebBrowser::hasId(9); // false

Alternative usage

You can also use the Provider class., (*4)

In this case, your entity is not required to extends AbstractStaticEntity, but still needs to implements StaticEntityInterface, (*5)

<?php
use Byscripts\StaticEntity\Provider;

Provider::get(WebBrowser::class, WebBrowser::FIREFOX);
Provider::getAssociative(WebBrowser::class);
Provider::getAssociative(WebBrowser::class, 'otherKey');
Provider::getIds(WebBrowser::class);
Provider::getAll(WebBrowser::class);
Provider::hasId(WebBrowser::class, WebBrowser::CHROMIUM);
Provider::toId(WebBrowser::class, $instanceOrId);

The Versions

02/02 2016

dev-master

9999999-dev

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

02/02 2016

2.1.3

2.1.3.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

23/10 2014

2.1.2

2.1.2.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

21/07 2014

2.1.1

2.1.1.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

23/06 2014

2.1.0

2.1.0.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

05/06 2014

2.0.3

2.0.3.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

04/05 2014

2.0.2

2.0.2.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

04/05 2014

2.0.1

2.0.1.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

04/05 2014

2.0.0

2.0.0.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

01/05 2014

1.0.0

1.0.0.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

30/04 2014

0.1.4

0.1.4.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

30/04 2014

0.1.3

0.1.3.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

30/04 2014

0.1.2

0.1.2.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

22/04 2014

0.1.1

0.1.1.0

Provide some kind of static entities

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Thierry Goettelmann

18/04 2014

0.1.0

0.1.0.0

Provide some kind of static entities

  Sources   Download

MIT

by Thierry Goettelmann