A PHP language extension that provides an efficient alternative to Symfony's Normalizer.
This extension provides a Normalizer\ObjectNormalizer
class that can be used to normalize objects into arrays and denormalize arrays back into objects. The normalization process can be controlled using attributes.
To install the extension, you can use the following commands:
phpize
./configure
make
make install
You'll need to add extension=normalizer.so
to your primary php.ini file.
# To see where .ini files are located
php -i | grep "\.ini"
You can also enable the extension temporarily using the command line:
php -d extension=normalizer.so
Here is a basic example of how to use the ObjectNormalizer
:
<?php
use Normalizer\ObjectNormalizer;
use Normalizer\Expose;
use Normalizer\Groups;
use Normalizer\Ignore;
use Normalizer\MaxDepth;
use Normalizer\SerializedName;
class User
{
#[Expose]
#[SerializedName("user_id")]
public int $id;
#[Groups(["public"])]
public string $name;
#[Ignore]
public string $password;
#[Groups(["admin"])]
public string $email;
#[MaxDepth(1)]
public Profile $profile;
}
class Profile
{
#[Expose]
public string $site;
}
$user = new User();
$user->id = 1;
$user->name = 'John Doe';
$user->password = 'secret';
$user->email = '[email protected]';
$user->profile = new Profile();
$user->profile->site = 'example.com';
$normalizer = new ObjectNormalizer();
// Normalize the object
$data = $normalizer->normalize($user, [ObjectNormalizer::GROUPS => ['public']]);
// $data will be:
// [
// 'user_id' => 1,
// 'name' => 'John Doe',
// 'profile' => [],
// ]
// Denormalize the array back to an object
$newUser = $normalizer->denormalize($data, User::class);
The normalization process can be controlled with the following attributes:
By default, all properties are ignored. The #[Expose]
attribute marks a property to be included in the normalization.
The #[Ignore]
attribute marks a property to be excluded from the normalization.
The #[Groups]
attribute allows you to conditionally include properties based on normalization groups. A property with a #[Groups]
attribute will only be included if the group is passed to the normalize
method.
class MyObj
{
#[Groups(['group1', 'group2'])]
public string $foo;
#[Groups(['group4'])]
public string $anotherProperty;
#[Groups(['group3'])]
public function getBar() // is* and has* methods are also supported
{
return $this->bar;
}
}
$normalizer = new Normalizer\ObjectNormalizer();
$obj = new MyObj();
$obj->foo = 'foo';
$obj->anotherProperty = 'property';
$array = $normalizer->normalize($obj, [Normalizer\ObjectNormalizer::GROUPS => ['group1', 'group4']]);
// Will return ['foo' => 'foo', 'anotherProperty' => 'property]
The #[MaxDepth]
attribute prevents the normalizer from going too deep into the object graph. When the max depth is reached, the normalizer will return an empty array.
The #[SerializedName]
attribute allows you to specify a different name for a property in the normalized output.
Contributions are welcome! Please feel free to submit a pull request.
$ phpize
$ ./configure
$ make
$ make install
make
make test
The MIT License (MIT). Please see LICENSE for more information.