A PHP project for working with flexible data objects, supporting dynamic properties and array initialization.
- Dynamic Properties: Easily set and get properties on data objects.
- Array Initialization: Instantiate objects with an associative array of properties.
- Extensible: Create your own data object classes by extending the base
DataObject
.
composer require chriha/data-objects
use Chriha\DataObject;
use Chriha\DataObjects\Attributes\MapFrom;
use Chriha\DataObjects\Attributes\Rules;
use Illuminate\Support\Collection;
class Person extends DataObject
{
#[MapFrom('name_attr')]
public string $name;
#[Rules(['required', 'integer'])]
public int $age;
#[CollectionOf(Address::class)]
public Collection $addresses;
#[TransformWith(UppercaseTransformer::class)]
public string $country;
}
$person = Person::from(['name_attr' => 'Alice', 'age' => 30]);
echo $person->name; // Alice
echo $person->age; // 30
This project uses PestPHP for testing.
To run the tests:
composer install
./vendor/bin/pest
src/
- Source code for data objectstests/
- PestPHP test suite
- PHP 8.3+
- Composer
MIT