Form.php 32 KB

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