Form.php 32 KB

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