Skip to content

Instantly share code, notes, and snippets.

@frv-dev
Last active December 2, 2020 02:32
Show Gist options
  • Save frv-dev/52612b2eff281aefe8bf6c5e4e45cc0d to your computer and use it in GitHub Desktop.
Save frv-dev/52612b2eff281aefe8bf6c5e4e45cc0d to your computer and use it in GitHub Desktop.
PHP 8 - Attributes Test
<?php
#[Attribute]
class Letter
{
public const UPPER_CASE = 'UPPER_CASE';
public const LOWER_CASE = 'LOWER_CASE';
public function __construct(
public string $modifier
) {}
}
function letterAnnotationProcessor(object $objectToModify): void
{
$reflectionClass = new reflectionClass($objectToModify::class);
$properties = $reflectionClass->getProperties();
foreach ($properties as $property) {
$propertyName = $property->getName();
$attributes = $property->getAttributes(Letter::class);
foreach ($attributes as $attribute) {
$modifier = $attribute->newInstance()->modifier;
$value = $objectToModify->$propertyName;
$newValue = match ($modifier) {
Letter::UPPER_CASE => strtoupper($value),
Letter::LOWER_CASE => strtolower($value),
default => $value,
};
$property->setValue($objectToModify, $newValue);
}
}
}
class Person
{
#[Letter(Letter::UPPER_CASE)]
public string $name;
}
$felipe = new Person();
$felipe->name = 'Felipe Renan Vieira';
var_dump($felipe);
letterAnnotationProcessor($felipe);
var_dump($felipe);
<?php
interface IAttribute
{
public static function action(object $object): void;
}
#[Attribute]
class Letter implements IAttribute
{
public const UPPER_CASE = 'UPPER_CASE';
public const LOWER_CASE = 'LOWER_CASE';
public function __construct(
public string $modifier
) {}
public static function action(object $object): void
{
$reflectionClass = new reflectionClass($object::class);
$properties = $reflectionClass->getProperties();
foreach ($properties as $property) {
$propertyName = $property->getName();
$attributes = $property->getAttributes(self::class);
foreach ($attributes as $attribute) {
$modifier = $attribute->newInstance()->modifier;
$value = $property->getValue($object);
$newValue = match ($modifier) {
self::UPPER_CASE => strtoupper($value),
self::LOWER_CASE => strtolower($value),
default => $value,
};
$property->setValue($object, $newValue);
}
}
}
}
function processAnnotations(array $items): void
{
foreach ($items as $item) {
$reflectionClass = new reflectionClass($item->object::class);
$properties = $reflectionClass->getProperties();
foreach ($properties as $property) {
if ($property->getName() !== $item->property) continue;
$attributes = $property->getAttributes();
foreach ($attributes as $attribute) {
$attributeClass = $attribute->getName();
if (!in_array(IAttribute::class, class_implements($attributeClass))) continue;
$attributeClass::action($item->object);
}
}
}
}
class PersonUpper
{
#[Letter(Letter::UPPER_CASE)]
public string $name;
public function setName(string $name)
{
$this->name = $name;
processAnnotations([
(object) [
'object' => $this,
'property' => 'name',
],
]);
}
}
class PersonLower
{
#[Letter(Letter::LOWER_CASE)]
public string $name;
public function setName(string $name)
{
$this->name = $name;
}
}
$felipe = new PersonUpper();
$felipe->setName('Felipe');
$renan = new PersonLower();
$renan->setName('Felipe');
var_dump($felipe, $renan);
processAnnotations([
(object) [
'object' => $renan,
'property' => 'name',
],
]);
var_dump($felipe, $renan);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment