| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 | <?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 LuMethodDocParser
{
    /**
     * @var string
     */
    private $_doc;
    /**
     * @return string
     */
    public function getSummary()
    {
        return $this->_summary;
    }
    /**
     * @var string
     */
    private $_summary = "";
    /**
     * @var LuParameterDbo[]
     */
    private $_params = [];
    /**
     * @var string
     */
    private $_returnType = "";
    /**
     * @return string
     */
    public function getDoc()
    {
        return $this->_doc;
    }
    /**
     * @return \Luticate\Utils\Dbo\LuParameterDbo[]
     */
    public function getParams()
    {
        return $this->_params;
    }
    /**
     * @return string
     */
    public function getReturnType()
    {
        return $this->_returnType;
    }
    /**
     * LuDocParser constructor.
     * @param $doc string
     */
    public function __construct($doc)
    {
        $this->_doc = $doc;
    }
    
    public function parse()
    {
        if (!is_string($this->_doc)) {
            return;
        }
        $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 = null;
        
        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 == "param") {
                        $currentParam = new LuParameterDbo();
                        $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");
                        }
                        $this->_params[$currentParam->getName()] = $currentParam;
                    }
                    else if ($command == "deprecated") {
                    }
                    else if ($command == "inheritdoc") {
                    }
                    else if ($command == "internal") {
                    }
                    else if ($command == "link") {
                    }
                    else if ($command == "return") {
                        $this->_returnType = $line;
                    }
                    else if ($command == "see") {
                    }
                    else if ($command == "throws") {
                    }
                    else if (!is_null($currentParam)) {
                        $methodName = $command;
                        if ($methodName == "nullable") {
                            $currentParam->setNotNull(false);
                        }
                        else {
                            $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 {
                    if (is_null($currentParam)) {
                        $this->_summary .= $line . "\n";
                    }
                    else {
                        $currentParam->setSummary($currentParam->getSummary() . $line . "\n");
                    }
                }
            }
        }
    }
}
 |