Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * @author manyrus
- */
- abstract class Parse{
- /**
- * @abstract
- * @param string $string string|path to file
- * @return array string parsed data
- */
- abstract public function decode($string);
- }
- abstract class Xml extends Parse{
- /**
- * @param SimpleXMLElement $xml
- * @return array parsed xml
- */
- protected function simpleXmlToArray($xml) {
- $array = $xml;
- $newArray = array();
- $array = (array) $array;
- foreach ($array as $key => $value) {
- $value = (array) $value;
- if (isset($value [0])) {
- $newArray [$key] = trim($value [0]);
- } else {
- if(!empty($array[$key])) {
- $newArray [$key] = $this->simpleXmlToArray($value);
- } else {
- $newArray [$key] ='';
- }
- }
- }
- return $newArray;
- }
- }
- abstract class Ini extends Parse{}
- class ParseXmlFile extends Xml{
- /**
- * @param string $file
- * @see Parse
- */
- public function decode($file) {
- return $this->simpleXmlToArray(simplexml_load_file($file));
- }
- }
- class ParseXmlString extends Xml{
- /**
- * @param string $str
- * @see Parse
- */
- public function decode($str) {
- return $this->simpleXmlToArray(simplexml_load_string($str));
- }
- }
- class ParseIniFile extends Ini{
- /**
- * @param string $file
- * @see Parse
- */
- public function decode($file) {
- return parse_ini_file($file);
- }
- }
- class ParseIniString extends Ini{
- /**
- * @param string $str
- * @see Parse
- */
- public function decode($str) {
- return parse_ini_string($str);
- }
- }
- abstract class ParseFactory {
- /**
- * const of type
- */
- const XML = 0;
- const INI = 1;
- /**
- * @static
- * @abstract
- * @param const $type
- * @return Parse
- */
- abstract public static function createFactory($type);
- }
- class ParseStringFactory extends ParseFactory{
- /**
- * @see ParseFactory
- */
- public static function createFactory($type)
- {
- switch($type) {
- case self::INI:
- return new ParseIniString();
- break;
- case self::XML:
- return new ParseXmlString();
- break;
- }
- }
- }
- class ParseFileFactory extends ParseFactory{
- /**
- * @see ParseFactory
- */
- public static function createFactory($type)
- {
- switch($type) {
- case self::INI:
- return new ParseIniFile();
- break;
- case self::XML:
- return new ParseXmlFile();
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement