xaboy 5 年之前
父节点
当前提交
345fd72d7b
共有 2 个文件被更改,包括 47 次插入23 次删除
  1. 38 1
      src/Annotation/AnnotationReader.php
  2. 9 22
      src/FormHandle.php

+ 38 - 1
src/Annotation/AnnotationReader.php

@@ -17,13 +17,20 @@ use Doctrine\Common\Annotations\AnnotationRegistry;
 use Doctrine\Common\Annotations\AnnotationReader as Render;
 use FormBuilder\Contract\AnnotationInterface;
 use FormBuilder\FormHandle;
+use FormBuilder\Util;
 
 class AnnotationReader
 {
     protected static $isInit = false;
 
+    /**
+     * @var Render
+     */
     protected $annotationReader;
 
+    /**
+     * @var FormHandle
+     */
     protected $handle;
 
     public function __construct(FormHandle $handle)
@@ -43,10 +50,40 @@ class AnnotationReader
     }
 
     /**
+     * @return array
+     * @throws \ReflectionException
+     */
+    public function render()
+    {
+        $reflectionClass = new \ReflectionClass($this->handle);
+        $methods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
+        $rule = [];
+        $except = $this->handle->getExcept();
+        foreach ($methods as $method) {
+            $field = preg_replace('/^(.+)(Field|_field)$/', '$1', $method->name);
+            $value = null;
+            if ($field != $method->name && !in_array($field, $except)) {
+                $params = $method->getParameters();
+                if (isset($params[0]) && ($dep = $params[0]->getClass())) {
+                    if (in_array('FormBuilder\\Contract\\FormComponentInterface', $dep->getInterfaceNames())) {
+                        $componentClass = $dep->getName();
+                        $value = $method->invokeArgs($this->handle, [new $componentClass($field, $this->handle->getFieldTitle($field))]);
+                    }
+                }
+                if (is_null($value)) $value = $method->invoke($this->handle);
+                if (!is_null($value) && (($isArray = is_array($value)) || Util::isComponent($value))) {
+                    $rule[] = compact('value', 'method', 'isArray');
+                }
+            }
+        }
+        return $this->parse($rule);
+    }
+
+    /**
      * @param $rules
      * @return array
      */
-    public function parse($rules)
+    protected function parse($rules)
     {
         $formRule = [];
         $groupList = [];

+ 9 - 22
src/FormHandle.php

@@ -48,6 +48,11 @@ abstract class FormHandle implements FormHandleInterface
      */
     abstract public function ui();
 
+    final public function getExcept()
+    {
+        return $this->except;
+    }
+
     /**
      * 获取表单数据
      * @return array
@@ -86,34 +91,15 @@ abstract class FormHandle implements FormHandleInterface
      */
     protected function getFormRule()
     {
-        $reflectionClass = new \ReflectionClass($this);
-        $methods = $reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC);
-        $rule = [];
-        foreach ($methods as $method) {
-            $field = preg_replace('/^(.+)(Field|_field)$/', '$1', $method->name);
-            $value = null;
-            if ($field != $method->name && !in_array($field, $this->except)) {
-                $params = $method->getParameters();
-                if (isset($params[0]) && ($dep = $params[0]->getClass())) {
-                    if (in_array('FormBuilder\\Contract\\FormComponentInterface', $dep->getInterfaceNames())) {
-                        $componentClass = $dep->getName();
-                        $value = $method->invokeArgs($this, [new $componentClass($field, $this->getFieldTitle($field))]);
-                    }
-                }
-                if (is_null($value)) $value = $method->invoke($this);
-                if (!is_null($value) && (($isArray = is_array($value)) || Util::isComponent($value))) {
-                    $rule[] = compact('value', 'method', 'isArray');
-                }
-            }
-        }
         $render = new AnnotationReader($this);
-        return $render->parse($rule);
+        return $render->render();
     }
 
     /**
      * 创建表单
      *
-     * @return  Form
+     * @return Form
+     * @throws \ReflectionException
      */
     protected function createForm()
     {
@@ -123,6 +109,7 @@ abstract class FormHandle implements FormHandleInterface
 
     /**
      * @return array
+     * @throws \ReflectionException
      */
     protected function getParams()
     {