schmittjoh / serializer

Library for (de-)serializing data of any complexity (supports JSON, and XML)
http://jmsyst.com/libs/serializer
MIT License
2.32k stars 585 forks source link

Handling of Property Order when using Traits #1419

Open Hanmac opened 2 years ago

Hanmac commented 2 years ago
Q A
Bug report? no
Feature request? yes
BC Break report? ?
RFC? no

Steps required to reproduce the problem

use annotations on properties, and have some of them be inserted via traits:

trait T { protected $t; }
class C {
  protected $a;
  use T;
  protected $b;
}

Expected Result

i Expected the result that $t is serialized between $a and $b

Actual Result

But instead $t is serialized after $b.

i assume it has something to do with the order of the properties like: (new ReflectionClass(C::class))->getProperties()

I assume that PHP can't detect at which position the use is called.

for a Possible Solution, a better way of ordering the Properties would be nice, like to give $a the @Order(-1) and $b the @Order( +1). So when it would try to order after this new annotation, it would order them $a, $t, $b as i want.

The Reason

I have multiple classes that uses the same (block of) attributes over and over again, but because the xml needs to follow a specific sequence of elements, they need to be in a specific order.

scyzoryck commented 2 years ago

I'm afraid it seems to be impossible with Reflection - we would need to rewrite it to use AST instead. May you can try to use AccessorOrder annotation?

Hanmac commented 2 years ago

AccessorOrder means i would need to use custom and to define the whole order again

if i had a way to define a relative order (-1 vs +1) i think it would be way cleaner

pvm commented 2 months ago

I'm having the same issue, any news on this?