WIP deep array format
authorColeman Watts <coleman@civicrm.org>
Wed, 11 Sep 2019 20:32:28 +0000 (16:32 -0400)
committerCiviCRM <info@civicrm.org>
Wed, 16 Sep 2020 02:13:18 +0000 (19:13 -0700)
ext/afform/core/CRM/Afform/ArrayHtml.php
ext/afform/core/Civi/Api4/Utils/AfformFormatTrait.php

index 3a94dddf4fdfea335ca014d2ec95fdcd59254c3a..af5c4a4c410e60ca1287936b30ec783b01c50202 100644 (file)
@@ -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: '<div class="greeting">Hello world</div>'
    */
-  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: '<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 [];
     }
index 58110d74f04c334e6d962d058901e07d6edfff3a..4f3af742812753453b288100658c82e256e75749 100644 (file)
@@ -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);
   }
 
 }