Skip to content

wubinworks/magic-object

Repository files navigation

Object with Magic Getter and Setter

The MagicObject class is an excellent choice for manipulating structured data. For example, configuration data represented by JSON, XML, YML, etc.

The MagicObject can also be used for representing a database entry and the class extending it is often called Model.

$obj->setSomething(123);
$obj->getSomething(); // 123

The Magic Getter and Setter can make your code extremely simple and straightforward. For detailed usage, see the full example section.

Requirements

  • php >= 5.3

Installation

composer require wubinworks/magic-object

Usage

Data path naming

The MagicObject stores nested array data and those nested array keys can be represented by "path".

The key's separator is /. The best practice to name a path is to use lowercase letters, digits and underscores for keys only and the keys should begin with a lowercase letter.

For instance, this/is/a/path. The keys are this, is, a and path.

/** @var \Wubinworks\MagicObject\Data\MagicObject $obj */
$obj->setData('this/is/a/path', 'some value');
$obj->getData('this/is/a/path'); // some value

Path and method name conversion

Here is the Magic. The path can be converted to Getter and Setter method name.

/** @var \Wubinworks\MagicObject\Data\MagicObject $obj */
$obj->getData(<snake_case path>)
$obj->get<PascalCase partial method name>()

$obj->setData(<snake_case path>, $value)
$obj->set<PascalCase partial method name>($value)

$obj->hasData(<snake_case path>)
$obj->has<PascalCase partial method name>()

$obj->unsData(<snake_case path>)
$obj->uns<PascalCase partial method name>()

The above example demonstrates how snake_case path is converted to PascalCase partial method name, and vice versa.

Note the key's separator / is converted to _, and vice versa.

For instance, customer/reward_points/expired <==> Customer_RewardPoints_Expired.

You may still be confused, so see the full example below.

Full example

$data = json_decode('{
    "customer": {
        "name": "John Smith",
        "gender": null,
        "age": 25,
        "reward_points": {
            "used": 1000,
            "expired": 2000
        }
    },
    "order_number": 111111111
}', true);
/*
$data = [
    'customer' => [
        'name' => 'John Smith',
        'gender' => null,
        'age' => 25,
        'reward_points' => [
            'used' => 1000,
            'expired' => 2000
        ];
    ],
    'order_number' => 11111111
];
*/

// Feed the object with data. Array with numeric keys are *NOT* supported.
$obj = new \Wubinworks\MagicObject\Data\MagicObject($data);

// Getter

$obj->getData('order_number'); // 11111111
$obj->getOrderNumber(); // 11111111

$obj->getData('customer/reward_points/used'); // 1000
$obj->getCustomer_RewardPoints_Used(); // 1000

// If the path does not exist, null will be returned and no exception will be thrown.
$obj->getData('customer/nonexist_key'); // null
$obj->getCustomer_NonexistKey(); // null

// Get all data
$obj->getData(/** No path parameter */); // $data

// Setter

$obj->setData('order_number', 22222222);
$obj->getData('order_number'); // 22222222
$obj->setOrderNumber(33333333);
$obj->getData('order_number'); // 33333333

$obj->setData('customer/reward_points/used', 7777);
$obj->getData('customer/reward_points/used'); // 7777
$obj->setCustomer_RewardPoints_Used(8888);
$obj->getData('customer/reward_points/used'); // 8888
$obj->setData('customer/new_key', 9999);
$obj->getData('customer/new_key'); // 9999

// Replace all data
// Actually, it is Setter. Note the path parameter of setData is null.

$arr = ['a' => 1, 'b' => 2];
$obj->setData(null, $arr);
$obj->getData(); // $arr

// Has path

$obj->setData(null, $data);
$obj->hasData('customer/gender'); // true
$obj->hasCustomer_Gender(); // true
$obj->hasData('customer/nonexist_key'); // false

// Unset path ("uns" means "unset")
// Always succeeds, even if the path does not exist.
// Note the path parameter of unsData cannot be null and must be string

$obj->unsData('customer/gender');
$obj->hasData('customer/gender'); // false
$obj->unsCustomer_Age();
$obj->hasData('customer/age'); // false

$obj->unsData('customer/nonexist_key');
$obj->unsCustomer_NonexistKey();

Unit testing

Install the require-dev dependencies and run the following command.

vendor/bin/phpunit

If you like this package or this package helped you, please share and give a ★☆star☆★, it's NOT hard!

About

Object with Magic Getter and Setter.

Topics

Resources

License

OSL-3.0, Unknown licenses found

Licenses found

OSL-3.0
LICENSE.txt
Unknown
COPYING.txt

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages