Remove some parsers

This commit is contained in:
Dennis Eichhorn 2017-10-30 20:43:14 +01:00
parent 17c3c0ffd6
commit d9524dffe7
6 changed files with 29 additions and 1409 deletions

View File

@ -46,10 +46,38 @@ class ArrayParser
$key = '"' . $key . '"';
}
$stringify .= ' ' . $key . ' => ' . MemberParser::parseVariable($val) . ',' . PHP_EOL;
$stringify .= ' ' . $key . ' => ' . self::parseVariable($val) . ',' . PHP_EOL;
}
return $stringify . ']';
}
/**
* Serialize value.
*
* @param mixed $value Value to serialzie
*
* @return string
*
* @since 1.0.0
*/
public static function parseVariable($value) : string
{
if (is_array($value)) {
return ArrayParser::serializeArray($value) . PHP_EOL;
} elseif (is_string($value)) {
return '"' . $value . '"';
} elseif (is_scalar($value)) {
return (string) $value;
} elseif (is_null($value)) {
return 'null';
} elseif (is_bool($value)) {
return $value ? 'true' : 'false';
} elseif ($value instanceOf \Serializable) {
return self::parseVariable($value->serialize());
} else {
throw new \UnexpectedValueException();
}
}
}

View File

@ -1,688 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types = 1);
namespace phpOMS\Utils\Parser\Php;
/**
* Class parser class.
*
* Parsing/serializing classes, interfaces to and from php file
*
* @category Framework
* @package phpOMS\Utils\Parser
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class ClassParser
{
/**
* Indention.
*
* @var int
* @since 1.0.0
*/
/* public */ const INDENT = 4;
/**
* Is final?
*
* @var bool
* @since 1.0.0
*/
private $isFinal = false;
/**
* Is abstract?
*
* @var bool
* @since 1.0.0
*/
private $isAbstract = false;
/**
* Type.
*
* @var string
* @since 1.0.0
*/
private $type = ClassType::_CLASS;
/**
* Extends.
*
* @var string
* @since 1.0.0
*/
private $extends = '';
/**
* Namespace.
*
* @var null|string
* @since 1.0.0
*/
private $namespace = '';
/**
* Includes.
*
* @var array
* @since 1.0.0
*/
private $includes = [];
/**
* Requires.
*
* @var array
* @since 1.0.0
*/
private $requires = [];
/**
* Uses.
*
* @var array
* @since 1.0.0
*/
private $use = [];
/**
* Name.
*
* @var string
* @since 1.0.0
*/
private $name = '';
/**
* Implements.
*
* @var array
* @since 1.0.0
*/
private $implements = [];
/**
* Traits.
*
* @var array
* @since 1.0.0
*/
private $traits = [];
/**
* Members.
*
* @var MemberParser[]
* @since 1.0.0
*/
private $members = [];
/**
* Functions.
*
* @var FunctionParser[]
* @since 1.0.0
*/
private $functions = [];
/**
* Saving class to file.
*
* @param string $path Path
*
* @return void
*
* @since 1.0.0
*/
public function createFile(string $path) /* : void */
{
// todo: implement
}
/**
* Set final.
*
* @param bool $final Is final
*
* @return void
*
* @since 1.0.0
*/
public function setFinal(bool $final) /* : void */
{
$this->isFinal = $final;
}
/**
* Is final?
*
* @return bool
*
* @since 1.0.0
*/
public function isFinal() : bool
{
return $this->isFinal;
}
/**
* Set abstract.
*
* @param bool $abstract Is abstract
*
* @return void
*
* @since 1.0.0
*/
public function setAbstract(bool $abstract) /* : void */
{
$this->isAbstract = $abstract;
}
/**
* Is abstract?
*
* @return bool
*
* @since 1.0.0
*/
public function isAbstract() : bool
{
return $this->isAbstract;
}
/**
* Get type.
*
* @return string
*
* @since 1.0.0
*/
public function getType() : string
{
return $this->type;
}
/**
* Set type.
*
* Available types are ClassType::
*
* @param string $type Set type
*
* @return void
*
* @since 1.0.0
*/
public function setType(string $type) /* : void */
{
$this->type = $type;
}
/**
* Get extends.
*
* @return string
*
* @since 1.0.0
*/
public function getExtends() : string
{
return $this->extends;
}
/**
* Set extends.
*
* @param string $extends Extended class
*
* @return void
*
* @since 1.0.0
*/
public function setExtends(string $extends) /* : void */
{
$this->extends = $extends;
}
/**
* Remove extends.
*
* @return void
*
* @since 1.0.0
*/
public function removeExtends() /* : void */
{
$this->extends = '';
}
/**
* Get namespace.
*
* @return string
*
* @since 1.0.0
*/
public function getNamespace() : string
{
return $this->namespace;
}
/**
* Set namespace.
*
* @param string $namespace Namespace
*
* @return void
*
* @since 1.0.0
*/
public function setNamespace(string $namespace) /* : void */
{
$this->namespace = $namespace;
}
/**
* Remove namespace.
*
* @return void
*
* @since 1.0.0
*/
public function removeNamespace() /* : void */
{
$this->namespace = '';
}
/**
* Add use.
*
* @param string $namespace Namespace to use
* @param string $as Namespace as
*
* @return void
*
* @since 1.0.0
*/
public function addUse(string $namespace, string $as = null) /* : void */
{
if (isset($as)) {
$this->use[$as] = $namespace;
} else {
$this->use[] = $namespace;
}
}
/**
* Remove use.
*
* @param string $id Namespace numerical id or 'as' if used.
*
* @return bool
*
* @since 1.0.0
*/
public function removeUse($id) : bool
{
if (isset($this->use[$id])) {
unset($this->use[$id]);
return true;
}
return false;
}
/**
* Get name.
*
* @return string
*
* @since 1.0.0
*/
public function getName() : string
{
return $this->name;
}
/**
* Set name.
*
* @param string $name Class name
*
* @return void
*
* @since 1.0.0
*/
public function setName(string $name) /* : void */
{
$this->name = $name;
}
/**
* Add implements.
*
* @param string $implements Implement
*
* @return void
*
* @since 1.0.0
*/
public function addImplements(string $implements) /* : void */
{
$this->implements[] = $implements;
array_unique($this->implements);
}
/**
* Add include.
*
* @param string $include Include
*
* @return void
*
* @since 1.0.0
*/
public function addInclude(string $include) /* : void */
{
$this->includes[] = $include;
array_unique($this->includes);
}
/**
* Add $require.
*
* @param string $require Require
*
* @return void
*
* @since 1.0.0
*/
public function addRequire(string $require) /* : void */
{
$this->requires[] = $require;
array_unique($this->requires);
}
/**
* Add trait.
*
* @param string $trait Trait to use
* @param string $as Trait as
*
* @return void
*
* @since 1.0.0
*/
public function addTrait(string $trait, string $as = null) /* : void */
{
if (isset($as)) {
$this->traits[$as] = $trait;
} else {
$this->traits[] = $trait;
}
}
/**
* Remove trait.
*
* @param string $id Namespace numerical id or 'as' if used.
*
* @return bool
*
* @since 1.0.0
*/
public function removeTrait($id) : bool
{
if (isset($this->traits[$id])) {
unset($this->traits[$id]);
return true;
}
return false;
}
/**
* Add member.
*
* @param MemberParser $member Member
*
* @return bool
*
* @since 1.0.0
*/
public function addMember(MemberParser $member) /* : void */
{
$this->members[$member->getName()] = $member;
}
/**
* Remove member by name.
*
* @param string $name Member name
*
* @return bool
*
* @since 1.0.0
*/
public function removeMember(string $name) : bool
{
if (isset($this->members[$name])) {
unset($this->members[$name]);
return true;
}
return false;
}
/**
* Get member by name.
*
* @param string $name Member name
*
* @return MemberParser
*
* @since 1.0.0
*/
public function getMember(string $name) : MemberParser
{
return $this->members[$name] ?? new MemberParser();
}
/**
* Add function.
*
* @param FunctionParser $function Function
*
* @return bool
*
* @since 1.0.0
*/
public function addFunction(FunctionParser $function)
{
$this->functions[$function->getName()] = $function;
}
/**
* Remove function by name.
*
* @param string $name Function name
*
* @return bool
*
* @since 1.0.0
*/
public function removeFunction(string $name) : bool
{
if (isset($this->functions[$name])) {
unset($this->functions[$name]);
return true;
}
return false;
}
/**
* Get function by name.
*
* @param string $name Function name
*
* @return FunctionParser
*
* @since 1.0.0
*/
public function getFunction(string $name) : FunctionParser
{
return $this->functions[$name] ?? new FunctionParser();
}
/**
* Serialize class.
*
* @return string
*
* @since 1.0.0
*/
public function serialize() : string
{
$class = '';
$class .= $this->serializeRequire('require_once', $this->requires);
$class .= $this->serializeRequire('include_once', $this->includes);
$class .= $this->serializeNamespace();
$class .= $this->serializeUse($this->use);
$class .= $this->serializeClass();
$class .= '{' . PHP_EOL . PHP_EOL;
$class .= $this->serializeUse($this->traits);
foreach ($this->members as $name => $member) {
$class .= $member->serialize() . PHP_EOL . PHP_EOL;
}
foreach ($this->functions as $name => $function) {
$class .= $function->serialize() . PHP_EOL . PHP_EOL;
}
$class .= '}';
return $class;
}
/**
* Serialize require.
*
* @param string $keyword Keyword (e.g. include, require, include_once)
* @param array $source Require source
*
* @return string
*
* @since 1.0.0
*/
private function serializeRequire(string $keyword, array $source) : string
{
$serialze = '';
if (!empty($source)) {
foreach ($source as $require) {
$serialze .= $keyword . ' "' . $require . '";' . PHP_EOL;
}
$serialze .= PHP_EOL;
}
return $serialze;
}
/**
* Serialize namespace.
*
* @return string
*
* @since 1.0.0
*/
private function serializeNamespace() : string
{
$serialze = '';
if (!empty($this->namespace)) {
$serialze = $this->namespace . ';' . PHP_EOL . PHP_EOL;
}
return $serialze;
}
/**
* Serialize use.
*
* @param array $source Use source
*
* @return string
*
* @since 1.0.0
*/
private function serializeUse(array $source) : string
{
$serialze = '';
if (!empty($source)) {
foreach ($source as $as => $use) {
$serialze .= 'use ' . $use . (is_string($as) ? ' as ' . $as : '') . ';' . PHP_EOL;
}
$serialze .= PHP_EOL;
}
return $serialze;
}
/**
* Serialize class.
*
* @return string
*
* @since 1.0.0
*/
private function serializeClass() : string
{
$serialze = '';
if ($this->isFinal) {
$serialze .= 'final ';
}
if ($this->isAbstract) {
$serialze .= 'abstract ';
}
$serialze .= $this->type . ' ' . $this->name . ' ';
if (!empty($this->extends)) {
$serialze .= 'extends ' . $this->extends . ' ';
}
if (!empty($this->implements)) {
$serialze .= 'implements ' . implode(', ', $this->implements) . PHP_EOL;
}
return $serialze;
}
}

