Created
April 17, 2015 06:52
-
-
Save nimmneun/f6ec97ba632ab10769df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * @Author neun | |
| * @since 17.04.2015 23:51 | |
| */ | |
| class XMLifier | |
| { | |
| /** | |
| * Regex chars that _dont_ need to be wrapped in CDATA. | |
| * @var string | |
| */ | |
| private static $noCDATA = '\w\s\%.,+\-:'; | |
| private static $w; | |
| /** | |
| * Some extra stuff before we get going. | |
| * @param $array | |
| * @param string $noCDATA | |
| * @return string | |
| */ | |
| public static function toXml($array, $noCDATA = null) | |
| { | |
| if (null !== $noCDATA) | |
| { | |
| self::$noCDATA = $noCDATA; | |
| } | |
| self::$w = new XMLWriter; | |
| self::$w->openMemory(); | |
| self::$w->setIndentString("\t"); | |
| self::$w->setIndent(true); | |
| self::xmlify($array); | |
| echo self::$w->outputMemory(); | |
| } | |
| /** | |
| * Expects an n-dimensional array to be transformed into a XML string. | |
| * @param $array | |
| * @return null|string | |
| */ | |
| private static function xmlify($array) | |
| { | |
| foreach ($array as $name => $value) | |
| { | |
| if (is_array($value)) | |
| { | |
| self::$w->startElement($name); | |
| } | |
| if (!is_array($value)) | |
| { | |
| if (!preg_match('/^[' . self::$noCDATA . ']+$/', $value, $m)) | |
| { | |
| self::$w->startElement($name); | |
| self::$w->writeCData($value); | |
| self::$w->endElement(); | |
| } | |
| else | |
| { | |
| self::$w->writeElement($name, $value); | |
| } | |
| } | |
| else | |
| { | |
| self::xmlify($value); | |
| } | |
| if (is_array($value)) | |
| { | |
| self::$w->endElement(); | |
| } | |
| } | |
| } | |
| } | |
| /** | |
| * My lovable dummy for testing ... | |
| */ | |
| $dummy = | |
| array( | |
| 'eins' => array( | |
| 'title' => 'SuperXXL Shirt', | |
| 'info' => "That's one friggin awesome shirt there!! <3", | |
| 'attributes' => array( | |
| 'size' => 'XXL', | |
| 'color' => 'blue', | |
| 'price' => '9.95', | |
| 'discounts' => array( | |
| 'spring' => '10%', | |
| 'summer' => '5%', | |
| 'autumn' => '15%', | |
| 'winter' => '20%', | |
| ) | |
| ) | |
| ), | |
| 'zwei' => array( | |
| 'title' => 'MegaXXS Shirt', | |
| 'attributes' => array( | |
| 'size' => 'XXS', | |
| 'color' => 'green', | |
| 'price' => '7.95', | |
| 'materials' => array( | |
| 'whool' => '50%', | |
| 'polyester' => '50%' | |
| ) | |
| ), | |
| 'shipping' => array( | |
| 'domestic' => '2.95', | |
| 'europe' => '4.95', | |
| 'other' => '9.99' | |
| ) | |
| ), | |
| 'drei' => array( | |
| 'title' => 'BoringM Shirt', | |
| 'attributes' => array( | |
| 'size' => 'M', | |
| 'color' => 'yellow', | |
| 'price' => '8.97', | |
| 'some' => array( | |
| 'other' => array( | |
| 'funny' => array( | |
| 'value' => array( | |
| 'with' => array( | |
| 'crazy' => array( | |
| 'deep' => array( | |
| 'nesting' => 'lol' | |
| ) | |
| ) | |
| ) | |
| ) | |
| ) | |
| ) | |
| ) | |
| ) | |
| ) | |
| ); | |
| header("Content-type:text/xml"); | |
| echo XMLifier::toXml($dummy); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment