/**
* @param array $array
* Ex: ['#tag' => 'div', 'class' => 'greeting', '#children' => ['Hello world']]
+ * @param string $format
* @return string
* Ex: '<div class="greeting">Hello world</div>'
*/
- public function convertArrayToHtml($array) {
+ public function convertArrayToHtml(array $array, $format = 'shallow') {
if ($array === []) {
return '';
}
/**
* @param string $html
* Ex: '<div class="greeting">Hello world</div>'
+ * @param string $format
* @return array
* Ex: ['#tag' => 'div', 'class' => 'greeting', '#children' => ['Hello world']]
*/
- public function convertHtmlToArray($html) {
+ public function convertHtmlToArray($html, $format = 'shallow') {
if ($html === '') {
return [];
}
/**
* @var string
- * Either 'array' or 'html'.
+ * @options html,shallow,deep
*/
- protected $layoutFormat = 'array';
+ protected $layoutFormat = 'shallow';
/**
* @param string $html
* @throws \API_Exception
*/
protected function convertHtmlToOutput($html) {
- switch ($this->layoutFormat) {
- case 'html':
- return $html;
-
- case 'array':
- case NULL:
- $converter = new \CRM_Afform_ArrayHtml();
- return $converter->convertHtmlToArray($html);
-
- default:
- throw new \API_Exception("Requested format is unrecognized");
+ if ($this->layoutFormat === 'html') {
+ return $html;
}
+ $converter = new \CRM_Afform_ArrayHtml();
+ return $converter->convertHtmlToArray($html, $this->layoutFormat);
}
/**
* @throws \API_Exception
*/
protected function convertInputToHtml($mixed) {
- switch ($this->layoutFormat) {
- case 'html':
- return $mixed;
-
- case 'array':
- case NULL:
- $converter = new \CRM_Afform_ArrayHtml();
- return $converter->convertArrayToHtml($mixed);
-
- default:
- throw new \API_Exception("Requested format is unrecognized");
+ if ($this->layoutFormat === 'html') {
+ return $mixed;
}
+ $converter = new \CRM_Afform_ArrayHtml();
+ return $converter->convertArrayToHtml($mixed, $this->layoutFormat);
}
}