View File

@ -1,36 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types = 1);
namespace phpOMS\Utils\Parser\Php;
use phpOMS\Stdlib\Base\Enum;
/**
* Database type enum.
*
* Database types that are supported by the application
*
* @category Framework
* @package phpOMS\Utils\Parser
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
abstract class ClassType extends Enum
{
/* public */ const _CLASS = 'class';
/* public */ const _TRAIT = 'trait';
/* public */ const _INTERFACE = 'interface';
}

View File

@ -1,396 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types = 1);
namespace phpOMS\Utils\Parser\Php;
/**
* Member parser class.
*
* Parsing/serializing functions
*
* @category Framework
* @package phpOMS\Utils\Parser
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class FunctionParser
{
/**
* Function name.
*
* @var string
* @since 1.0.0
*/
private $name = '';
/**
* Function visibility.
*
* @var string
* @since 1.0.0
*/
private $visibility = Visibility::_PUBLIC;
/**
* Is static?
*
* @var bool
* @since 1.0.0
*/
private $isStatic = false;
/**
* Is abstract?
*
* @var bool
* @since 1.0.0
*/
private $isAbstract = false;
/**
* Is final?
*
* @var bool
* @since 1.0.0
*/
private $isFinal = false;
/**
* Return type.
*
* @var string
* @since 1.0.0
*/
private $return = null;
/**
* Parameters.
*
* @var array
* @since 1.0.0
*/
private $parameters = [];
/**
* Function body.
*
* @var string
* @since 1.0.0
*/
private $body = '';
/**
* Get function name.
*
* @return string
*
* @since 1.0.0
*/
public function getName() : string
{
return $this->name;
}
/**
* Set function name.
*
* @param string $name Function name
*
* @return void
*
* @since 1.0.0
*/
public function setName(string $name) /* : void */
{
$this->name = $name;
}
/**
* Set function body.
*
* @param string $body Function body
*
* @return void
*
* @since 1.0.0
*/
public function seBody(string $body) /* : void */
{
$this->body = $body;
}
/**
* Get function body.
*
* @return string
*
* @since 1.0.0
*/
public function getBody() : string
{
return $this->body;
}
/**
* Remove body.
*
* @return void
*
* @since 1.0.0
*/
public function removeBody() /* : void */
{
$this->body = '';
}
/**
* Get function visibility.
*
* @return string
*
* @since 1.0.0
*/
public function getVisibility() : string
{
return $this->visibility;
}
/**
* Set visibility.
*
* @param string $visibility Function visibility
*
* @return void
*
* @since 1.0.0
*/
public function setVisibility(string $visibility) /* : void */
{
$this->visibility = $visibility;
}
/**
* Set static.
*
* @param bool $static Is static
*
* @return void
*
* @since 1.0.0
*/
public function setStatic(bool $static) /* : void */
{
$this->isStatic = $static;
}
/**
* Is static?
*
* @return bool
*
* @since 1.0.0
*/
public function isStatic() : bool
{
return $this->isStatic;
}
/**
* Set final.
*
* @param bool $final Is final
*
* @return void
*
* @since 1.0.0
*/
public function setFinal(bool $final) /* : void */
{
$this->isFinal = $final;
}
/**
* Is final?
*
* @return bool
*
* @since 1.0.0
*/
public function isFinal() : bool
{
return $this->isFinal;
}
/**
* Set abstract.
*
* @param bool $abstract Is abstract
*
* @return void
*
* @since 1.0.0
*/
public function setAbstract(bool $abstract) /* : void */
{
$this->isAbstract = $abstract;
if ($this->isAbstract) {
$this->body = null;
} elseif (!$this->isAbstract && !isset($this->body)) {
$this->body = '';
}
}
/**
* Is abstract?
*
* @return bool
*
* @since 1.0.0
*/
public function isAbstract() : bool
{
return $this->isAbstract;
}
/**
* Remove return type.
*
* @return void
*
* @since 1.0.0
*/
public function removeReturn() /* : void */
{
$this->return = null;
}
/**
* Get return type.
*
* @return string
*
* @since 1.0.0
*/
public function getReturn() : string
{
return $this->return;
}
/**
* Set return type.
*
* @param string $return Return type
*
* @return void
*
* @since 1.0.0
*/
public function setReturn(string $return) /* : void */
{
$this->return = $return;
}
/**
* Add parameter to function.
*
* @param string $name Parameter name
* @param string $typehint Typehint
* @param string $default Default value
*
* @return void
*
* @since 1.0.0
*/
public function addParameter(string $name, string $typehint = null, string $default = null) /* : void */
{
$this->parameters[$name]['name'] = $name;
$this->parameters[$name]['typehint'] = $typehint;
if (isset($default)) {
if ($default === 'null') {
$default = null;
}
$this->parameters[$name]['default'] = $default;
}
}
/**
* Serialize function.
*
* @return string
*
* @since 1.0.0
*/
public function serialize()
{
$function = '';
$function .= str_repeat(' ', ClassParser::INDENT);
if ($this->isFinal) {
$function .= 'final ';
}
if ($this->isAbstract) {
$function .= 'abstract ';
}
$function .= $this->visibility . ' ';
if ($this->isStatic) {
$function .= 'static ';
}
$function .= 'function ' . $this->name . '(';
$parameters = '';
foreach ($this->parameters as $name => $para) {
$parameters = (isset($para['typehint']) ? $para['typehint'] . ' ' : '') . $para['name'] . (array_key_exists('default', $para) ? ' = ' . MemberParser::parseVariable($para['default']) : '') . ', ';
}
$function .= rtrim($parameters, ', ') . ') ';
$function .= ($this->return ?? '') . PHP_EOL;
if (isset($this->body)) {
$function .= str_repeat(' ', ClassParser::INDENT) . '{' . PHP_EOL . $this->addIndent($this->body) . PHP_EOL . str_repeat(' ', ClassParser::INDENT) . '}';
} else {
$function .= ';';
}
return $function;
}
/**
* Add indention for body.
*
* @param string $body Function body to indent
*
* @return string
*
* @since 1.0.0
*/
private function addIndent(string $body) : string
{
$body = preg_split('/\r\n|\r|\n/', $body);
foreach ($body as &$line) {
$line = str_repeat(' ', ClassParser::INDENT) . $line;
}
return $body;
}
}

