new pisilinux web sites
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
<?php namespace ZN\XML;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Singleton;
|
||||
|
||||
class Builder
|
||||
{
|
||||
/**
|
||||
* Keeps version
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $version = '1.0';
|
||||
|
||||
/**
|
||||
* Keeps encoding
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $encoding = 'UTF-8';
|
||||
|
||||
/**
|
||||
* Creates the version of the query XML document.
|
||||
*
|
||||
* @param string $version = '1.0'
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function version(string $version = '1.0') : Builder
|
||||
{
|
||||
$this->version = $version;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies the encoding of an XML document.
|
||||
*
|
||||
* @param string $encoding = 'UTF-8'
|
||||
*
|
||||
* @return Builder
|
||||
*/
|
||||
public function encoding(string $encoding = 'UTF-8') : Builder
|
||||
{
|
||||
$this->encoding = $encoding;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build XML document.
|
||||
*
|
||||
* @param array $data
|
||||
* @param string $version = NULL
|
||||
* @param string $encoding = NULL
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function do(array $data, ?string $version = NULL, ?string $encoding = NULL) : string
|
||||
{
|
||||
if( ! empty($version) ) $this->version = $version;
|
||||
if( ! empty($encoding) ) $this->encoding = $encoding;
|
||||
|
||||
$xml = '<?xml version="' . $this->version . '" encoding="' . $this->encoding . '"?>' . EOL;
|
||||
$xml .= $this->_document($data, '', 0);
|
||||
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an XML document.
|
||||
*
|
||||
* @param string $xml = ''
|
||||
* @param string $tab = ''
|
||||
* @param string $start = 0
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _document($xml = '', $tab = '', $start = 0)
|
||||
{
|
||||
static $start;
|
||||
|
||||
$eof = EOL;
|
||||
$output = '';
|
||||
$tab = str_repeat("\t", (int) $start);
|
||||
|
||||
if( ! isset($xml[0]) )
|
||||
{
|
||||
$xml = [$xml];
|
||||
$start = 0;
|
||||
}
|
||||
|
||||
foreach( $xml as $data )
|
||||
{
|
||||
$name = $data['name'] ?? '';
|
||||
$attr = $data['attr'] ?? [];
|
||||
$content = $data['content'] ?? '';
|
||||
$child = $data['child'] ?? '';
|
||||
|
||||
$output .= "$tab<$name".Singleton::class('ZN\Hypertext\Html')->attributes($attr).">";
|
||||
|
||||
if( ! empty($content) )
|
||||
{
|
||||
$output .= $content;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( ! empty($child) )
|
||||
{
|
||||
$output .= $eof.$this->_document($child, $tab, $start++).$tab;
|
||||
}
|
||||
else
|
||||
{
|
||||
$output .= $content;
|
||||
}
|
||||
}
|
||||
|
||||
$output .= "</".$name.">".$eof;
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php namespace ZN\XML;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
class Check
|
||||
{
|
||||
/**
|
||||
* Controls whether an XML document is valid.
|
||||
*
|
||||
* @param string $xml
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function check(string $xml) : bool
|
||||
{
|
||||
if( empty($xml) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
libxml_use_internal_errors(true);
|
||||
|
||||
simplexml_load_string($xml);
|
||||
|
||||
$return = ! (bool) libxml_get_errors();
|
||||
|
||||
libxml_clear_errors();
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php namespace ZN\XML\Exception;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Exception;
|
||||
|
||||
class FileNotFoundException extends Exception
|
||||
{
|
||||
const lang =
|
||||
[
|
||||
'en' => 'Error: `%` file was not found!',
|
||||
'tr' => 'Hata: `%` dosyasi bulunamadi!'
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php namespace ZN\XML;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Base;
|
||||
use ZN\XML\Exception\FileNotFoundException;
|
||||
|
||||
class Loader
|
||||
{
|
||||
/**
|
||||
* Loads an XML file.
|
||||
*
|
||||
* @param string $file
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function do(string $file) : string
|
||||
{
|
||||
$file = Base::suffix($file, '.xml');
|
||||
|
||||
if( is_file($file) )
|
||||
{
|
||||
return file_get_contents($file);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new FileNotFoundException(NULL, $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php namespace ZN\XML;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use stdClass;
|
||||
use SimpleXMLElement;
|
||||
|
||||
class Parser
|
||||
{
|
||||
/**
|
||||
* Parses an XML document.
|
||||
*
|
||||
* @param string $xml
|
||||
* @param string $result = 'object'
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function do(string $xml, string $result = 'object')
|
||||
{
|
||||
$xml = self::cleanXMLElement($xml);
|
||||
$parser = xml_parser_create();
|
||||
|
||||
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
|
||||
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
|
||||
xml_parse_into_struct($parser, $xml, $tags);
|
||||
xml_parser_free($parser);
|
||||
|
||||
$elements = [];
|
||||
$stack = [];
|
||||
|
||||
if( ! empty($tags) ) foreach( $tags as $tag )
|
||||
{
|
||||
|
||||
$index = count($elements);
|
||||
|
||||
if( $tag['type'] === 'complete' || $tag['type'] === 'open' )
|
||||
{
|
||||
if( $result === 'object' )
|
||||
{
|
||||
$elements[$index] = new stdClass;
|
||||
$elements[$index]->name = $tag['tag'] ?? NULL;
|
||||
$elements[$index]->content = $tag['value'] ?? NULL;
|
||||
$elements[$index]->attr = $tag['attributes'] ?? NULL;
|
||||
|
||||
if( $tag['type'] === 'open' )
|
||||
{
|
||||
$elements[$index]->child = [];
|
||||
$stack[count($stack)] = &$elements;
|
||||
$elements = &$elements[$index]->child;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$elements[$index] = [];
|
||||
$elements[$index]['name'] = $tag['tag'] ?? NULL;
|
||||
$elements[$index]['content'] = $tag['value'] ?? NULL;
|
||||
$elements[$index]['attr'] = $tag['attributes'] ?? NULL;
|
||||
|
||||
if( $tag['type'] === 'open' )
|
||||
{
|
||||
$elements[$index]['child'] = [];
|
||||
$stack[count($stack)] = &$elements;
|
||||
$elements = &$elements[$index]['child'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( $tag['type'] === 'close' )
|
||||
{
|
||||
$elements = &$stack[count($stack) - 1];
|
||||
unset($stack[count($stack) - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
return $elements[0] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an XML document.
|
||||
*
|
||||
* @param string $xml
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function array(string $data) : array
|
||||
{
|
||||
return self::do($data, 'array');
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an XML document.
|
||||
*
|
||||
* @param string $xml
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function json(string $data) : string
|
||||
{
|
||||
return json_encode(self::do($data, 'array'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an XML document.
|
||||
*
|
||||
* @param string $xml
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public static function object(string $data)
|
||||
{
|
||||
return self::do($data, 'object');
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple XML Element
|
||||
*
|
||||
* @param string $data
|
||||
* @param int $option = 0
|
||||
*
|
||||
* @return SimpleXMLElement
|
||||
*/
|
||||
public static function simple(string $data, int $option = 0)
|
||||
{
|
||||
$data = self::cleanXMLElement($data);
|
||||
|
||||
return new SimpleXMLElement($data, $option);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple URL XML Element
|
||||
*
|
||||
* @param string $data
|
||||
* @param int $option = 0
|
||||
*
|
||||
* @return SimpleXMLElement
|
||||
*/
|
||||
public static function simpleURL(string $data, int $option = 0)
|
||||
{
|
||||
return new SimpleXMLElement($data, $option, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Private clean XML element
|
||||
*/
|
||||
private static function cleanXMLElement($xml)
|
||||
{
|
||||
return preg_replace('/<\?xml(.*?)>/', '', $xml ?? '');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php namespace ZN\XML;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Controller\Factory;
|
||||
|
||||
class Processor extends Factory
|
||||
{
|
||||
const factory =
|
||||
[
|
||||
'methods' =>
|
||||
[
|
||||
'version' => 'Builder::version:this',
|
||||
'encoding' => 'Builder::encoding:this',
|
||||
'build' => 'Builder::do',
|
||||
'save' => 'Save::do',
|
||||
'load' => 'Loader::do',
|
||||
'parse' => 'Parser::do',
|
||||
'parsearray' => 'Parser::array',
|
||||
'parsejson' => 'Parser::json',
|
||||
'parseobject' => 'Parser::object',
|
||||
'parsesimple' => 'Parser::simple',
|
||||
'parsesimpleurl' => 'Parser::simpleURL',
|
||||
'check' => 'Check::check',
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php namespace ZN\XML;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Base;
|
||||
|
||||
class Save
|
||||
{
|
||||
/**
|
||||
* Keeps XML file extension
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $extension = '.xml';
|
||||
|
||||
/**
|
||||
* It saves an XML document.
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function do(string $file, string $data) : bool
|
||||
{
|
||||
$file = Base::suffix($file, self::$extension);
|
||||
|
||||
return file_put_contents($file, $data);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user