incraigulous/objection

Wrap nested arrays in DTO objects and collections.

1.1.0 2023-04-20 22:11 UTC

This package is auto-updated.

Last update: 2024-11-21 01:54:17 UTC


README

Recursively wrap nested arrays in Laravel-style DTO objects and collections. Just pass in your array and BLAMO, you'll get a nested DTO objects and collections back.

Why?

I like accessing data in my views using a fluent object syntax instead of array syntax.

How to use it:
  $data = objection(
    [
        [
            'test' => 'asdf',
            'test2' => [
                'sub' => 'asdasdffasdf',
                'sub2' => 'teasdasdffasdfst',
            ],
        ],
        [
            'test' => 'asdasdffasdf',
            'test2' => 'teasdasdffasdfst',
        ]
    ]
  )
  
  echo $data->first()->test2->sub2; //teasdasdffasdfst
Convert it back to an array:
  $data->toArray();

What if I don't like using helper functions?
  return \Incraigulous\Objection\ObjectionFactory::make($array);

Installation

  composer require incraigulous/objection

Credits