Build Page Elements by Block

Takes array of page elements and changes keys to use block->key as array keys
@param array  $elements Array of page element domain models
@return array
328637
protected function buildElementsByBlock($elements)
    {
        if (empty($elements)) {
            return $elements;
        }

        $output = [];
        foreach ($elements as $element) {
            $output[$element->block_key][] = $element;
        }

        return $output;
    }
1363717
public static function groupElementsByValueKeys($array, $groupKeys) {

        if (!is_array($groupKeys)) {
            $groupKeys = array($groupKeys);
        }

        $newArray = new AssociativeArray();
        foreach ($array as $element) {

            $applyObject = $newArray;
            foreach ($groupKeys as $index => $groupKey) {
                if ($index < sizeof($groupKeys) - 1) {
                    $subArray = $applyObject[$element[$groupKeys[$index]]];
                    if (!$subArray) {
                        $subArray = new AssociativeArray();
                        $applyObject[$element[$groupKeys[$index]]] = $subArray;
                    }
                    $applyObject = $subArray;
                } else {
                    if (!is_array($applyObject[$element[$groupKeys[$index]]])) {
                        $applyObject[$element[$groupKeys[$index]]] = array();
                    }
                    $applyObject[$element[$groupKeys[$index]]][] = $element;

                }
            }

        }

        
        return $newArray->toArray();
    }
1005428
final public function blockCreate(array $block)
    {
        $result = [];
        foreach ($block as $k => $pointer) {
            $result[$k] = $this->create($pointer);
        }
        return $result;
    }
