AthenaeumAthenaeum
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
  • Version 3.x

    • How to install
  • Config

    • Configuration Loader
  • Container

    • IoC Service Container
  • Dto

    • Data Transfer Object (DTO)
    • Create Interface
    • Implement DTO
    • Property overloading
    • Populate
    • Json
    • Nested DTOs
    • Array DTO
  • Http

    • Introduction
    • Http Clients
  • Properties

    • Properties Overload
  • Support

    • Support Introduction
    • Laravel Aware Of Helpers
    • Aware Of Properties
    • Aware-Of Component Generator
  • Testing

    • Testing Introduction
    • Laravel
    • Test Cases
    • Traits
  • Utils

    • Populate
    • Json
You are viewing documentation for an outdated version. It is no longer supported!

Populate

You can populate your DTO using an array.

// property-name => value array
$data = [
    'name' => 'Timmy Jones',
    'age'  => 32
];

// Create instance and invoke populate
$person = new Person();
$person->populate($data); // setName() and setAge() are invoked with the given values

Via __construct

If you are extending the default DTO abstraction, then you can also pass in an array in the constructor.

// property-name => value array
$data = [
    'name' => 'Carmen Rock',
    'age'  => 25
];

// Create instance and invoke populate
$person = new Person($data);

Export to array

Each DTO can be exported to an array.

$properties = $person->toArray();

var_dump($properties);  // Will output a "property-name => value" list
                        // Example:
                        //  [
                        //      'name'  => 'Timmy'
                        //      'age'   => 16
                        //  ]
Edit page
Last Updated: 08/02/2020, 09:56
Contributors: Alin Eugen Deac
Prev
Property overloading
Next
Json