PHPackages                             gong023/turmeric-spice - 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. gong023/turmeric-spice

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

gong023/turmeric-spice
======================

0.2.9(2y ago)18138.1k↑273.3%31PHPPHP &gt;=5.4.0

Since Oct 11Pushed 2y ago3 watchersCompare

[ Source](https://github.com/gong023/turmeric-spice)[ Packagist](https://packagist.org/packages/gong023/turmeric-spice)[ RSS](/packages/gong023-turmeric-spice/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (5)Versions (13)Used By (1)

turmeric-spice [![Build Status](https://camo.githubusercontent.com/1140f61bb4815cba5ec8d251614672068ddbca5c2334a48e7db2c67c8b9946c9/68747470733a2f2f7472617669732d63692e6f72672f676f6e673032332f7475726d657269632d73706963652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/gong023/turmeric-spice) [![Coverage Status](https://camo.githubusercontent.com/155fb24d143a338ea517bb2713c2fe800c9c917a242ec34277b8ed6f8fa50f1c/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f676f6e673032332f7475726d657269632d73706963652f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/gong023/turmeric-spice?branch=master) [![Latest Stable Version](https://camo.githubusercontent.com/f719bd85213e4a9e1c43bc3e8bbafed57223108c51adf0f578d857e926bb0db9/68747470733a2f2f706f7365722e707567782e6f72672f676f6e673032332f7475726d657269632d73706963652f762f737461626c65)](https://packagist.org/packages/gong023/turmeric-spice)
=================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#turmeric-spice---)

TurmericSpice is a library inspired by [MagicSpice](https://github.com/gree/MagicSpice).

Give class syntax sugar for getter/setter.

Installation
============

[](#installation)

via composer

```
composer require gong023/turmeric-spice

```

Basic Usage
===========

[](#basic-usage)

Definition

```
use TurmericSpice\ReadableAttributes;

class User
{
    use ReadableAttributes {
        mayHaveAsInt     as public getId;
        mayHaveAsString  as public getName;
        mayHaveAsFloat   as public getBalance;
        mayHaveAsBoolean as public getRestricted;
        mayHaveAsArray   as public getFriendIds;
    }

    public function getCreated()
    {
        return $this->attributes->mayHave('created')->asInstanceOf('\\Datetime');
    }
}
```

Instantiate and usage

```
$user = new User([
    'id'         => 1,
    'name'       => 'Taro',
    'balance'    => 100.0,
    'restricted' => false,
    'friend_ids' => [2, 3, 4],
    'created'    => '2015-01-01 00:00:00',
]);

$user->getId();        // 1
$user->getName();      // 'Taro'
$user->getBalance();   // 100.0
$user->getRestricted;  // false
$user->getFriendIds(); // [2, 3, 4]
$user->getCreated();   // new \Datetime('2015-01-01 00:00:00') casted automatically.
$user->toArray();      // return array which contains above values.
```

If you want to use setter, Use `TurmericSpice\ReadWriteAttributes` trait.

```
use TurmericSpice\ReadWriteAttributes;

class User
{
    use ReadWriteAttributes {
        setValue         as public setId;
        setValue         as public setName;
        mayHaveAsInt     as public getId;
        mustHaveAsString as public getName;
    }
}

$user = new User(['id' => null]);
$user->setId(1);
$user->getId(); // 1
$user->setName('Taro');
$user->getName(); // 'Taro'
```

may or must
===========

[](#may-or-must)

TurmericSpice has two DSL, `may` and `must`.

If nullable value is given at constructor, `may` casts specified type and `must` throws `\TurmericSpice\Container\InvalidAttributeException`.

```
use TurmericSpice\ReadableAttributes;

class User
{
    use ReadableAttributes {
        mayHaveAsString   as public getName;
        mayHaveAsFloat    as public getBalance;
        mustHaveAsInt     as public getId;
        mustHaveAsArray   as public getFriendIds;
        mustHaveAsBoolean as public getRestricted;
    }
}

$user = new User([
    'name'       => null,
    'balance'    => 100,
    'id'         => null,
    'restricted' => 'false',
]);

$user->getName();       // return ''. casted automatically.
$user->getBalance();    // return 100.0. casted automatically.
$user->getId();         // If you give null(able),  you will get InvalidAttributeException.
$user->getFriendIds();  // If key is not defined, you will get InvalidAttributeException.
$user->getRestricted(); // return false. casted automatically.
```

Advanced Usage
==============

[](#advanced-usage)

### Get raw value

[](#get-raw-value)

```
use TurmericSpice\ReadableAttributes;

class User
{
    use ReadableAttributes;

    public function getIdRaw()
    {
        return $this->attributes->mayHave('id')->value();
    }

    public function getRawArray()
    {
        return $this->attributes->getRaw();
    }
}

$user = new User(['id' => '1']);
$user->getIdRaw();    // return '1'.
$user->getRawArray(); // return ['id' => '1']
```

### Get specified type array

[](#get-specified-type-array)

You can express `int[]`, `float[]`, `bool[]`, and `YourClass[]`.

```
use TurmericSpice\ReadableAttributes;

class User
{
    use ReadableAttributes {
        mayHaveAsIntArray    as public getFriendIds;
        mayHaveStringArray   as public getFriendNames;
        mustHaveAsFloatArray as public getBalanceHistories;
    }

    public function getUpdatedHistories()
    {
        return $this->attributes->mayHave('updated_histories')->asInstanceArray('\\Datetime');
    }
}

$user = new User([
    'friend_ids'        => ['1', '2', '3'],
    'friend_names'      => ['name1', 'name2', null],
    'balance_histories' => [10.0, 20.0, null],
    'updated_histories' => ['2015-01-01 00:00:00', '2016-01-01 00:00:00'],
]);

$user->getFriendIds;          // return [1, 2, 3]
$user->getFriendNames;        // return ['name1', 'name2', '']
$user->balanceHistories();    // throw exception.
$user->getUpdatedHistories(); // return [new Datetime('2015-01-01 00:00:00'), new Datetime('2016-01-01 00:00:00')]
```

### Validation

[](#validation)

```
use TurmericSpice\ReadableAttributes;

class User
{
    use ReadableAttributes;

    public function getOneOrZero()
    {
        return $this->attributes->mayHave('id')->asInteger(function ($value) {
            return $value === 1;
        });
    }

    public function getOne()
    {
        return $this->attributes->mustHave('id')->asInteger(function ($value) {
            return $value === 1;
        });
    }
}

$user = new User(['id' => 2]);
$user->getOneOrZero(); // return 0.
$user->getOne();       // throws exception.
```

### More example

[](#more-example)

If you want to know more, [see Example and test cases](https://github.com/gong023/turmeric-spice/tree/master/tests/Example).

IDE friendly
============

[](#ide-friendly)

As with MagicSpice, TurmericSpice is also IDE friendly.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.2% 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 ~261 days

Recently: every ~636 days

Total

12

Last Release

1041d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/034b987bca6692b17b6a5923eca68302fec17cd13f09f8d2f0a45c6d38125c03?d=identicon)[gong023](/maintainers/gong023)

---

Top Contributors

[![gong023](https://avatars.githubusercontent.com/u/1222856?v=4)](https://github.com/gong023 "gong023 (41 commits)")[![suzuki](https://avatars.githubusercontent.com/u/10488?v=4)](https://github.com/suzuki "suzuki (2 commits)")[![tisogawa](https://avatars.githubusercontent.com/u/145603?v=4)](https://github.com/tisogawa "tisogawa (2 commits)")[![ATOM594](https://avatars.githubusercontent.com/u/26355769?v=4)](https://github.com/ATOM594 "ATOM594 (1 commits)")[![yuchimiri](https://avatars.githubusercontent.com/u/117805761?v=4)](https://github.com/yuchimiri "yuchimiri (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/gong023-turmeric-spice/health.svg)

```
[![Health](https://phpackages.com/badges/gong023-turmeric-spice/health.svg)](https://phpackages.com/packages/gong023-turmeric-spice)
```

###  Alternatives

[sonnenglas/amazon-fba-fulfillment-center-list

List of Amazon fulfillment centers around the world available as JSON and PHP array

1910.4k](/packages/sonnenglas-amazon-fba-fulfillment-center-list)

PHPackages © 2026

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