123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- /**
- * Created by PhpStorm.
- * User: robin
- * Date: 5/29/16
- * Time: 7:42 PM
- */
-
- namespace Luticate\Utils\Business;
-
- use Luticate\Utils\Dbo\LuParameterConstraintDbo;
- use Luticate\Utils\Dbo\LuParameterDbo;
-
- class LuPropertyDocParser
- {
- /**
- * LuDocParser constructor.
- * @param $doc string
- */
- public function __construct($doc)
- {
- $this->_doc = $doc;
- }
-
- /**
- * @return LuParameterDbo
- */
- public function parse()
- {
- if (!is_string($this->_doc)) {
- return null;
- }
- $lines = preg_split("/(\r?\n)/", $this->_doc);
- $count = count($lines);
- if ($count > 2) {
- array_splice($lines, 0, 1);
- array_splice($lines, $count - 2, 1);
- }
-
- $currentParam = new LuParameterDbo();
-
- foreach ($lines as $line) {
- $lineMatches = [];
- if (preg_match("/ *\\** *(.*) */", $line, $lineMatches) === 1) {
- $line = $lineMatches[1];
- $commandMatches = [];
- if (preg_match("/@([^ ]+) *(.*)/", $line, $commandMatches)) {
- $command = strtolower($commandMatches[1]);
- $line = $commandMatches[2];
- if ($command == "var") {
- $paramMatches = [];
- if (preg_match("/([^ ]+) *([^ ]+) *(.*)/", $line, $paramMatches) === 1) {
- if ($paramMatches[1][0] == "$") {
- $currentParam->setName(substr($paramMatches[1], 1, strlen($paramMatches[1]) - 1));
- $currentParam->setType($paramMatches[2]);
- }
- else {
- $currentParam->setName(substr($paramMatches[2], 1, strlen($paramMatches[2]) - 1));
- $currentParam->setType($paramMatches[1]);
- }
- $currentParam->setSummary($paramMatches[3] . "\n");
- }
- }
- else {
- $methodName = $command;
- $constraint = new LuParameterConstraintDbo();
- $constraint->setMethod($methodName);
- $args = [];
- $argMatches = [];
- if (preg_match_all('/ *(-?(?:\d*\.\d+|\d+|true|false|null|"[^"]*"|\'[^\']*\'))/', $line, $argMatches) !== false) {
- $args = $argMatches[1];
- foreach ($args as $key => $arg) {
- $argLower = strtolower($arg);
- if ($arg[0] == '"' || $arg[0] == "'") {
- $args[$key] = substr($arg, 1, count($arg) - 2);
- }
- else if ($argLower == "true") {
- $args[$key] = true;
- }
- else if ($argLower == "false") {
- $args[$key] = false;
- }
- else if ($argLower == "null") {
- $args[$key] = null;
- }
- else if (strpos($arg, ".") !== false) {
- $args[$key] = floatval($arg);
- }
- else {
- $args[$key] = intval($arg);
- }
- }
- }
- $constraint->setArguments($args);
- $currentParam->addConstraint($constraint);
- }
- }
- else {
- $currentParam->setSummary($currentParam->getSummary() . $line . "\n");
- }
- }
- }
- return $currentParam;
- }
- }
|