From 0813676bbada920cfb5b7b8e942ad2d2ee512728 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 11 Sep 2019 16:32:28 -0400 Subject: [PATCH] WIP deep array format --- ext/afform/core/CRM/Afform/ArrayHtml.php | 6 ++-- .../Civi/Api4/Utils/AfformFormatTrait.php | 34 ++++++------------- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/ext/afform/core/CRM/Afform/ArrayHtml.php b/ext/afform/core/CRM/Afform/ArrayHtml.php index 3a94dddf4f..af5c4a4c41 100644 --- a/ext/afform/core/CRM/Afform/ArrayHtml.php +++ b/ext/afform/core/CRM/Afform/ArrayHtml.php @@ -13,10 +13,11 @@ class CRM_Afform_ArrayHtml { /** * @param array $array * Ex: ['#tag' => 'div', 'class' => 'greeting', '#children' => ['Hello world']] + * @param string $format * @return string * Ex: '
Hello world
' */ - public function convertArrayToHtml($array) { + public function convertArrayToHtml(array $array, $format = 'shallow') { if ($array === []) { return ''; } @@ -66,10 +67,11 @@ class CRM_Afform_ArrayHtml { /** * @param string $html * Ex: '
Hello world
' + * @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 []; } diff --git a/ext/afform/core/Civi/Api4/Utils/AfformFormatTrait.php b/ext/afform/core/Civi/Api4/Utils/AfformFormatTrait.php index 58110d74f0..4f3af74281 100644 --- a/ext/afform/core/Civi/Api4/Utils/AfformFormatTrait.php +++ b/ext/afform/core/Civi/Api4/Utils/AfformFormatTrait.php @@ -12,9 +12,9 @@ trait AfformFormatTrait { /** * @var string - * Either 'array' or 'html'. + * @options html,shallow,deep */ - protected $layoutFormat = 'array'; + protected $layoutFormat = 'shallow'; /** * @param string $html @@ -22,18 +22,11 @@ trait AfformFormatTrait { * @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); } /** @@ -42,18 +35,11 @@ trait AfformFormatTrait { * @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); } } -- 2.25.1