View File

@ -1,251 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types = 1);
namespace phpOMS\Utils\Parser\Php;
/**
* Member parser class.
*
* Parsing/serializing member variables
*
* @category Framework
* @package phpOMS\Utils\Parser
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
class MemberParser
{
/**
* Member name.
*
* @var string
* @since 1.0.0
*/
private $name = '';
/**
* Member visibility.
*
* @var string
* @since 1.0.0
*/
private $visibility = Visibility::_PUBLIC;
/**
* Is static.
*
* @var bool
* @since 1.0.0
*/
private $isStatic = false;
/**
* Is const.
*
* @var bool
* @since 1.0.0
*/
private $isConst = false;
/**
* Default value.
*
* @var mixed
* @since 1.0.0
*/
private $default = null;
/**
* Get member name.
*
* @return string
*
* @since 1.0.0
*/
public function getName() : string
{
return $this->name;
}
/**
* Set member name.
*
* @param string $name Member name
*
* @return void
*
* @since 1.0.0
*/
public function setName(string $name) /* : void */
{
$this->name = $name;
}
/**
* Get visibility.
*
* @return string
*
* @since 1.0.0
*/
public function getVisibility() : string
{
return $this->visibility;
}
/**
* Set visibility.
*
* @param string $visibility Member visibility
*
* @return void
*
* @since 1.0.0
*/
public function setVisibility(string $visibility) /* : void */
{
$this->visibility = $visibility;
}
/**
* Set static.
*
* @param bool $static Is static
*
* @return void
*
* @since 1.0.0
*/
public function setStatic(bool $static) /* : void */
{
$this->isStatic = $static;
if ($this->isStatic) {
$this->isConst = false;
}
}
/**
* Is static?
*
* @return bool
*
* @since 1.0.0
*/
public function isStatic() : bool
{
return $this->isStatic;
}
/**
* Set const.
*
* @param bool $const Is const
*
* @return void
*
* @since 1.0.0
*/
public function setConst(bool $const) /* : void */
{
$this->isConst = $const;
if ($this->isConst) {
$this->isStatic = false;
}
}
/**
* Is const?
*
* @return bool
*
* @since 1.0.0
*/
public function isConst() : bool
{
return $this->isConst;
}
/**
* Set default value.
*
* @param string $default
*
* @return void
*
* @since 1.0.0
*/
public function setDefault($default) /* : void */
{
$this->default = $default;
}
/**
* Serialize member.
*
* @return string
*
* @since 1.0.0
*/
public function serialize() : string
{
$member = '';
$member .= str_repeat(' ', ClassParser::INDENT);
$member .= $this->visibility . ' ';
if ($this->isStatic) {
$member .= 'static ';
}
if ($this->isConst) {
$member .= 'const ';
}
$member .= (!$this->isConst ? '$' : '') . $this->name . ' = ' . self::parseVariable($this->default) . ';';
return $member;
}
/**
* Serialize value.
*
* @param mixed $value Value to serialzie
*
* @return string
*
* @since 1.0.0
*/
public static function parseVariable($value) : string
{
if (is_array($value)) {
return ArrayParser::serializeArray($value) . PHP_EOL;
} elseif (is_string($value)) {
return '"' . $value . '"';
} elseif (is_scalar($value)) {
return (string) $value;
} elseif (is_null($value)) {
return 'null';
} elseif (is_bool($value)) {
return $value ? 'true' : 'false';
} elseif ($value instanceOf \Serializable) {
return self::parseVariable($value->serialize());
} else {
throw new \UnexpectedValueException();
}
}
}

View File

@ -1,37 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.1
*
* @category TBD
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://orange-management.com
*/
declare(strict_types = 1);
namespace phpOMS\Utils\Parser\Php;
use phpOMS\Stdlib\Base\Enum;
/**
* Visibility type enum.
*
* Visibility for member variables and functions
*
* @category Framework
* @package phpOMS\Utils\Parser
* @license OMS License 1.0
* @link http://orange-management.com
* @since 1.0.0
*/
abstract class Visibility extends Enum
{
/* public */ const _NONE = '';
/* public */ const _PUBLIC = 'public';
/* public */ const _PRIVATE = 'private';
/* public */ const _PROTECTED = 'protected';
}