Form.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  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. }