1. Go to this page and download the library: Download vuryss/serializer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$pst6cache = new CacheItemPool(); // Some PSR-6 cache implementation
$serializer = new Serializer(
metadataExtractor: new CachedMetadataExtractor(
new MetadataExtractor(),
$pst6cache,
),
);
class SomeClass
{
#[SerializerContext(name: 'changedPropertyName')]
public string $someProperty;
}
use Vuryss\Serializer\Context;
class SomeClass
{
#[SerializerContext(groups: ['group1'])]
public string $property1;
// Has implicit group 'default'
public string $property2;
}
$serializer = new Serializer();
$object = new SomeClass();
$object->property1 = 'value1';
$object->property2 = 'value2';
$serializer->serialize($object, context: [Context::GROUPS => ['group1']]); // {"property1":"value1"}
use Vuryss\Serializer\Context;
class SomeClass
{
#[SerializerContext(groups: ['group1'])]
public string $property1;
// Has implicit group 'default'
public string $property2;
}
$serializer = new Serializer();
$data = '{"property1":"value1","property2":"value2"}';
$object = $serializer->deserialize($data, SomeClass::class, context: [Context::GROUPS => ['group1']]);
isset($object->property1); // true
isset($object->property2); // false
use Vuryss\Serializer\Context;
class SomeClass
{
#[SerializerContext(context: [Context::DATETIME_FORMAT => 'Y-m-d'])]
public DateTime $someDate;
}
use Vuryss\Serializer\Context;
$serializer = new Serializer(
context: [
Context::DATETIME_FORMAT => \DateTimeInterface::RFC2822,
]
);
use Vuryss\Serializer\Context;
class SomeClass
{
#[SerializerContext(context: [
Context::DATETIME_FORMAT => 'Y-m-d',
Context::DATETIME_FORMAT_STRICT => true
])]
public DateTime $someDate;
}
use Vuryss\Serializer\Context;
$serializer = new Serializer(
context: [
Context::DATETIME_FORMAT => 'Y-m-d',
Context::DATETIME_FORMAT_STRICT => true
]
);
use Vuryss\Serializer\Context;
class SomeClass
{
#[SerializerContext(context: [Context::DATETIME_TARGET_TIMEZONE => 'UTC'])]
public DateTime $someDate;
}
use Vuryss\Serializer\Context;
$serializer = new Serializer(
context: [
Context::DATETIME_TARGET_TIMEZONE => 'UTC',
]
);
class SomeClass
{
#[SerializerContext(ignore: true)]
public string $someProperty;
}
use Vuryss\Serializer\Context;
class SomeClass
{
#[SerializerContext(context: [Context::SKIP_NULL_VALUES => true])]
public ?string $someProperty;
}
use Vuryss\Serializer\Context;
$serializer = new Serializer(
context: [
Context::SKIP_NULL_VALUES => true,
]
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.