annotate vendor/consolidation/output-formatters/src/StructuredData/RenderCellCollectionTrait.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 namespace Consolidation\OutputFormatters\StructuredData;
Chris@0 3
Chris@0 4 use Consolidation\OutputFormatters\Options\FormatterOptions;
Chris@13 5 use Consolidation\OutputFormatters\Formatters\FormatterAwareInterface;
Chris@13 6 use Consolidation\OutputFormatters\Formatters\FormatterAwareTrait;
Chris@0 7
Chris@0 8 trait RenderCellCollectionTrait
Chris@0 9 {
Chris@13 10 use FormatterAwareTrait;
Chris@0 11
Chris@0 12 /** @var RenderCellInterface[] */
Chris@0 13 protected $rendererList = [
Chris@0 14 RenderCellCollectionInterface::PRIORITY_FIRST => [],
Chris@0 15 RenderCellCollectionInterface::PRIORITY_NORMAL => [],
Chris@0 16 RenderCellCollectionInterface::PRIORITY_FALLBACK => [],
Chris@0 17 ];
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Add a renderer
Chris@0 21 *
Chris@0 22 * @return $this
Chris@0 23 */
Chris@0 24 public function addRenderer(RenderCellInterface $renderer, $priority = RenderCellCollectionInterface::PRIORITY_NORMAL)
Chris@0 25 {
Chris@0 26 $this->rendererList[$priority][] = $renderer;
Chris@0 27 return $this;
Chris@0 28 }
Chris@0 29
Chris@0 30 /**
Chris@0 31 * Add a callable as a renderer
Chris@0 32 *
Chris@0 33 * @return $this
Chris@0 34 */
Chris@0 35 public function addRendererFunction(callable $rendererFn, $priority = RenderCellCollectionInterface::PRIORITY_NORMAL)
Chris@0 36 {
Chris@0 37 $renderer = new CallableRenderer($rendererFn);
Chris@0 38 return $this->addRenderer($renderer, $priority);
Chris@0 39 }
Chris@0 40
Chris@0 41 /**
Chris@0 42 * {@inheritdoc}
Chris@0 43 */
Chris@0 44 public function renderCell($key, $cellData, FormatterOptions $options, $rowData)
Chris@0 45 {
Chris@0 46 $flattenedRendererList = array_reduce(
Chris@0 47 $this->rendererList,
Chris@0 48 function ($carry, $item) {
Chris@0 49 return array_merge($carry, $item);
Chris@0 50 },
Chris@0 51 []
Chris@0 52 );
Chris@0 53
Chris@0 54 foreach ($flattenedRendererList as $renderer) {
Chris@13 55 if ($renderer instanceof FormatterAwareInterface) {
Chris@13 56 $renderer->setFormatter($this->getFormatter());
Chris@13 57 }
Chris@0 58 $cellData = $renderer->renderCell($key, $cellData, $options, $rowData);
Chris@0 59 }
Chris@0 60 return $cellData;
Chris@0 61 }
Chris@0 62 }