3535 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3636 */
3737class array_converter {
38- /** @var callable[] associative array of class names to configuration hook functions for those classes */
39- private static array $ hooks = [];
40-
41- /**
42- * Customizes the way in which classes and their subclasses are converted from and to arrays.
43- *
44- * In the case of traits, the configuration will be applied to any classes using them.
45- * Configuration is done in hook functions. When converting to or from a class instance, all hook function of it and
46- * its superclasses and used traits are applied to a single {@see converter_config} instance.
47- *
48- * @param string $class class or trait for whom array conversion should be customized
49- * @param callable $hook configuration function which takes a {@see converter_config} instance as its argument,
50- * adds its own configuration to it, and returning nothing
51- * @see converter_config for the available options
52- */
53- public static function configure (string $ class , callable $ hook ): void {
54- self ::$ hooks [$ class ] = $ hook ;
55- }
56-
5738 /**
5839 * Recursively converts an array to an instance of the given class.
5940 *
@@ -345,46 +326,27 @@ private static function convert_to_required_type(?ReflectionNamedType $type, con
345326 }
346327
347328 /**
348- * Calls all configuration hooks for the given class.
329+ * Inspects the attributes on the given class, superclasses and traits and updates the given config .
349330 *
350- * @param ReflectionClass $class
331+ * @param ReflectionClass $reflect
351332 * @param converter_config|null $config an existing config to add to or null, in which case a new one will be
352333 * created
353334 * @return converter_config
354- * @see self::configure()
355335 */
356- private static function get_config_for (ReflectionClass $ class , ?converter_config $ config = null ): converter_config {
336+ private static function get_config_for (ReflectionClass $ reflect , ?converter_config $ config = null ): converter_config {
357337 if (!$ config ) {
358338 $ config = new converter_config ();
359339 }
360340
361- $ parent = $ class ->getParentClass ();
341+ $ parent = $ reflect ->getParentClass ();
362342 if ($ parent ) {
363343 $ config = self ::get_config_for ($ parent , $ config );
364344 }
365345
366- foreach ($ class ->getTraits () as $ trait ) {
346+ foreach ($ reflect ->getTraits () as $ trait ) {
367347 $ config = self ::get_config_for ($ trait , $ config );
368348 }
369349
370- self ::configure_from_attributes ($ class , $ config );
371-
372- $ hook = self ::$ hooks [$ class ->getName ()] ?? null ;
373- if ($ hook ) {
374- $ hook ($ config );
375- }
376-
377- return $ config ;
378- }
379-
380- /**
381- * Inspects the attributes on the given class and updates the given config.
382- *
383- * @param ReflectionClass $reflect
384- * @param converter_config $config
385- * @return void
386- */
387- private static function configure_from_attributes (ReflectionClass $ reflect , converter_config $ config ): void {
388350 $ polyattrs = $ reflect ->getAttributes (array_polymorphic::class);
389351 foreach ($ polyattrs as $ attr ) {
390352 /** @var array_polymorphic $instance */
@@ -400,14 +362,20 @@ private static function configure_from_attributes(ReflectionClass $reflect, conv
400362 }
401363
402364 foreach ($ property ->getAttributes (array_key::class) as $ attr ) {
403- $ config ->rename ( $ property ->getName (), $ attr ->newInstance ()->key ) ;
365+ $ config ->renames [ $ property ->getName ()] = $ attr ->newInstance ()->key ;
404366 }
405367 foreach ($ property ->getAttributes (array_alias::class) as $ attr ) {
406- $ config ->alias ($ property ->getName (), $ attr ->newInstance ()->alias );
368+ if (isset ($ config ->aliases [$ property ->getName ()])) {
369+ $ config ->aliases [$ property ->getName ()][] = $ attr ->newInstance ()->alias ;
370+ } else {
371+ $ config ->aliases [$ property ->getName ()] = [$ attr ->newInstance ()->alias ];
372+ }
407373 }
408374 foreach ($ property ->getAttributes (array_element_class::class) as $ attr ) {
409- $ config ->array_elements ( $ property ->getName (), $ attr ->newInstance ()->class ) ;
375+ $ config ->elementclasses [ $ property ->getName ()] = $ attr ->newInstance ()->class ;
410376 }
411377 }
378+
379+ return $ config ;
412380 }
413381}
0 commit comments