1+ <?php
2+
3+ namespace PHPZlc \Document ;
4+
5+ use Doctrine \Common \Annotations \Annotation \Enum ;
6+
7+ class Document
8+ {
9+ private static $ documents = array ();
10+
11+ private static $ item = 0 ;
12+
13+ public function add ()
14+ {
15+ $ debug_backtrace = debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS , 2 )[1 ];
16+
17+ return $ this ->set ('physical_address ' , $ debug_backtrace ['class ' ] . ':: ' . $ debug_backtrace ['function ' ] . ' ' . $ debug_backtrace ['line ' ] . '行 ' );
18+ }
19+
20+ /**
21+ * Api设置
22+ *
23+ * @param array $config
24+ * config_item:
25+ * is_hide : 是否隐藏
26+ * is_host : 是否本域
27+ * @return $this
28+ * @throws \Exception
29+ */
30+ public function config ($ config = array ())
31+ {
32+ foreach ($ config as $ key => $ value ){
33+ if (!in_array ($ key , array ('is_hide ' , 'is_host ' ))){
34+ $ this ->Exception ('config_item ' , $ key , '设置项 "is_hide", "is_host" 中选择 ' );
35+ }
36+
37+ if ($ value !== true && $ value !== false ){
38+ $ this ->Exception ($ key , $ value , '设置项可在 "true", "false" 中选择 ' );
39+ }
40+
41+ $ this ->set ($ key , $ value );
42+ }
43+
44+ return $ this ;
45+ }
46+
47+ public function generate ()
48+ {
49+ if (!array_key_exists ('title ' , $ this ->getLocalDocument ()) || empty ($ this ->getLocalDocument ()['title ' ])){
50+ $ this ->Exception ('title ' , '' , '标题不能为空 ' );
51+ }
52+
53+ if (!array_key_exists ('url ' , $ this ->getLocalDocument ()) || empty ($ this ->getLocalDocument ()['url ' ])){
54+ $ this ->Exception ('url ' , '' , ' 请求地址不能为空 ' );
55+ }
56+
57+ if (!array_key_exists ('method ' , $ this ->getLocalDocument ())){
58+ $ this ->setMethod ('get ' );
59+ }
60+
61+ if (!array_key_exists ('return_type ' , $ this ->getLocalDocument ())){
62+ $ this ->setReturnType ('json ' );
63+ }
64+
65+ if (!array_key_exists ('is_hide ' , $ this ->getLocalDocument ())){
66+ $ this ->set ('is_hide ' , false );
67+ }
68+
69+ if (!array_key_exists ('is_host ' , $ this ->getLocalDocument ())){
70+ $ this ->set ('is_host ' , true );
71+ }
72+
73+ if (!array_key_exists ('group ' , $ this ->getLocalDocument ())){
74+ $ this ->setGroup ('' );
75+ }
76+
77+ if (!array_key_exists ('explain ' , $ this ->getLocalDocument ())){
78+ $ this ->setExplain ('' );
79+ }
80+
81+ if (!array_key_exists ('return ' , $ this ->getLocalDocument ())){
82+ $ this ->setReturn ('' );
83+ }
84+
85+ if (!array_key_exists ('param ' , $ this ->getLocalDocument ())){
86+ $ this ->set ('param ' , array ());
87+ }else {
88+ $ params = $ this ->get ('param ' );
89+
90+ foreach ($ params as &$ param )
91+ {
92+ if (empty ($ param ['method ' ])){
93+ $ param ['method ' ] = $ this ->get ('method ' );
94+ }
95+ }
96+
97+ $ this ->set ('param ' , $ params );
98+ }
99+
100+ self ::$ item ++;
101+ }
102+
103+ public function set ($ key , $ value )
104+ {
105+ self ::$ documents [self ::$ item ][$ key ] = $ value ;
106+
107+ return $ this ;
108+ }
109+
110+ private function get ($ key )
111+ {
112+ return self ::$ documents [self ::$ item ][$ key ];
113+ }
114+
115+ private function getLocalDocument ()
116+ {
117+ return self ::$ documents [self ::$ item ];
118+ }
119+
120+ public function getDocuments ()
121+ {
122+ return self ::$ documents ;
123+ }
124+
125+ /**
126+ * 抛出异常
127+ *
128+ * @param $key
129+ * @param $value
130+ * @param $explain
131+ * @throws \Exception
132+ */
133+ private function Exception ($ key , $ value , $ explain )
134+ {
135+ throw new \Exception ($ this ->get ('physical_address ' ) .' Param ' . $ key .' : " ' . $ value . '" 出错。 ' . $ explain . '。 ' );
136+ }
137+
138+ /**
139+ * 设置标题
140+ *
141+ * @param string $title
142+ * @return $this
143+ */
144+ public function setTitle ($ title )
145+ {
146+ return $ this ->set ('title ' , $ title );
147+ }
148+
149+ /**
150+ * 设置分组
151+ *
152+ * @param string $group 分组用/分离 例如 用户端/登陆模块
153+ * @return $this
154+ */
155+ public function setGroup ($ group )
156+ {
157+ return $ this ->set ('group ' , $ group );
158+ }
159+
160+ /**
161+ * 设置请求地址
162+ *
163+ * @param string $url 如果是拼接地址则以/开头 例如 /service/area/all
164+ * @return $this
165+ */
166+ public function setUrl ($ url )
167+ {
168+ return $ this ->set ('url ' , $ url );
169+ }
170+
171+ /**
172+ * 设置说明
173+ *
174+ * @param $explain
175+ * @return $this
176+ */
177+ public function setExplain ($ explain )
178+ {
179+ return $ this ->set ('explain ' , $ explain );
180+ }
181+ /**
182+ * 设置请求方式
183+ *
184+ * @param Enum({"get", "post"}) $method
185+ * @return $this
186+ * @throws \Exception
187+ */
188+ public function setMethod ($ method )
189+ {
190+ $ key = 'method ' ;
191+
192+ if (!in_array ($ method , array ('get ' , 'post ' ))){
193+ $ this ->Exception ($ key , $ method , '设置项可在 "get", "post" 中选择 ' );
194+ }
195+
196+ return $ this ->set ($ key , $ method );
197+ }
198+
199+ /**
200+ * 设置返回值类型
201+ *
202+ * @param Enum({"json", "file", "html", "xml", "image"}) 返回值类型
203+ * @return $this
204+ * @throws \Exception
205+ */
206+ public function setReturnType ($ return_type )
207+ {
208+ $ key = 'return_type ' ;
209+
210+ if (!in_array ($ return_type , array ("json " , "file " , "html " , "xml " , "image " ))){
211+ $ this ->Exception ($ key , $ return_type , '设置项可在 "json", "file", "html", "xml", "image" 中选择 ' );
212+ }
213+
214+ return $ this ->set ($ key , $ return_type );
215+ }
216+
217+ /**
218+ * 设置返回值
219+ *
220+ * @param $return
221+ * @return $this
222+ */
223+ public function setReturn ($ return )
224+ {
225+ return $ this ->set ('return ' , $ return );
226+ }
227+
228+ /**
229+ * 添加接口参数
230+ *
231+ * @param string $name 参数名
232+ * @param string $comment 注解
233+ * @param Enum({"string", "file", "json_array", "simple_array", "integer", "boolean", "text", 'url_param'}) $type 参数类型
234+ * 特别说明:
235+ * url_param: 路由参数 形如 http://host/symfony-dev/web/app_dev.php/{url_param}
236+ * @param bool $is_null 是为为空
237+ * @param string $default 默认值
238+ * @param string $explain 说明
239+ * @param string $method 参数请求方式 不传则取接口设置的请求方式
240+ * @return $this
241+ */
242+ public function addParam ($ name , $ comment = '' , $ type = 'string ' , $ is_null = false , $ default = '' , $ explain = '' , $ method = '' )
243+ {
244+ if ($ is_null !== true && $ is_null !== false ){
245+ $ this ->Exception ('param ' , $ name , '设置项 is_null 可在 "true", "false" 中选择 ' );
246+ }
247+
248+ if (!in_array ($ type , array ("string " , "file " , "json_array " , "simple_array " , "integer " , "boolean " , "text " , "url_param " ))){
249+ $ this ->Exception ('param ' , $ name , '设置项 type 可在 "file", "json_array", "simple_array", "integer", "boolean", "text", "url_param" 中选择 ' );
250+ }
251+
252+ if (!empty ($ method )){
253+ if (!in_array ($ method , array ('get ' , 'post ' ))){
254+ $ this ->Exception ('param ' , $ method , '设置项可在 "get", "post" 中选择 ' );
255+ }
256+ }
257+
258+ self ::$ documents [self ::$ item ]['param ' ][] = array (
259+ 'name ' => $ name ,
260+ 'type ' => $ type ,
261+ 'is_null ' => $ is_null ,
262+ 'default ' => $ default ,
263+ 'comment ' => $ comment ,
264+ 'explain ' => $ explain ,
265+ 'is_null_string ' => $ is_null ? 'YES ' : 'NO ' ,
266+ 'method ' => $ method
267+ );
268+
269+ return $ this ;
270+ }
271+
272+ public function addGetParam ($ name , $ comment = '' , $ type = 'string ' , $ is_null = false , $ default = '' , $ explain = '' )
273+ {
274+ return $ this ->addParam ($ name , $ comment , $ type , $ is_null , $ default , $ explain , 'get ' );
275+ }
276+
277+
278+ public function addPostParam ($ name , $ comment = '' , $ type = 'string ' , $ is_null = false , $ default = '' , $ explain = '' )
279+ {
280+ return $ this ->addParam ($ name , $ comment , $ type , $ is_null , $ default , $ explain , 'post ' );
281+ }
282+ }
0 commit comments