note 127899 added to language.attributes.overview
| From: | FlorianKrmer at main dot php dot net | Date: | Sat, 05 Nov 2022 21:57:58 +0000 |
| Subject: | note 127899 added to language.attributes.overview | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219852@lists.php.net to get a copy of this message | ||
I've tried Harshdeeps example and it didn't run out of the box and I think it is not
complete, so I wrote a complete and working naive example regarding attribute based serialization.
<?php
declare(strict_types=1);
#[Attribute(Attribute::TARGET_CLASS_CONSTANT|Attribute::TARGET_PROPERTY)]
class JsonSerialize
{
public function __construct(public ?string $fieldName = null) {}
}
class VersionedObject
{
#[JsonSerialize]
public const version = '0.0.1';
}
class UserLandClass extends VersionedObject
{
protected string $notSerialized = 'nope';
#[JsonSerialize('foobar')]
public string $myValue = '';
#[JsonSerialize('companyName')]
public string $company = '';
#[JsonSerialize('userLandClass')]
protected ?UserLandClass $test;
public function __construct(?UserLandClass $userLandClass = null)
{
$this->test = $userLandClass;
}
}
class AttributeBasedJsonSerializer {
protected const ATTRIBUTE_NAME = 'JsonSerialize';
public function serialize($object)
{
$data = $this->extract($object);
return json_encode($data, JSON_THROW_ON_ERROR);
}
protected function reflectProperties(array $data, ReflectionClass $reflectionClass, object
$object)
{
$reflectionProperties = $reflectionClass->getProperties();
foreach ($reflectionProperties as $reflectionProperty) {
$attributes = $reflectionProperty->getAttributes(static::ATTRIBUTE_NAME);
foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
$name = $instance->fieldName ?? $reflectionProperty->getName();
$value = $reflectionProperty->getValue($object);
if (is_object($value)) {
$value = $this->extract($value);
}
$data[$name] = $value;
}
}
return $data;
}
protected function reflectConstants(array $data, ReflectionClass $reflectionClass)
{
$reflectionConstants = $reflectionClass->getReflectionConstants();
foreach ($reflectionConstants as $reflectionConstant) {
$attributes = $reflectionConstant->getAttributes(static::ATTRIBUTE_NAME);
foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
$name = $instance->fieldName ?? $reflectionConstant->getName();
$value = $reflectionConstant->getValue();
if (is_object($value)) {
$value = $this->extract($value);
}
$data[$name] = $value;
}
}
return $data;
}
protected function extract(object $object)
{
$data = [];
$reflectionClass = new ReflectionClass($object);
$data = $this->reflectProperties($data, $reflectionClass, $object);
$data = $this->reflectConstants($data, $reflectionClass);
return $data;
}
}
$userLandClass = new UserLandClass();
$userLandClass->company = 'some company name';
$userLandClass->myValue = 'my value';
$userLandClass2 = new UserLandClass($userLandClass);
$userLandClass2->company = 'second';
$userLandClass2->myValue = 'my second value';
$serializer = new AttributeBasedJsonSerializer();
$json = $serializer->serialize($userLandClass2);
var_dump(json_decode($json, true));
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.161 (proxied: 2a03:7500:8f4:9d00:e9b9:ac00:5ebf:5191)
----
Manual Page -- https://php.net/manual/en/language.attributes.overview.php
Edit -- https://main.php.net/note/edit/127899
Del: integrated -- https://main.php.net/note/delete/127899/integrated
Del: useless -- https://main.php.net/note/delete/127899/useless
Del: bad code -- https://main.php.net/note/delete/127899/bad+code
Del: spam -- https://main.php.net/note/delete/127899/spam
Del: non-english -- https://main.php.net/note/delete/127899/non-english
Del: in docs -- https://main.php.net/note/delete/127899/in+docs
Del: other reasons-- https://main.php.net/note/delete/127899
Reject -- https://main.php.net/note/reject/127899
Search -- https://main.php.net/manage/user-notes.php