PHPackages                             blabs-dev/php-dictionary - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. blabs-dev/php-dictionary

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

blabs-dev/php-dictionary
========================

A simple abstract class that let you write quick dictionaries using constants

v2.0.0(4y ago)0158MITPHPPHP ^7.3|^8.0

Since Sep 29Pushed 4y ago2 watchersCompare

[ Source](https://github.com/blabs-dev/php-dictionary)[ Packagist](https://packagist.org/packages/blabs-dev/php-dictionary)[ RSS](/packages/blabs-dev-php-dictionary/feed)WikiDiscussions main Synced today

READMEChangelog (4)Dependencies (1)Versions (5)Used By (0)

Quick Dictionary implementation for your classes
================================================

[](#quick-dictionary-implementation-for-your-classes)

A simple but powerful package to manage quick "dictionaries" of primitive values with php.

The concept of "dictionary" refers to a simple list of valid values for a specific domain. The package let you define dictionaries using php constants and quickly access to their values outside the class context.

It not aims to be an implementation of an **enumerable class**, it just let you being able to quick get all constants values of your class as an array, or checking if a value is in the dictionary and thus can be considered as "valid".

Installation
------------

[](#installation)

```
composer require blabs-dev/dictionary

```

Usage
-----

[](#usage)

The package let you choose if you prefer to extend an abstract class or use a trait to implement dictionary features in your class.

The first approach is useful for single responsibility classes such as a simple class for a dictionary of strings, while the latter can be convenient if your class already has inheritance with other dependencies and you simply want to being able to check vailidity of a value against a list of the class constants.

### Sample usage with inheritance

[](#sample-usage-with-inheritance)

Here's an example of a dictionary of fruits that extends dictionary abstract.

```
// create a class extending the Dictionary abstract

use Blabs\Dictionary\Dictionary;

class Fruits extends Dictionary
{
    const APPLE = 'apple';
    const BANANA = 'banana';
    const ORANGE = 'orange';
}

// return all "values" from the dictionary

Fruits::values() // outputs [ 'apple', 'banana', 'orange' ]

// check if a value is valid

Fruits::isValid('apple') // outputs `true`

Fruits::isValid('tomato') // outputs `false`
```

### Sample usage with composition

[](#sample-usage-with-composition)

Here's an example of a hypothetical vegetables warehouse that already extends another class called `StockService` to check product's availability, in this case you can include `WithDictionary` trait to add dictionary features

```
use Blabs\Dictionary\WithDictionary;

class MyVegetablesWarehouse extends StockService
{
    use WithDictionary;  // include the trait in your class to use dictionary features

    // Defines all products handled by the warehouse
    const AUBERGINE = 'aubergine';
    const SWEET_PEPPER = 'sweet pepper';
    const TOMATO = 'tomato';

    // Checks if a product is a "legit" vegetable :)
    public function isVegetable($product)
    {
        // recalls dictionary trait method to check if the dictionary has specified product
        return self::isValid($product);
    }

    // Lists all available vegetables
    public function getVegetables()
    {
        // recalls dictionary static method to list all vegetable products
        return self::values();
    }

    // Get stocks for a specific vegetable
    public function getVegetablesStocks($vegetable)
    {
        // uses dictionary static method to check if the product is "valid" before doing anything else
        if (! self::isValid($vegetable))
            throw new InvalidArgumentException('this is not a veggie!')

        // then recalls a hypothetical method from elsewhere (i.e. the StockService class)
        return $this->getStocks($vegetable);
    }

    // Creates an "inventory" array
    public function getVegetablesInventory()
    {
        $stocks = [];

        foreach (self::values() as $vegetable)  // cycle all vegetables in the dictionary
        {
            $stocks[$vegetable] = $this->getStocks($vegetable);
        }

        return $stocks;
    }
}

// the class will still expose static methods outside its context

MyVegetablesWarehouse::values() // outputs [ 'aubergine', 'sweet pepper', 'tomato' ]

// check if a value is valid

MyVegetablesWarehouse::isValid('apple') // outputs `false`

MyVegetablesWarehouse::isValid('tomato') // outputs `true`
```

TODO
----

[](#todo)

- Filtering
- Sorting
- Mapping

Disclaimer
----------

[](#disclaimer)

**PLEASE NOTE** This class will probably become useless when php 8.1 will be released within new `enums` API. If you need a complete implementation of an enumerable class, you can rely on more robust and supported packages, like [myclabs/php-enum](https://github.com/myclabs/php-enum).

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 72.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

4

Last Release

1738d ago

Major Versions

v1.0.x-dev → v2.0.02021-09-29

PHP version history (3 changes)1.0.0PHP ^5.6|^7.1|8.0

1.1.0PHP ^5.6|^7.1|^8.0

v2.0.0PHP ^7.3|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/75724335?v=4)[blabs-dev](/maintainers/blabs-dev)[@blabs-dev](https://github.com/blabs-dev)

---

Top Contributors

[![salvobee](https://avatars.githubusercontent.com/u/19645064?v=4)](https://github.com/salvobee "salvobee (8 commits)")[![blabs-dev](https://avatars.githubusercontent.com/u/75724335?v=4)](https://github.com/blabs-dev "blabs-dev (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/blabs-dev-php-dictionary/health.svg)

```
[![Health](https://phpackages.com/badges/blabs-dev-php-dictionary/health.svg)](https://phpackages.com/packages/blabs-dev-php-dictionary)
```

###  Alternatives

[brick/date-time

Date and time library

3663.8M108](/packages/brick-date-time)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
