Source of file HtmlSerializer.php
Size: 1,666 Bytes - Last Modified: 2021-12-23T10:07:36+00:00
/var/www/docs.ssmods.com/process/src/code/serializers/HtmlSerializer.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 | <?php namespace Ntb\RestAPI; /** * Serializer for html. * @author Christian Blank <c.blank@notthatbad.net> */ class HtmlSerializer extends \ViewableData implements IRestSerializer { /** * @config */ private static $is_active = true; /** * The content type * @var string */ private $contentType = "text/html"; /** * The given data will be serialized into an html string using a Silverstripe template. * * @param array $data * @return string an html string */ public function serialize($data) { $list = $this->recursive($data, 1); return $this->renderWith(['Result', 'Controller'], ['Data' => \ArrayList::create($list)]); } public function contentType() { return $this->contentType; } private function recursive($data, $level) { $list = []; if(is_array($data)) { foreach ($data as $key => $value) { if(is_array($value)) { $list[] = \ArrayData::create(['Key' => $key, 'Value' => '', 'Heading' => true, 'Level' => $level]); $list = array_merge($list, $this->recursive($value, $level+1)); } else { $list[] = \ArrayData::create(['Key' => $key, 'Value' => $value, 'Level' => $level]); } } } return $list; } /** * Indicates if the serializer is active. * Serializers can be deactivated to use another implementation for the same mime type. * * @return boolean */ public function active() { return $this->config()->get('is_active'); } } |