diff --git a/Utils/Parser/Php/ArrayParser.php b/Utils/Parser/Php/ArrayParser.php index 0d63cb5b4..14b266fcc 100644 --- a/Utils/Parser/Php/ArrayParser.php +++ b/Utils/Parser/Php/ArrayParser.php @@ -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(); + } + } } diff --git a/Utils/Parser/Php/ClassParser.php b/Utils/Parser/Php/ClassParser.php deleted file mode 100644 index 2ae53ae41..000000000 --- a/Utils/Parser/Php/ClassParser.php +++ /dev/null @@ -1,688 +0,0 @@ -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; - } - -} \ No newline at end of file diff --git a/Utils/Parser/Php/ClassType.php b/Utils/Parser/Php/ClassType.php deleted file mode 100644 index 49fd53eff..000000000 --- a/Utils/Parser/Php/ClassType.php +++ /dev/null @@ -1,36 +0,0 @@ -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; - } -} \ No newline at end of file diff --git a/Utils/Parser/Php/MemberParser.php b/Utils/Parser/Php/MemberParser.php deleted file mode 100644 index b82ed9894..000000000 --- a/Utils/Parser/Php/MemberParser.php +++ /dev/null @@ -1,251 +0,0 @@ -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(); - } - } -} \ No newline at end of file diff --git a/Utils/Parser/Php/Visibility.php b/Utils/Parser/Php/Visibility.php deleted file mode 100644 index 955911246..000000000 --- a/Utils/Parser/Php/Visibility.php +++ /dev/null @@ -1,37 +0,0 @@ -