"Fossies" - the Fresh Open Source Software Archive

Member "papayacms-core-6.12.5/src/system/Papaya/Template/Engine/Values/ArrayLoader.php" (7 Jul 2020, 1693 Bytes) of package /linux/www/papayacms-core-6.12.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ArrayLoader.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 /**
    3  * papaya CMS
    4  *
    5  * @copyright 2000-2018 by papayaCMS project - All rights reserved.
    6  * @link http://www.papaya-cms.com/
    7  * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License, version 2
    8  *
    9  *  You can redistribute and/or modify this script under the terms of the GNU General Public
   10  *  License (GPL) version 2, provided that the copyright and license notes, including these
   11  *  lines, remain unmodified. papaya is distributed in the hope that it will be useful, but
   12  *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13  *  FOR A PARTICULAR PURPOSE.
   14  */
   15 namespace Papaya\Template\Engine\Values {
   16 
   17   use Papaya\Utility\Constraints;
   18   use Papaya\XML\Document;
   19   use Papaya\XML\Element;
   20 
   21   class ArrayLoader implements Loadable {
   22 
   23     /**
   24      * @param array|\Traversable $values
   25      * @return false|Document|Element
   26      */
   27     public function load($values) {
   28       Constraints::assertArrayOrTraversable($values);
   29       $document = new Document();
   30       $this->appendArray($document->appendElement('_'), $values);
   31       return $document->documentElement;
   32     }
   33 
   34     /**
   35      * @param Element $parent
   36      * @param mixed $data
   37      */
   38     private function appendArray(Element $parent, $data) {
   39       foreach ($data as $key => $value) {
   40         $nodeName = \preg_replace('([^A-Za-z_-])', '', $key) ?: NULL;
   41         if (!$nodeName) {
   42           continue;
   43         }
   44         if (\is_array($value) || $value instanceof \Traversable) {
   45           $this->appendArray($parent->appendElement($nodeName), $value);
   46         } else {
   47           $parent->appendElement($nodeName, (string)$value);
   48         }
   49       }
   50     }
   51   }
   52 }