Form.php 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. <?php
  2. namespace fast;
  3. use ArrayAccess;
  4. /**
  5. * 表单元素生成
  6. * @class Form
  7. * @package fast
  8. * @method mixed token() static token
  9. * @method mixed close() static 关闭一个HTML表单
  10. * @method mixed setModel(mixed $model) static 设置HTML表单模型
  11. * @method mixed open(array $options) static 打开一个新的HTML表单
  12. * @method mixed file(string $name, array $options = array()) static 表单file
  13. * @method mixed reset(string $name, array $options = array()) static 表单reset
  14. * @method mixed password(string $name, array $options = array()) static 表单password
  15. * @method mixed button(string $value = null, array $options = array()) static 表单button
  16. * @method mixed model(mixed $model ,array $options = array()) static 打开一个新的HTML表单模型
  17. * @method mixed submit(string $value = null, array $options = array()) static 表单submit提交
  18. * @method mixed text(string $name, string $value = null, array $options = []) static 表单text
  19. * @method mixed label(string $name, string $value = null, array $options = []) static label标签
  20. * @method mixed url(string $name, string $value = null, array $options = array()) static 表单url
  21. * @method mixed email(string $name, string $value = null, array $options = array()) static 表单email
  22. * @method mixed hidden(string $name, string $value = null, array $options = array()) static 表单hidden
  23. * @method mixed image(string $url, string $name = null, array $attributes = array()) static 表单image提交
  24. * @method mixed textarea(string $name, string $value = null, array $options = array()) static 表单textarea
  25. * @method mixed checkbox(string $name, string $value = 1, string $checked = null, array $options = array()) static 表单checkbox
  26. * @method mixed radio(string $name, string $value = null, string $checked = null, array $options = array())) static 表单radio
  27. * @method mixed select(string $name, array $list = array(), string $selected = null, array $options = array()) static 表单textarea
  28. * @method mixed selectYear(string $name ,string $begin,string $end, string $selected = null ,array $options = array()) static select年
  29. * @method mixed selectMonth(string $name ,string $selected = null ,array $options = array() ,string $format = '%m') static select月
  30. */
  31. class Form
  32. {
  33. public function __construct()
  34. {
  35. }
  36. public static function __callStatic($name, $arguments)
  37. {
  38. return call_user_func_array([FormBuilder::instance(), $name], $arguments);
  39. }
  40. }
  41. /**
  42. *
  43. * 表单元素生成
  44. * @from https://github.com/illuminate/html
  45. * @package fast
  46. */
  47. class FormBuilder
  48. {
  49. /**
  50. * The CSRF token used by the form builder.
  51. *
  52. * @var string
  53. */
  54. protected $csrfToken = array('name' => '__token__');
  55. /**
  56. * The current model instance for the form.
  57. *
  58. * @var mixed
  59. */
  60. protected $model;
  61. /**
  62. * An array of label names we've created.
  63. *
  64. * @var array
  65. */
  66. protected $labels = array();
  67. /**
  68. * The reserved form open attributes.
  69. *
  70. * @var array
  71. */
  72. protected $reserved = array('method', 'url', 'route', 'action', 'files');
  73. /**
  74. * The form methods that should be spoofed, in uppercase.
  75. *
  76. * @var array
  77. */
  78. protected $spoofedMethods = array('DELETE', 'PATCH', 'PUT');
  79. /**
  80. * The types of inputs to not fill values on by default.
  81. *
  82. * @var array
  83. */
  84. protected $skipValueTypes = array('file', 'password', 'checkbox', 'radio');
  85. /**
  86. * Escape html
  87. * @var boolean
  88. */
  89. protected $escapeHtml = true;
  90. protected static $instance;
  91. /**
  92. * Create a new form builder instance.
  93. *
  94. * @return void
  95. */
  96. public function __construct()
  97. {
  98. }
  99. public static function instance($options = [])
  100. {
  101. if (is_null(self::$instance))
  102. {
  103. self::$instance = new static($options);
  104. }
  105. return self::$instance;
  106. }
  107. /**
  108. * Open up a new HTML form.
  109. *
  110. * @param array $options
  111. * @return string
  112. */
  113. public function open(array $options = array())
  114. {
  115. $method = array_get($options, 'method', 'post');
  116. // We need to extract the proper method from the attributes. If the method is
  117. // something other than GET or POST we'll use POST since we will spoof the
  118. // actual method since forms don't support the reserved methods in HTML.
  119. $attributes['method'] = $this->getMethod($method);
  120. $attributes['action'] = array_get($options, 'action', '');
  121. $attributes['accept-charset'] = 'UTF-8';
  122. // If the method is PUT, PATCH or DELETE we will need to add a spoofer hidden
  123. // field that will instruct the Symfony request to pretend the method is a
  124. // different method than it actually is, for convenience from the forms.
  125. $append = $this->getAppendage($method);
  126. if (isset($options['files']) && $options['files'])
  127. {
  128. $options['enctype'] = 'multipart/form-data';
  129. }
  130. // Finally we're ready to create the final form HTML field. We will attribute
  131. // format the array of attributes. We will also add on the appendage which
  132. // is used to spoof requests for this PUT, PATCH, etc. methods on forms.
  133. $attributes = array_merge(
  134. $attributes, array_except($options, $this->reserved)
  135. );
  136. // Finally, we will concatenate all of the attributes into a single string so
  137. // we can build out the final form open statement. We'll also append on an
  138. // extra value for the hidden _method field if it's needed for the form.
  139. $attributes = $this->attributes($attributes);
  140. return '<form' . $attributes . '>' . $append;
  141. }
  142. /**
  143. * Create a new model based form builder.
  144. *
  145. * @param mixed $model
  146. * @param array $options
  147. * @return string
  148. */
  149. public function model($model, array $options = array())
  150. {
  151. $this->model = $model;
  152. return $this->open($options);
  153. }
  154. /**
  155. * Set the model instance on the form builder.
  156. *
  157. * @param mixed $model
  158. * @return void
  159. */
  160. public function setModel($model)
  161. {
  162. $this->model = $model;
  163. }
  164. /**
  165. * Set the escape html mode
  166. * @param boolean $escape
  167. */
  168. public function setEscapeHtml($escape)
  169. {
  170. $this->escapeHtml = $escape;
  171. }
  172. /**
  173. * Escape HTML special characters in a string.
  174. * @return string
  175. */
  176. public function escape($value)
  177. {
  178. if (!$this->escapeHtml)
  179. {
  180. return $value;
  181. }
  182. if (is_array($value))
  183. {
  184. $value = json_encode($value, JSON_UNESCAPED_UNICODE);
  185. }
  186. return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
  187. }
  188. /**
  189. * Close the current form.
  190. *
  191. * @return string
  192. */
  193. public function close()
  194. {
  195. $this->labels = array();
  196. $this->model = null;
  197. return '</form>';
  198. }
  199. /**
  200. * Generate a hidden field with the current CSRF token.
  201. *
  202. * @return string
  203. */
  204. public function token($name = '__token__', $type = 'md5')
  205. {
  206. if (function_exists('token'))
  207. {
  208. return token($name, $type);
  209. }
  210. return '';
  211. }
  212. /**
  213. * Create a form label element.
  214. *
  215. * @param string $name
  216. * @param string $value
  217. * @param array $options
  218. * @return string
  219. */
  220. public function label($name, $value = null, $options = array())
  221. {
  222. $this->labels[] = $name;
  223. $options = $this->attributes($options);
  224. $value = $this->escape($this->formatLabel($name, $value));
  225. return '<label for="' . $name . '"' . $options . '>' . $value . '</label>';
  226. }
  227. /**
  228. * Format the label value.
  229. *
  230. * @param string $name
  231. * @param string|null $value
  232. * @return string
  233. */
  234. protected function formatLabel($name, $value)
  235. {
  236. return $value ? : ucwords(str_replace('_', ' ', $name));
  237. }
  238. /**
  239. * Create a form input field.
  240. *
  241. * @param string $type
  242. * @param string $name
  243. * @param string $value
  244. * @param array $options
  245. * @return string
  246. */
  247. public function input($type, $name, $value = null, $options = array())
  248. {
  249. if (!isset($options['name']))
  250. $options['name'] = $name;
  251. // We will get the appropriate value for the given field. We will look for the
  252. // value in the session for the value in the old input data then we'll look
  253. // in the model instance if one is set. Otherwise we will just use empty.
  254. $id = $this->getIdAttribute($name, $options);
  255. if (!in_array($type, $this->skipValueTypes))
  256. {
  257. $value = $this->getValueAttribute($name, $value);
  258. }
  259. // Once we have the type, value, and ID we can merge them into the rest of the
  260. // attributes array so we can convert them into their HTML attribute format
  261. // when creating the HTML element. Then, we will return the entire input.
  262. $merge = compact('type', 'value', 'id');
  263. $options = array_merge($options, $merge);
  264. return '<input' . $this->attributes($options) . '>';
  265. }
  266. /**
  267. * Create a text input field.
  268. *
  269. * @param string $name
  270. * @param string $value
  271. * @param array $options
  272. * @return string
  273. */
  274. public function text($name, $value = null, $options = array())
  275. {
  276. return $this->input('text', $name, $value, $options);
  277. }
  278. /**
  279. * Create a password input field.
  280. *
  281. * @param string $name
  282. * @param array $options
  283. * @return string
  284. */
  285. public function password($name, $options = array())
  286. {
  287. return $this->input('password', $name, '', $options);
  288. }
  289. /**
  290. * Create a hidden input field.
  291. *
  292. * @param string $name
  293. * @param string $value
  294. * @param array $options
  295. * @return string
  296. */
  297. public function hidden($name, $value = null, $options = array())
  298. {
  299. return $this->input('hidden', $name, $value, $options);
  300. }
  301. /**
  302. * Create an e-mail input field.
  303. *
  304. * @param string $name
  305. * @param string $value
  306. * @param array $options
  307. * @return string
  308. */
  309. public function email($name, $value = null, $options = array())
  310. {
  311. return $this->input('email', $name, $value, $options);
  312. }
  313. /**
  314. * Create a url input field.
  315. *
  316. * @param string $name
  317. * @param string $value
  318. * @param array $options
  319. * @return string
  320. */
  321. public function url($name, $value = null, $options = array())
  322. {
  323. return $this->input('url', $name, $value, $options);
  324. }
  325. /**
  326. * Create a file input field.
  327. *
  328. * @param string $name
  329. * @param array $options
  330. * @return string
  331. */
  332. public function file($name, $options = array())
  333. {
  334. return $this->input('file', $name, null, $options);
  335. }
  336. /**
  337. * Create a textarea input field.
  338. *
  339. * @param string $name
  340. * @param string $value
  341. * @param array $options
  342. * @return string
  343. */
  344. public function textarea($name, $value = null, $options = array())
  345. {
  346. if (!isset($options['name']))
  347. $options['name'] = $name;
  348. // Next we will look for the rows and cols attributes, as each of these are put
  349. // on the textarea element definition. If they are not present, we will just
  350. // assume some sane default values for these attributes for the developer.
  351. $options = $this->setTextAreaSize($options);
  352. $options['id'] = $this->getIdAttribute($name, $options);
  353. $value = (string) $this->getValueAttribute($name, $value);
  354. unset($options['size']);
  355. // Next we will convert the attributes into a string form. Also we have removed
  356. // the size attribute, as it was merely a short-cut for the rows and cols on
  357. // the element. Then we'll create the final textarea elements HTML for us.
  358. $options = $this->attributes($options);
  359. return '<textarea' . $options . '>' . $this->escape($value) . '</textarea>';
  360. }
  361. /**
  362. * Set the text area size on the attributes.
  363. *
  364. * @param array $options
  365. * @return array
  366. */
  367. protected function setTextAreaSize($options)
  368. {
  369. if (isset($options['size']))
  370. {
  371. return $this->setQuickTextAreaSize($options);
  372. }
  373. // If the "size" attribute was not specified, we will just look for the regular
  374. // columns and rows attributes, using sane defaults if these do not exist on
  375. // the attributes array. We'll then return this entire options array back.
  376. $cols = array_get($options, 'cols', 50);
  377. $rows = array_get($options, 'rows', 10);
  378. return array_merge($options, compact('cols', 'rows'));
  379. }
  380. /**
  381. * Set the text area size using the quick "size" attribute.
  382. *
  383. * @param array $options
  384. * @return array
  385. */
  386. protected function setQuickTextAreaSize($options)
  387. {
  388. $segments = explode('x', $options['size']);
  389. return array_merge($options, array('cols' => $segments[0], 'rows' => $segments[1]));
  390. }
  391. /**
  392. * Create a select box field.
  393. *
  394. * @param string $name
  395. * @param array $list
  396. * @param string $selected
  397. * @param array $options
  398. * @return string
  399. */
  400. public function select($name, $list = array(), $selected = null, $options = array())
  401. {
  402. // When building a select box the "value" attribute is really the selected one
  403. // so we will use that when checking the model or session for a value which
  404. // should provide a convenient method of re-populating the forms on post.
  405. $selected = $this->getValueAttribute($name, $selected);
  406. $options['id'] = $this->getIdAttribute($name, $options);
  407. if (!isset($options['name']))
  408. $options['name'] = $name;
  409. // We will simply loop through the options and build an HTML value for each of
  410. // them until we have an array of HTML declarations. Then we will join them
  411. // all together into one single HTML element that can be put on the form.
  412. $html = array();
  413. foreach ($list as $value => $display)
  414. {
  415. $html[] = $this->getSelectOption($display, $value, $selected);
  416. }
  417. // Once we have all of this HTML, we can join this into a single element after
  418. // formatting the attributes into an HTML "attributes" string, then we will
  419. // build out a final select statement, which will contain all the values.
  420. $options = $this->attributes($options);
  421. $list = implode('', $html);
  422. return "<select{$options}>{$list}</select>";
  423. }
  424. /**
  425. * Create a select range field.
  426. *
  427. * @param string $name
  428. * @param string $begin
  429. * @param string $end
  430. * @param string $selected
  431. * @param array $options
  432. * @return string
  433. */
  434. public function selectRange($name, $begin, $end, $selected = null, $options = array())
  435. {
  436. $range = array_combine($range = range($begin, $end), $range);
  437. return $this->select($name, $range, $selected, $options);
  438. }
  439. /**
  440. * Create a select year field.
  441. *
  442. * @param string $name
  443. * @param string $begin
  444. * @param string $end
  445. * @param string $selected
  446. * @param array $options
  447. * @return string
  448. */
  449. public function selectYear()
  450. {
  451. return call_user_func_array(array($this, 'selectRange'), func_get_args());
  452. }
  453. /**
  454. * Create a select month field.
  455. *
  456. * @param string $name
  457. * @param string $selected
  458. * @param array $options
  459. * @param string $format
  460. * @return string
  461. */
  462. public function selectMonth($name, $selected = null, $options = array(), $format = '%m')
  463. {
  464. $months = array();
  465. foreach (range(1, 12) as $month)
  466. {
  467. $months[$month] = strftime($format, mktime(0, 0, 0, $month, 1));
  468. }
  469. return $this->select($name, $months, $selected, $options);
  470. }
  471. /**
  472. * Get the select option for the given value.
  473. *
  474. * @param string $display
  475. * @param string $value
  476. * @param string $selected
  477. * @return string
  478. */
  479. public function getSelectOption($display, $value, $selected)
  480. {
  481. if (is_array($display))
  482. {
  483. return $this->optionGroup($display, $value, $selected);
  484. }
  485. return $this->option($display, $value, $selected);
  486. }
  487. /**
  488. * Create an option group form element.
  489. *
  490. * @param array $list
  491. * @param string $label
  492. * @param string $selected
  493. * @return string
  494. */
  495. protected function optionGroup($list, $label, $selected)
  496. {
  497. $html = array();
  498. foreach ($list as $value => $display)
  499. {
  500. $html[] = $this->option($display, $value, $selected);
  501. }
  502. return '<optgroup label="' . $this->escape($label) . '">' . implode('', $html) . '</optgroup>';
  503. }
  504. /**
  505. * Create a select element option.
  506. *
  507. * @param string $display
  508. * @param string $value
  509. * @param string $selected
  510. * @return string
  511. */
  512. protected function option($display, $value, $selected)
  513. {
  514. $selected = $this->getSelectedValue($value, $selected);
  515. $options = array('value' => $this->escape($value), 'selected' => $selected);
  516. return '<option' . $this->attributes($options) . '>' . $this->escape($display) . '</option>';
  517. }
  518. /**
  519. * Determine if the value is selected.
  520. *
  521. * @param string $value
  522. * @param string $selected
  523. * @return string
  524. */
  525. protected function getSelectedValue($value, $selected)
  526. {
  527. if (is_array($selected))
  528. {
  529. return in_array($value, $selected) ? 'selected' : null;
  530. }
  531. return ((string) $value == (string) $selected) ? 'selected' : null;
  532. }
  533. /**
  534. * Create a checkbox input field.
  535. *
  536. * @param string $name
  537. * @param mixed $value
  538. * @param bool $checked
  539. * @param array $options
  540. * @return string
  541. */
  542. public function checkbox($name, $value = 1, $checked = null, $options = array())
  543. {
  544. return $this->checkable('checkbox', $name, $value, $checked, $options);
  545. }
  546. /**
  547. * Create a radio button input field.
  548. *
  549. * @param string $name
  550. * @param mixed $value
  551. * @param bool $checked
  552. * @param array $options
  553. * @return string
  554. */
  555. public function radio($name, $value = null, $checked = null, $options = array())
  556. {
  557. if (is_null($value))
  558. $value = $name;
  559. return $this->checkable('radio', $name, $value, $checked, $options);
  560. }
  561. /**
  562. * Create a checkable input field.
  563. *
  564. * @param string $type
  565. * @param string $name
  566. * @param mixed $value
  567. * @param bool $checked
  568. * @param array $options
  569. * @return string
  570. */
  571. protected function checkable($type, $name, $value, $checked, $options)
  572. {
  573. $checked = $this->getCheckedState($type, $name, $value, $checked);
  574. if ($checked)
  575. $options['checked'] = 'checked';
  576. return $this->input($type, $name, $value, $options);
  577. }
  578. /**
  579. * Get the check state for a checkable input.
  580. *
  581. * @param string $type
  582. * @param string $name
  583. * @param mixed $value
  584. * @param bool $checked
  585. * @return bool
  586. */
  587. protected function getCheckedState($type, $name, $value, $checked)
  588. {
  589. switch ($type)
  590. {
  591. case 'checkbox':
  592. return $this->getCheckboxCheckedState($name, $value, $checked);
  593. case 'radio':
  594. return $this->getRadioCheckedState($name, $value, $checked);
  595. default:
  596. return $this->getValueAttribute($name) == $value;
  597. }
  598. }
  599. /**
  600. * Get the check state for a checkbox input.
  601. *
  602. * @param string $name
  603. * @param mixed $value
  604. * @param bool $checked
  605. * @return bool
  606. */
  607. protected function getCheckboxCheckedState($name, $value, $checked)
  608. {
  609. if (isset($this->session) && !$this->oldInputIsEmpty() && is_null($this->old($name)))
  610. return false;
  611. if ($this->missingOldAndModel($name))
  612. return $checked;
  613. $posted = $this->getValueAttribute($name);
  614. return is_array($posted) ? in_array($value, $posted) : (bool) $posted;
  615. }
  616. /**
  617. * Get the check state for a radio input.
  618. *
  619. * @param string $name
  620. * @param mixed $value
  621. * @param bool $checked
  622. * @return bool
  623. */
  624. protected function getRadioCheckedState($name, $value, $checked)
  625. {
  626. if ($this->missingOldAndModel($name))
  627. return $checked;
  628. return $this->getValueAttribute($name) == $value;
  629. }
  630. /**
  631. * Determine if old input or model input exists for a key.
  632. *
  633. * @param string $name
  634. * @return bool
  635. */
  636. protected function missingOldAndModel($name)
  637. {
  638. return (is_null($this->old($name)) && is_null($this->getModelValueAttribute($name)));
  639. }
  640. /**
  641. * Create a HTML reset input element.
  642. *
  643. * @param string $value
  644. * @param array $attributes
  645. * @return string
  646. */
  647. public function reset($value, $attributes = array())
  648. {
  649. return $this->input('reset', null, $value, $attributes);
  650. }
  651. /**
  652. * Create a HTML image input element.
  653. *
  654. * @param string $url
  655. * @param string $name
  656. * @param array $attributes
  657. * @return string
  658. */
  659. public function image($url, $name = null, $attributes = array())
  660. {
  661. $attributes['src'] = $url;
  662. return $this->input('image', $name, null, $attributes);
  663. }
  664. /**
  665. * Create a submit button element.
  666. *
  667. * @param string $value
  668. * @param array $options
  669. * @return string
  670. */
  671. public function submit($value = null, $options = array())
  672. {
  673. return $this->input('submit', null, $value, $options);
  674. }
  675. /**
  676. * Create a button element.
  677. *
  678. * @param string $value
  679. * @param array $options
  680. * @return string
  681. */
  682. public function button($value = null, $options = array())
  683. {
  684. if (!array_key_exists('type', $options))
  685. {
  686. $options['type'] = 'button';
  687. }
  688. return '<button' . $this->attributes($options) . '>' . $value . '</button>';
  689. }
  690. /**
  691. * Parse the form action method.
  692. *
  693. * @param string $method
  694. * @return string
  695. */
  696. protected function getMethod($method)
  697. {
  698. $method = strtoupper($method);
  699. return $method != 'GET' ? 'POST' : $method;
  700. }
  701. /**
  702. * Get the form action from the options.
  703. *
  704. * @param array $options
  705. * @return string
  706. */
  707. /* protected function getAction(array $options)
  708. {
  709. // We will also check for a "route" or "action" parameter on the array so that
  710. // developers can easily specify a route or controller action when creating
  711. // a form providing a convenient interface for creating the form actions.
  712. if (isset($options['url']))
  713. {
  714. return $this->getUrlAction($options['url']);
  715. }
  716. if (isset($options['route']))
  717. {
  718. return $this->getRouteAction($options['route']);
  719. }
  720. // If an action is available, we are attempting to open a form to a controller
  721. // action route. So, we will use the URL generator to get the path to these
  722. // actions and return them from the method. Otherwise, we'll use current.
  723. elseif (isset($options['action']))
  724. {
  725. return $this->getControllerAction($options['action']);
  726. }
  727. return $this->url->current();
  728. } */
  729. /**
  730. * Get the action for a "url" option.
  731. *
  732. * @param array|string $options
  733. * @return string
  734. */
  735. /* protected function getUrlAction($options)
  736. {
  737. if (is_array($options))
  738. {
  739. return $this->url->to($options[0], array_slice($options, 1));
  740. }
  741. return $this->url->to($options);
  742. } */
  743. /**
  744. * Get the action for a "route" option.
  745. *
  746. * @param array|string $options
  747. * @return string
  748. */
  749. /* protected function getRouteAction($options)
  750. {
  751. if (is_array($options))
  752. {
  753. return $this->url->route($options[0], array_slice($options, 1));
  754. }
  755. return $this->url->route($options);
  756. } */
  757. /**
  758. * Get the action for an "action" option.
  759. *
  760. * @param array|string $options
  761. * @return string
  762. */
  763. /* protected function getControllerAction($options)
  764. {
  765. if (is_array($options))
  766. {
  767. return $this->url->action($options[0], array_slice($options, 1));
  768. }
  769. return $this->url->action($options);
  770. } */
  771. /**
  772. * Get the form appendage for the given method.
  773. *
  774. * @param string $method
  775. * @return string
  776. */
  777. protected function getAppendage($method)
  778. {
  779. list($method, $appendage) = array(strtoupper($method), '');
  780. // If the HTTP method is in this list of spoofed methods, we will attach the
  781. // method spoofer hidden input to the form. This allows us to use regular
  782. // form to initiate PUT and DELETE requests in addition to the typical.
  783. if (in_array($method, $this->spoofedMethods))
  784. {
  785. $appendage .= $this->hidden('_method', $method);
  786. }
  787. // If the method is something other than GET we will go ahead and attach the
  788. // CSRF token to the form, as this can't hurt and is convenient to simply
  789. // always have available on every form the developers creates for them.
  790. if ($method != 'GET')
  791. {
  792. $appendage .= $this->token(array_get($this->csrfToken, 'name'), array_get($this->csrfToken, 'type'));
  793. }
  794. return $appendage;
  795. }
  796. /**
  797. * Get the ID attribute for a field name.
  798. *
  799. * @param string $name
  800. * @param array $attributes
  801. * @return string
  802. */
  803. public function getIdAttribute($name, $attributes)
  804. {
  805. if (array_key_exists('id', $attributes))
  806. {
  807. return $attributes['id'];
  808. }
  809. if (in_array($name, $this->labels))
  810. {
  811. return $name;
  812. }
  813. }
  814. /**
  815. * Get the value that should be assigned to the field.
  816. *
  817. * @param string $name
  818. * @param string $value
  819. * @return string
  820. */
  821. public function getValueAttribute($name, $value = null)
  822. {
  823. if (is_null($name))
  824. return $value;
  825. if (!is_null($this->old($name)))
  826. {
  827. return $this->old($name);
  828. }
  829. if (!is_null($value))
  830. return $value;
  831. if (isset($this->model))
  832. {
  833. return $this->getModelValueAttribute($name);
  834. }
  835. }
  836. /**
  837. * Get the model value that should be assigned to the field.
  838. *
  839. * @param string $name
  840. * @return string
  841. */
  842. protected function getModelValueAttribute($name)
  843. {
  844. if (is_object($this->model))
  845. {
  846. return object_get($this->model, $this->transformKey($name));
  847. }
  848. elseif (is_array($this->model))
  849. {
  850. return array_get($this->model, $this->transformKey($name));
  851. }
  852. }
  853. /**
  854. * Get a value from the session's old input.
  855. *
  856. * @param string $name
  857. * @return string
  858. */
  859. public function old($name)
  860. {
  861. if (isset($this->session))
  862. {
  863. return $this->session->getOldInput($this->transformKey($name));
  864. }
  865. }
  866. /**
  867. * Determine if the old input is empty.
  868. *
  869. * @return bool
  870. */
  871. public function oldInputIsEmpty()
  872. {
  873. return (isset($this->session) && count($this->session->getOldInput()) == 0);
  874. }
  875. /**
  876. * Transform key from array to dot syntax.
  877. *
  878. * @param string $key
  879. * @return string
  880. */
  881. protected function transformKey($key)
  882. {
  883. return str_replace(array('.', '[]', '[', ']'), array('_', '', '.', ''), $key);
  884. }
  885. /**
  886. * 数组转换成一个HTML属性字符串。
  887. *
  888. * @param array $attributes
  889. * @return string
  890. */
  891. public function attributes($attributes)
  892. {
  893. $html = array();
  894. // 假设我们的keys 和 value 是相同的,
  895. // 拿HTML“required”属性来说,假设是['required']数组,
  896. // 会已 required="required" 拼接起来,而不是用数字keys去拼接
  897. foreach ((array) $attributes as $key => $value)
  898. {
  899. $element = $this->attributeElement($key, $value);
  900. if (!is_null($element))
  901. $html[] = $element;
  902. }
  903. return count($html) > 0 ? ' ' . implode(' ', $html) : '';
  904. }
  905. /**
  906. * 拼接成一个属性。
  907. *
  908. * @param string $key
  909. * @param string $value
  910. * @return string
  911. */
  912. protected function attributeElement($key, $value)
  913. {
  914. if (is_numeric($key))
  915. $key = $value;
  916. if (!is_null($value))
  917. return $key . '="' . $value . '"';
  918. }
  919. }
  920. class Arr
  921. {
  922. /**
  923. * Determine whether the given value is array accessible.
  924. *
  925. * @param mixed $value
  926. * @return bool
  927. */
  928. public static function accessible($value)
  929. {
  930. return is_array($value) || $value instanceof ArrayAccess;
  931. }
  932. /**
  933. * Determine if the given key exists in the provided array.
  934. *
  935. * @param \ArrayAccess|array $array
  936. * @param string|int $key
  937. * @return bool
  938. */
  939. public static function exists($array, $key)
  940. {
  941. if ($array instanceof ArrayAccess)
  942. {
  943. return $array->offsetExists($key);
  944. }
  945. return array_key_exists($key, $array);
  946. }
  947. /**
  948. * Get an item from an array using "dot" notation.
  949. *
  950. * @param \ArrayAccess|array $array
  951. * @param string $key
  952. * @param mixed $default
  953. * @return mixed
  954. */
  955. public static function get($array, $key, $default = null)
  956. {
  957. if (!static::accessible($array))
  958. {
  959. return $default;
  960. }
  961. if (is_null($key))
  962. {
  963. return $array;
  964. }
  965. if (static::exists($array, $key))
  966. {
  967. return $array[$key];
  968. }
  969. foreach (explode('.', $key) as $segment)
  970. {
  971. if (static::accessible($array) && static::exists($array, $segment))
  972. {
  973. $array = $array[$segment];
  974. }
  975. else
  976. {
  977. return $default;
  978. }
  979. }
  980. return $array;
  981. }
  982. /**
  983. * Get all of the given array except for a specified array of items.
  984. *
  985. * @param array $array
  986. * @param array|string $keys
  987. * @return array
  988. */
  989. public static function except($array, $keys)
  990. {
  991. static::forget($array, $keys);
  992. return $array;
  993. }
  994. /**
  995. * Remove one or many array items from a given array using "dot" notation.
  996. *
  997. * @param array $array
  998. * @param array|string $keys
  999. * @return void
  1000. */
  1001. public static function forget(&$array, $keys)
  1002. {
  1003. $original = &$array;
  1004. $keys = (array) $keys;
  1005. if (count($keys) === 0)
  1006. {
  1007. return;
  1008. }
  1009. foreach ($keys as $key)
  1010. {
  1011. // if the exact key exists in the top-level, remove it
  1012. if (static::exists($array, $key))
  1013. {
  1014. unset($array[$key]);
  1015. continue;
  1016. }
  1017. $parts = explode('.', $key);
  1018. // clean up before each pass
  1019. $array = &$original;
  1020. while (count($parts) > 1)
  1021. {
  1022. $part = array_shift($parts);
  1023. if (isset($array[$part]) && is_array($array[$part]))
  1024. {
  1025. $array = &$array[$part];
  1026. }
  1027. else
  1028. {
  1029. continue 2;
  1030. }
  1031. }
  1032. unset($array[array_shift($parts)]);
  1033. }
  1034. }
  1035. }
  1036. if (!function_exists('array_get'))
  1037. {
  1038. /**
  1039. * Get an item from an array using "dot" notation.
  1040. *
  1041. * @param \ArrayAccess|array $array
  1042. * @param string $key
  1043. * @param mixed $default
  1044. * @return mixed
  1045. */
  1046. function array_get($array, $key, $default = null)
  1047. {
  1048. return Arr::get($array, $key, $default);
  1049. }
  1050. }
  1051. if (!function_exists('e'))
  1052. {
  1053. /**
  1054. * Escape HTML special characters in a string.
  1055. *
  1056. *
  1057. * @return string
  1058. */
  1059. function e($value)
  1060. {
  1061. if (is_array($value))
  1062. {
  1063. $value = json_encode($value, JSON_UNESCAPED_UNICODE);
  1064. }
  1065. return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
  1066. }
  1067. }
  1068. if (!function_exists('array_except'))
  1069. {
  1070. /**
  1071. * Get all of the given array except for a specified array of items.
  1072. *
  1073. * @param array $array
  1074. * @param array|string $keys
  1075. * @return array
  1076. */
  1077. function array_except($array, $keys)
  1078. {
  1079. return Arr::except($array, $keys);
  1080. }
  1081. }