Serializes any object into a primitive type

v0.1.0 2025-06-16 07:56 UTC

This package is auto-updated.

Last update: 2025-06-16 07:58:55 UTC


README

Latest Version Software License

Attributes Options is a collection of Attributes that can be used to enhance the validation and/or serialization stages.

Supported options

Class specific options

  • AliasGenerator, generates an alias per each class property

Property options

  • Alias, specifies a given alias for a property
  • Ignore, skips the validation and/or serialization of a property

Requirements

  • PHP 8.1+

We aim to support versions that haven't reached their end-of-life.

How it works?

<?php

use Attributes\Validation\Validator;
use Attributes\Options\AliasGenerator;
use Attributes\Options\Alias;
use Attributes\Options\Ignore;
use Attributes\Serializer\ReflectionSerializer;

#[AliasGenerator('snake')]
class Login
{
    public string $userName;

    #[Alias('password'), Ignore(validation: false)]
    public string $myPassword;
}

$rawData = [
    'user_name' => 'user',
    'password'  => '1234',
];

// Validation stage
$validator = new Validator();
$login = $validator->validate($rawData, new Login);

var_dump($login->user);     // string(4) "user
var_dump($login->password); // string(4) "1234"

// Serialization stage
$serializer = new Serializer;
$serializedData = $serializer->serialize($login);

var_dump($serializedData);          // array { ["user_name"] => "user" }

Installation

composer require attributes-php/options

Attributes Options was created by André Gil and is open-sourced software licensed under the MIT license.