Skip to content

Commit 026cab7

Browse files
committed
refactor: remove array_converter::configure and related methods
1 parent 3655942 commit 026cab7

File tree

2 files changed

+15
-139
lines changed

2 files changed

+15
-139
lines changed

classes/array_converter/array_converter.php

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,6 @@
3535
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
3636
*/
3737
class 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
}

classes/array_converter/converter_config.php

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
namespace qtype_questionpy\array_converter;
1818

1919
/**
20-
* Allows customization of conversions by {@see array_converter}.
21-
*
22-
* @see array_converter::configure()
20+
* Holds customization of {@see array_converter}.
2321
*
2422
* @package qtype_questionpy
2523
* @author Maximilian Haye
@@ -41,94 +39,4 @@ class converter_config {
4139

4240
/** @var string[] mapping from property names to the classes of their array elements */
4341
public array $elementclasses = [];
44-
45-
/**
46-
* Changes the name under which the value of a property appears in arrays.
47-
*
48-
* Renames differ from {@see self::alias() aliases} in that they apply to both serialization and deserialization,
49-
* and replace the original property name.
50-
*
51-
* @param string $propname property whose array key to rename
52-
* @param string $arraykey new array key
53-
* @return $this for chaining
54-
*/
55-
public function rename(string $propname, string $arraykey): self {
56-
$this->renames[$propname] = $arraykey;
57-
return $this;
58-
}
59-
60-
/**
61-
* Adds an alias for the given property.
62-
*
63-
* Aliases differ from {@see self::rename() renames} in that they only apply to deserialization, and are tried in
64-
* addition to the original property name (or rename, if any).
65-
*
66-
* @param string $propname property name for which the alias should be tried
67-
* @param string $alias new alias
68-
* @return $this for chaining
69-
*/
70-
public function alias(string $propname, string $alias): self {
71-
if (isset($this->aliases[$propname])) {
72-
$this->aliases[$propname][] = $alias;
73-
} else {
74-
$this->aliases[$propname] = [$alias];
75-
}
76-
return $this;
77-
}
78-
79-
/**
80-
* Enables polymorphic deserialization for this class, using the given key as a discriminator.
81-
*
82-
* The value of the given array key will determine the actual class ('variant') used for deserialization. Variants
83-
* are registered using {@see self::variant()}.
84-
*
85-
* @param string $discriminator key of the array entry determining the concrete class to deserialize to.
86-
* @return $this for chaining
87-
*/
88-
public function discriminate_by(string $discriminator): self {
89-
$this->discriminator = $discriminator;
90-
return $this;
91-
}
92-
93-
/**
94-
* Adds a variant for polymorphic deserialization.
95-
*
96-
* @param string $discriminator the discriminator value associated with the concrete class
97-
* @param string $classname the concrete class to convert to when the discriminator is encountered
98-
* @return $this for chaining
99-
* @see self::discriminate_by()
100-
* @see self::fallback_variant()
101-
*/
102-
public function variant(string $discriminator, string $classname): self {
103-
$this->variants[$discriminator] = $classname;
104-
return $this;
105-
}
106-
107-
/**
108-
* Sets the fallback variant for polymorphic deserialization.
109-
*
110-
* When a discriminator is encountered which isn't {@see variant registered}, the default behaviour is to throw an
111-
* exception. Instead, you can register a fallback class to be used. A debugging message will still be emitted.
112-
*
113-
* @param string $classname the concrete class to convert to when an unknown discriminator is encountered
114-
* @return $this for chaining
115-
* @see self::discriminate_by()
116-
* @see self::variant()
117-
*/
118-
public function fallback_variant(string $classname): self {
119-
$this->fallbackvariant = $classname;
120-
return $this;
121-
}
122-
123-
/**
124-
* For an array-typed property with the given name, sets the class to use for the deserialization of its elements.
125-
*
126-
* @param string $propname property name
127-
* @param string $class class to deserialize to
128-
* @return $this for chaining
129-
*/
130-
public function array_elements(string $propname, string $class): self {
131-
$this->elementclasses[$propname] = $class;
132-
return $this;
133-
}
13442
}

0 commit comments

Comments
 (0)