1305632
protected function getAsync(array $keys)
    {
        return sprintf(
'<div id="block-cms-%s" >
    <script>
        /*<![CDATA[*/
            (function() {
                var b = document.createElement("script");
                b.type = "text/javascript";
                b.async = true;
                b.src = "%s";
                var s = document.getElementsByTagName("script")[0];
                s.parentNode.insertBefore(b, s);
            })();

        /*]]>*/
    </script>
</div>', $keys['block_id'], $this->router->generate('sonata_page_js_async_cache', $keys, UrlGeneratorInterface::ABSOLUTE_URL));
    }
233958
public static function create($elements) {
    /** @var ArrayNode $node */
    $node = Parser::parseExpression('[]');
    /** @var \Pharborist\Node $element */
    foreach ($elements as $element) {
      $node->getElementList()->appendItem($element);
    }
    return $node;
  }
758542
public function addElement(array $element)
    {
        if (array_key_exists('boxes', $element)) {
            if (array_key_exists($element['key'], $this->dashboard['boxes'])) {
                $this->dashboard['boxes'][$element['key']] = array_merge($this->dashboard['boxes'][$element['key']], $element['boxes']);
            } else {
                $this->dashboard['boxes'][$element['key']] = $element['boxes'];
            }
        }
        if (array_key_exists('cards', $element)) {
            if (array_key_exists($element['key'], $this->dashboard['cards'])) {
                $this->dashboard['cards'][$element['key']] = array_merge($this->dashboard['cards'][$element['key']], $element['cards']);
            } else {
                $this->dashboard['cards'][$element['key']] = $element['cards'];
            }
        }

        return $this;
    }
1191945
public function elementsGroup($elements){

        $groups = $this->formGroups;
        $groupNames = [];
        $groupEles = [];
        foreach ($groups as $k=>$v){
            $groupNames[] = $k;
            $groupEles[$v['name']] = ['title'=>$v['title']];
        }

        foreach ($elements as $ele){
            if (isset($ele['group']) && $ele['group']){
                $group = $ele['group'];
                if (in_array($group , $groupNames)){
                    $groupEles[$group]['elements'][] = $ele;
                }else{
                    $groupEles['base']['elements'][] = $ele;
                }
            }else {
                $groupEles['base']['elements'][] = $ele;
            }

        }

        return $groupEles;
    }
1013325
private static function simpleXmlToArray($element)
    {
        //====================================================================//
        // Safety Check
        if (false === $element) {
            return false;
        }
        //====================================================================//
        // Init Result
        $result = array();
        //====================================================================//
        // Get First Level Childrens
        $children = $element->children();
        $isArrayElement = false;
        //====================================================================//
        // For All Childrens
        foreach ($children as $elementName => $node) {
            //====================================================================//
            // This Element is an ArrayElement
            $isArrayElement = true;
            //====================================================================//
            // If Element Doesn't Already Exists => Store as Single Element
            if (!isset($result[$elementName])) {
                $result[$elementName] = array();
                $result[$elementName] = self::simpleXmlToArray($node);

                continue;
            }
            //====================================================================//
            // Element Already Exists => Store as Array Element
            //====================================================================//
            // Convert Single Element to Array Element
            if (!is_array($result[$elementName])) {
                $singleElement = $result[$elementName];       // Store Firts Element
                $result[$elementName] = array();                    // Create New Array
                $result[$elementName][] = $singleElement;             // Append To Array
            }
            //====================================================================//
            // Append Array Element
            $result[$elementName][] = self::simpleXmlToArray($node);
        }
        //====================================================================//
        // Return Single Element
        if (!$isArrayElement && '' == $children->getName()) {
            $result = base64_decode((string) $element, true);
        }

        return $result;
    }
125215
public function getMissingBlocks(Array $blocks = array())
    {
        $missing = array();
        $blocks  = $blocks ? $blocks : $this->getWroteBlocks();
        $last    = array_shift($blocks);

        if ($last['offset'] !== 0) {
            $missing[] = array('offset' => 0, 'size' => $last['offset']);
        }

        foreach ($blocks as $block) {
            if ($block['offset'] !== $last['offset'] + $last['size']) {
                $offset = $last['offset'] + $last['size'];
                $missing[] = array('offset' => $offset, 'size' => $block['offset'] - $offset);
            }
            $last = $block;
        }

        return $missing;
    }
560988
public function merge($elements = []): NodeList {
        if (!is_array($elements)) {
            $elements = $elements->toArray();
        }

        return $this->newNodeList(array_merge($this->toArray(), $elements));
    }
949215
public function push(...$elements): self
    {
        foreach ($elements as $element) {
            $this->validate($element);

            $this->elements[] = $element;
        }

        return $this;
    }
977622
protected function mapIDs($pages)
    {
        $ids = array();
        if ($pages) {
            foreach ($pages as $page) {
                $ids[] = array('ID' => $page->ID, 'ParentID' => $page->ParentID);
            }
        }
        return $ids;
    }
1123196
protected function getBlocks($nodes) {
    // Each completed block.
    $blocks = array();
    // Container for any top-level inline elements.
    $block = '';
    // Add a completed block to $blocks.
    $add_block = function($string = NULL) use(&$blocks, &$block) {
      if (preg_match('/\S/u', $block)) {
        $blocks[] = trim($block, " \t");
      }
      if (preg_match('/\S/u', $string)) {
        $blocks[] = rtrim($string, " \t");
      }
      $block = '';
    };
    /** @var \DOMNode $node */
    foreach ($nodes as $node) {
      switch ($node->nodeType) {

        case XML_ELEMENT_NODE:
          /** @var \DOMElement $node */
          switch ($node->nodeName) {

            // Explicitly handle these elements.

            case 'p':
            case 'pre':
              $add_block($this->getBlock($node));
              break;

            case 'h1':
              $add_block('# ' . $this->getBlock($node));
              break;

            case 'h2':
              $add_block('## ' . $this->getBlock($node));
              break;

            case 'h3':
              $add_block('### ' . $this->getBlock($node));
              break;

            case 'h4':
              $add_block('#### ' . $this->getBlock($node));
              break;

            case 'h5':
              $add_block('##### ' . $this->getBlock($node));
              break;

            case 'h6':
              $add_block('###### ' . $this->getBlock($node));
              break;

            case 'hr':
              $add_block('***');
              break;

            case 'ul':
            case 'ol':
            case 'dl':
              $add_block($this->getBlockList($node));
              break;

            // Other block-level elements.
            case 'article':
            case 'aside':
            case 'blockquote':
            case 'dd':
            case 'div':
            case 'fieldset':
            case 'figcaption':
            case 'figure':
            case 'footer':
            case 'form':
            case 'header':
            case 'hgroup':
            case 'main':
            case 'nav':
            case 'output':
            case 'section':
              // Recursively handle any descendant elements we care about.
              foreach ($this->getBlocks($node->childNodes) as $b) {
                $add_block($b);
              }
              break;

            // Unsupported.
            case 'canvas':
            case 'noscript':
            case 'script':
            case 'table':
            case 'tfoot':
            case 'video':
              break;

            // Treat as inline element.
            default:
              // Append series of inline elements together, as if they were
              // inside a block-level element.
              $block .= $this->getBlock($node);
              break;

          }
          break;

        case XML_TEXT_NODE:
          /** @var \DOMText $node */
          $block .= self::escape($node->textContent);
          break;

      }
    }

    $add_block();
    return $blocks;
  }
1301567
private function buildItems(array $tags): array
    {
        return array_map(
            function (Tag $tag): Item {
                return $this->buildItem($tag);
            },
            $tags
        );
    }
341493
protected function elementToArray(\SimpleXMLElement $element): array
    {
        $result = [];
        foreach ($element->xpath('*') as $node) {
            if (count($node->xpath('*')) > 0) {
                $value = $this->elementToArray($node);
            } else {
                $value = strval($node);
            }
            $result = array_merge_recursive($result, [$node->getName() => $value]);
        }

        return $result;
    }
510079
public function addAll(array $elements) : Set
    {
        $this->elements = self::sanitize(array_merge($this->elements, $elements));

        return $this;
    }
633610
protected function getIndexKey($element)
    {
        $key = $element->getFullyQualifiedStructuralElementName();

        // properties should have an additional $ before the property name
        if ($element instanceof PropertyInterface) {
            list($fqcn, $propertyName) = explode('::', $key);
            $key = $fqcn . '::$' . $propertyName;
        }

        return $key;
    }