X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fconsolidation%2Foutput-formatters%2Fsrc%2FStructuredData%2FRenderCellCollectionTrait.php;fp=vendor%2Fconsolidation%2Foutput-formatters%2Fsrc%2FStructuredData%2FRenderCellCollectionTrait.php;h=c4f23c7d38309495a16e827cba274111c2ce3722;hp=0000000000000000000000000000000000000000;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad diff --git a/vendor/consolidation/output-formatters/src/StructuredData/RenderCellCollectionTrait.php b/vendor/consolidation/output-formatters/src/StructuredData/RenderCellCollectionTrait.php new file mode 100644 index 000000000..c4f23c7d3 --- /dev/null +++ b/vendor/consolidation/output-formatters/src/StructuredData/RenderCellCollectionTrait.php @@ -0,0 +1,59 @@ + [], + RenderCellCollectionInterface::PRIORITY_NORMAL => [], + RenderCellCollectionInterface::PRIORITY_FALLBACK => [], + ]; + + /** + * Add a renderer + * + * @return $this + */ + public function addRenderer(RenderCellInterface $renderer, $priority = RenderCellCollectionInterface::PRIORITY_NORMAL) + { + $this->rendererList[$priority][] = $renderer; + return $this; + } + + /** + * Add a callable as a renderer + * + * @return $this + */ + public function addRendererFunction(callable $rendererFn, $priority = RenderCellCollectionInterface::PRIORITY_NORMAL) + { + $renderer = new CallableRenderer($rendererFn); + return $this->addRenderer($renderer, $priority); + } + + /** + * {@inheritdoc} + */ + public function renderCell($key, $cellData, FormatterOptions $options, $rowData) + { + $flattenedRendererList = array_reduce( + $this->rendererList, + function ($carry, $item) { + return array_merge($carry, $item); + }, + [] + ); + + foreach ($flattenedRendererList as $renderer) { + $cellData = $renderer->renderCell($key, $cellData, $options, $rowData); + if (is_string($cellData)) { + return $cellData; + } + } + return $cellData; + } +}