2017 © Pedro Peláez
 

library class-meta

Add arbitrary metadata to classes and their constants by annotation

image

ashleydawson/class-meta

Add arbitrary metadata to classes and their constants by annotation

  • Tuesday, December 20, 2016
  • by AshleyDawson
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,459 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 44 % Grown

The README.md

Class Meta

Apply metadata to classes and their constants via annotation. Handy if you need to attach arbitrary data to lookups, enumerations, etc., (*1)

Build Status, (*2)

Installation

Install ClassMeta via Composer using the following command:, (*3)

$ composer require ashleydawson/class-meta

Basic Usage

Apply metadata annotation to classes and constants:, (*4)

<?php

namespace Acme\Enum;

use AshleyDawson\ClassMeta\Annotation\Meta;

/**
 * @Meta(data={"name"="Invoice Status Types"})
 */
class InvoiceStatus
{
    /**
     * @Meta(data={"name"="Draft", "description"="Invoice has not yet been sent to the customer"})
     */
    const DRAFT = 'draft';

    /**
     * @Meta(data={"name"="Sent", "description"="Invoice has been sent to the customer"})
     */
    const SENT = 'sent';

    /**
     * @Meta(data={"name"="Paid", "description"="Invoice has been paid by the customer"})
     */
    const PAID = 'paid';

    /**
     * @Meta(data={"name"="Void", "description"="Invoice is void and no longer billable"})
     */
    const VOID = 'void';
}

You can now access the metadata using the class meta manager:, (*5)

use AshleyDawson\ClassMeta\ClassMetaManager;
use AshleyDawson\ClassMeta\Annotation\Meta;

$manager = new ClassMetaManager();

$classMeta = $manager->getClassMeta('Acme\Enum\InvoiceStatus');

// "Invoice Status Types" will be echoed
echo $classMeta->data['name'];

Get constant(s) metadata:, (*6)

$constantsMeta = $manager->getClassConstantsMeta('Acme\Enum\InvoiceStatus');

// Echo all constant metadata
foreach ($constantsMeta as $meta) {
    echo $meta->data['name'] . PHP_EOL;
    echo $meta->data['description'] . PHP_EOL;
}

Get individual meta by value (i.e. the value of the constant):, (*7)

$meta = $manager->getClassConstantMetaByValue('Acme\Enum\InvoiceStatus', InvoiceStatus::PAID);

// "Paid" will be echoed
echo $meta->data['name'];

Map the collection of constant meta for use in a select drop-down menu, for example:, (*8)

$options = $manager->getMappedClassConstantsMeta('Acme\Enum\InvoiceStatus', function (Meta $meta, $i) {

    // Return items indexed by class constant value
    return [
        $meta->value, 
        $meta->data['name'],
    ];

    // OR

    // Return items indexed by an incremental integer, starting at zero
    return [
        $i, 
        $meta->data['name'],
    ];

});

echo '<select>';
foreach ($options as $value => $name) {
    echo "<option value=\"{$value}\">{$name}</option>";
}
echo '</select>';

Note: The optional argument $i passed to the map closure is the iteration number (starting at 0). Useful if you want to index the mapped collection instead of returning it as an associative array., (*9)

Grouped Metadata

Pass optional arbitrary groups to help organise your metadata:, (*10)

<?php

namespace Acme\Enum;

use AshleyDawson\ClassMeta\Annotation\Meta;

/**
 * @Meta(data={"name"="Invoice Status Types"})
 */
class InvoiceStatus
{
    /**
     * @Meta(data={"name"="Draft"}, groups={"admin"})
     */
    const DRAFT = 'draft';

    /**
     * @Meta(data={"name"="Sent"}, groups={"admin"})
     */
    const SENT = 'sent';

    /**
     * @Meta(data={"name"="Paid"})
     */
    const PAID = 'paid';

    /**
     * @Meta(data={"name"="Void"}, groups={"admin"})
     */
    const VOID = 'void';
}

You can now access groups of metadata like so:, (*11)

use AshleyDawson\ClassMeta\ClassMetaManager;

$manager = new ClassMetaManager();

$constantsMeta = $manager->getClassConstantsMeta('Acme\Enum\InvoiceStatus', ['admin']);

// Echo only constant metadata in "admin" group
foreach ($constantsMeta as $meta) {
    echo $meta->data['name'] . PHP_EOL;
}

$constantsMeta = $manager->getClassConstantsMeta('Acme\Enum\InvoiceStatus', ['Default']);

// Echo only constant metadata in "Default" group (i.e. `const PAID = 'paid'` metadata)
foreach ($constantsMeta as $meta) {
    echo $meta->data['name'] . PHP_EOL;
}

$constantsMeta = $manager->getClassConstantsMeta('Acme\Enum\InvoiceStatus', ['Default', 'admin']);

// Echo all constant metadata
foreach ($constantsMeta as $meta) {
    echo $meta->data['name'] . PHP_EOL;
}

Note: The "Default" group will contain metadata that is not assigned a group, (*12)

If you need to get meta for all constants, even if they have groups assigned, use the special _all group name, like so:, (*13)

$constantsMeta = $manager->getClassConstantsMeta('Acme\Enum\InvoiceStatus', ['_all']);

Cache

All metadata can be cached by simply passing a valid Doctrine cache provider to the class meta manager:, (*14)

use Doctrine\Common\Cache\FilesystemCache;

$manager = new ClassMetaManager();
$manager->setCache(new FilesystemCache('/path/to/cache/dir'));

Cache is invalidated using the class file modify time, but you can also pass an optional TTL in seconds to the ClassMetaManager#setCache() method:, (*15)

use Doctrine\Common\Cache\FilesystemCache;

$manager = new ClassMetaManager();
$manager->setCache(new FilesystemCache('/path/to/cache/dir'), 300); // Cache stale after 5 minutes

Tests

To run the ClassMeta test suite, install Composer dev dependencies and run:, (*16)

$ bin/phpunit

The Versions

20/12 2016

dev-master

9999999-dev

Add arbitrary metadata to classes and their constants by annotation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ashley Dawson

class metadata meta annotation constant

01/11 2016

1.0.3

1.0.3.0

Add arbitrary metadata to classes and their constants by annotation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ashley Dawson

class metadata meta annotation constant

13/10 2016

1.0.2

1.0.2.0

Add arbitrary metadata to classes and their constants by annotation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ashley Dawson

class metadata meta annotation constant

19/05 2016

1.0.1

1.0.1.0

Add arbitrary metadata to classes and their constants by annotation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ashley Dawson

class metadata meta annotation constant

18/05 2016

1.0.0

1.0.0.0

Add arbitrary metadata to classes and their constants by annotation

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ashley Dawson

class metadata meta annotation constant