8000 [ValueExporter] added priority to formatters · symfony/symfony@eb1a34d · GitHub
[go: up one dir, main page]

Skip to content

Commit eb1a34d

Browse files
committed
[ValueExporter] added priority to formatters
1 parent 438b845 commit eb1a34d

File tree

4 files changed

+55
-43
lines changed

4 files changed

+55
-43
lines changed

src/Symfony/Component/ValueExporter/Exporter/AbstractValueExporter.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,19 @@ abstract class AbstractValueExporter implements ValueExporterInterface
3030
protected $formatterInterface = FormatterInterface::class;
3131

3232
/**
33-
* An array of formatters.
33+
* An array of arrays of formatters by priority.
3434
*
35-
* @var FormatterInterface[]
35+
* @var array[]
3636
*/
3737
private $formatters = array();
3838

39+
/**
40+
* A sorted array of formatters.
41+
*
42+
* @var FormatterInterface[]
43+
*/
44+
private $sortedFormatters;
45+
3946
/**
4047
* Takes {@link FormatterInterface} as arguments.
4148
*
@@ -49,25 +56,26 @@ final public function __construct()
4956
/**
5057
* {@inheritdoc}
5158
*/
52-
final public function addFormatters(array $appends, array $prepends = array())
59+
final public function addFormatters(array $formatters)
5360
{
54-
foreach ($appends as $append) {
55-
if (!$append instanceof $this->formatterInterface) {
56-
throw new InvalidFormatterException(get_class($append), self::class, $this->formatterInterface);
57-
}
58-
if ($append instanceof ExpandedFormatter) {
59-
$append->setExporter($this);
61+
$this->sortedFormatters = null;
62+
63+
foreach ($formatters as $formatter) {
64+
if (is_array($formatter)) {
65+
$priority = (int) $formatter[1];
66+
$formatter = $formatter[0];
67+
} else {
68+
$priority = 0;
6069
}
61-
$this->formatters[] = $append;
62-
}
63-
foreach (array_reverse($prepends) as $prepend) {
64-
if (!$prepend instanceof $this->formatterInterface) {
65-
throw new InvalidFormatterException(get_class($prepend), self::class, $this->formatterInterface);
70+
if (!$formatter instanceof $this->formatterInterface) {
71+
throw new InvalidFormatterException(get_class($formatter), self::class, $this->formatterInterface);
6672
}
67-
if ($prepend instanceof ExpandedFormatter) {
68-
$prepend->setExporter($this);
73+
if ($formatter instanceof ExpandedFormatter) {
74+
$formatter->setExporter($this);
6975
}
70-
array_unshift($this->formatters, $prepend);
76+
77+
// Using the class as key prevents duplicate
78+
$this->formatters[$priority][get_class($formatter)] = $formatter;
7179
}
7280
}
7381

@@ -76,6 +84,11 @@ final public function addFormatters(array $appends, array $prepends = array())
7684
*/
7785
final protected function formatters()
7886
{
79-
return $this->formatters;
87+
if (null === $this->sortedFormatters) {
88+
krsort($this->formatters);
89+
$this->sortedFormatters = call_user_func_array('array_merge', $this->formatters);
90+
}
91+
92+
return $this->sortedFormatters;
8093
}
8194
}

src/Symfony/Component/ValueExporter/Exporter/ValueExporterInterface.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ interface ValueExporterInterface
4242
public function exportValue($value, $depth, $expand);
4343

4444
/**
45-
* Adds {@link FormatterInterface} that will be called in the given order.
45+
* Adds {@link FormatterInterface} that will be called by priority.
4646
*
47-
* @param FormatterInterface[] $appends The formatters to execute at last
48-
* @param FormatterInterface[] $prepends The formatters to execute first
47+
* @param (FormatterInterface|array)[] $formatters
4948
*
5049
* @throws InvalidFormatterException If the exporter does not support a given formatter
5150
*/
52-
public function addFormatters(array $appends, array $prepends);
51+
public function addFormatters(array $formatters);
5352
}

src/Symfony/Component/ValueExporter/Tests/ValueExporterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testExportValueExpanded()
5252

5353
public function testExportTraversable()
5454
{
55-
ValueExporter::appendFormatter(new TraversableToStringFormatter());
55+
ValueExporter::addFormatters(array(new TraversableToStringFormatter()));
5656

5757
$value = new TraversableInstance();
5858
$exportedValue = <<<EOT

src/Symfony/Component/ValueExporter/ValueExporter.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ class ValueExporter
3030
{
3131
private static $handler;
3232
private static $exporter;
33-
private static $appends = array();
34-
private static $prepends = array();
33+
private static $formatters = array();
3534

3635
public static function export($value, $depth = 1, $expand = false)
3736
{
@@ -42,9 +41,9 @@ public static function export($value, $depth = 1, $expand = false)
4241
new EntityToStringFormatter(),
4342
new PhpIncompleteClassToStringFormatter()
4443
);
45-
$exporter->addFormatters(self::$appends, self::$prepends);
46-
// Clear extra formatters
47-
self::$appends = self::$prepends = array();
44+
$exporter->addFormatters(self::$formatters);
45+
// Clear formatters
46+
self::$formatters = array();
4847
self::$handler = function ($value, $depth = 1, $expand = false) use ($exporter) {
4948
return $exporter->exportValue($value, $depth, $expand);
5049
};
@@ -70,28 +69,29 @@ public static function setExporter(ValueExporterInterface $exporter)
7069
{
7170
self::$handler = null;
7271
self::$exporter = $exporter;
73-
self::$appends = self:: $prepends = array();
72+
self::$formatters = array();
7473
}
7574

7675
/**
77-
* Appends a {@link FormatterInterface} to the {@link ValueExporterInterface}.
76+
* Adds {@link FormatterInterface} to the {@link ValueExporterInterface}.
7877
*
79-
* @param FormatterInterface $formatter
80-
*/
81-
public static function appendFormatter(FormatterInterface $formatter)
82-
{
83-
self::$handler = null;
84-
self::$appends[] = $formatter;
85-
}
86-
87-
/**
88-
* Prepends a {@link FormatterInterface} to the {@link ValueExporterInterface}.
78+
* You can simple pass an instance or an array with the instance and the priority:
8979
*
90-
* @param FormatterInterface $formatter
80+
* <code>
81+
* ValueExporter::addFormatters(array(
82+
* new AcmeFormatter,
83+
* array(new AcmeOtherFormatter(), 10)
84+
* );
85+
* </code>
86+
*
87+
* @param mixed $formatters An array of FormatterInterface instances and/or
88+
* arrays holding an instance and its priority
9189
*/
92-
public static function prependFormatter(FormatterInterface $formatter)
90+
public static function addFormatters($formatters)
9391
{
9492
self::$handler = null;
95-
self::$prepends[] = $formatter;
93+
foreach ($formatters as $formatter) {
94+
self::$formatters[] = $formatter;
95+
}
9696
}
9797
}

0 commit comments

Comments
 (0)
0