Skip to content

Make configuration sections readable as objects #12

Description

@nathan-fiscaletti

It would be cool if you could deserialize configuration sections into php objects.

An example of the desired code:

$configuration->get('foo.bar.favoriteperson')->as(Person::class)->eatSomething();

The configuration section:

'favoriteperson' => [
    'name' => 'Nathan',
    'age' => 21,
]

The person class:

class Person {
    public $name;
    public $age;

    public function eatSomething()
    {
        ...
    }
}

The ->as() function on the configuration object would take a class definition, and then serialize the section passed in ->get back into the class passed.

I'm not sure what method you would go about using to do this, possibly something along the lines of

Parse section array into JSON
Use JSON to deserialize it into a PHP Object
Cast that object to the class passed

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions