PHPのフレームワークです。オートローディング、ルーティング、ORマッパ、フォームバリデータ、その他ユーティリティがセットになっています。
Révision | 80 (tree) |
---|---|
l'heure | 2021-01-14 15:30:17 |
Auteur | ![]() |
HTML出力周りをちょっと見直した
@@ -274,6 +274,7 @@ | ||
274 | 274 | * @param 属性名をキー、値を値とする配列。ViewUtil::attributes() に指定できる値。 |
275 | 275 | * @param タグで挟みたい内容がある場合は指定する。この場合、終了タグも一緒に返される。 |
276 | 276 | * 省略した場合、返されるタグは閉じタグが省略されたもの( <xxxx /> の形式)になる。 |
277 | + * 配列を指定した場合は \n で連結された文字列に変換される。 | |
277 | 278 | * @param 第三引数がすでにHTMLエンコード済みの場合は true を指定する。 |
278 | 279 | * falseの場合はエンコードされる。 |
279 | 280 | * @return 作成したHTML。 |
@@ -285,6 +286,9 @@ | ||
285 | 286 | if( is_null($content) ) |
286 | 287 | return $tag . ' />'; |
287 | 288 | |
289 | + if( is_array($content) ) | |
290 | + $content = "\n" . implode("\n", $content) . "\n"; | |
291 | + | |
288 | 292 | if(!$encoded) |
289 | 293 | $content = self::encode($content); |
290 | 294 |
@@ -360,8 +364,8 @@ | ||
360 | 364 | |
361 | 365 | //----------------------------------------------------------------------------------------------------- |
362 | 366 | /** |
363 | - * 引数に指定された情報で入力フォームの一項目を表すHTML文字列を作成する。<input> や <textarea> を始め、 | |
364 | - * <option>を含む <select>、ラジオボタン・チェックボックスのグループにも対応する。 | |
367 | + * 引数に指定された情報で入力フォームの一項目を表すHTML文字列を作成する。<input> や <textarea> を始め、ViewUtil::select() で説明されている | |
368 | + * 選択肢グループにも対応する。 | |
365 | 369 | * |
366 | 370 | * @param 基本的にはHTML要素の属性だが、以下のキーは特別に処理される。 |
367 | 371 | * type <input> の type 属性だが、次のいずれかの場合は特別に処理される。省略時は "text"。 |
@@ -381,7 +385,7 @@ | ||
381 | 385 | * options choice を指定するならそちらから生成できるので省略できる。 |
382 | 386 | * typeの値が select の場合、一つの <option> HTML文字列を1要素とした配列。 |
383 | 387 | * radios, checks の場合は、一つの <input> HTML文字列を1要素とした配列。 |
384 | - * glue typeの値が radios, checks の場合に、各選択肢をつなげるHTML。 | |
388 | + * glue typeの値が radios, checks の場合に、各選択肢をつなげるHTML。省略時は "\n"。 | |
385 | 389 | * placeholder 普通に <input> の placeholder 属性だが、type=select の場合は一番先頭に位置 |
386 | 390 | * する value="" の <option> の見出し文字列となる。 |
387 | 391 | * @return 作成したHTML。 |
@@ -389,68 +393,15 @@ | ||
389 | 393 | public static function input($attributes) { |
390 | 394 | |
391 | 395 | // 種別を取り出す。 |
392 | - $type = @ArrayUtil::eject($attributes, 'type') ?: 'text'; | |
396 | + $origtype = @ArrayUtil::eject($attributes, 'type') ?: 'text'; | |
397 | + $type = strtolower($origtype); | |
393 | 398 | |
394 | - // 選択肢から選ぶ項目の場合は... | |
395 | - if( in_array(strtolower($type), ['select', 'radios', 'checks']) ) { | |
399 | + // 種別 select, radios, checks はこちらで処理する。 | |
400 | + if( in_array($type, ['select', 'radios', 'checks']) ) | |
401 | + return static::select($type, $attributes); | |
396 | 402 | |
397 | - // 引数に指定されている glue を取り出しておく。先に取り出さないと生成するHTMLに反映されてしまうので… | |
398 | - $glue = @ArrayUtil::eject($attributes, 'glue') ?? "\n"; | |
399 | - | |
400 | - // 引数の options が必要になるので取り出す。指定されていない場合は... | |
401 | - $options = @ArrayUtil::eject($attributes, 'options'); | |
402 | - if( !isset($options) ) { | |
403 | - | |
404 | - // 引数に指定されている value, choice を取り出す。 | |
405 | - $value = @ArrayUtil::eject($attributes, 'value'); | |
406 | - $choices = @ArrayUtil::eject($attributes, 'choice'); | |
407 | - $format = @ArrayUtil::eject($attributes, 'label-format') ?? '%1$s'; | |
408 | - | |
409 | - // 種別によって分岐。options の値を作成する。 | |
410 | - switch( strtolower($type) ) { | |
411 | - | |
412 | - case 'select': | |
413 | - foreach($choices as $key => $label) { | |
414 | - $pars = array('type'=>'option', 'value'=>$key, 'label'=>sprintf($format, $label, $key), 'selected'=>$key==$value); | |
415 | - $options[] = self::choice($pars); | |
416 | - } | |
417 | - break; | |
418 | - | |
419 | - case 'radios': | |
420 | - | |
421 | - // 引数に指定されている属性を生かす形で選択肢を生成していく。 | |
422 | - $attributes['type'] = 'radio'; | |
423 | - foreach($choices as $key => $label) { | |
424 | - $attributes['value'] = $key; | |
425 | - $attributes['label'] = sprintf($format, $label, $key); | |
426 | - $attributes['checked'] = $key == $value; | |
427 | - $options[] = self::choice($attributes); | |
428 | - } | |
429 | - | |
430 | - break; | |
431 | - | |
432 | - case 'checks': | |
433 | - | |
434 | - // name を少し加工して、フォーム送信時にPHPが配列として処理するようにする。 | |
435 | - if(@$attributes['name']) | |
436 | - $attributes['name'] .= '[]'; | |
437 | - | |
438 | - // 引数に指定されている属性を生かす形で選択肢を生成していく。 | |
439 | - $attributes['type'] = 'check'; | |
440 | - foreach($choices as $key => $label) { | |
441 | - $attributes['value'] = $key; | |
442 | - $attributes['label'] = sprintf($format, $label, $key); | |
443 | - $attributes['checked'] = in_array($key, (array)$value); | |
444 | - $options[] = self::choice($attributes); | |
445 | - } | |
446 | - | |
447 | - break; | |
448 | - } | |
449 | - } | |
450 | - } | |
451 | - | |
452 | 403 | // 日時系のコントロールを使うときに、valueをいつもの形式で指定できるように少し面倒を見てやる。 |
453 | - switch( strtolower($type) ) { | |
404 | + switch( $type ) { | |
454 | 405 | |
455 | 406 | // "datetime-local" を使うときは、valueの日付と時刻の区切り文字を "T" にする必要がある。 |
456 | 407 | case 'datetime-local': $attributes['value'] = preg_replace('/(?<=\d)\s+(?=\d)/', 'T', $attributes['value']); break; |
@@ -463,24 +414,8 @@ | ||
463 | 414 | } |
464 | 415 | |
465 | 416 | // 種別によって処理する。 |
466 | - switch( strtolower($type) ) { | |
417 | + switch($type) { | |
467 | 418 | |
468 | - // <option>を含む <select>。 | |
469 | - case 'select': | |
470 | - | |
471 | - // 引数に指定されている placeholder を取り出して、指定されている場合は最初の選択肢として反映する。 | |
472 | - $placeholder = @ArrayUtil::eject($attributes, 'placeholder'); | |
473 | - if( isset($placeholder) ) | |
474 | - array_unshift( $options, self::choice(array('type'=>'option', 'value'=>'', 'label'=>$placeholder)) ); | |
475 | - | |
476 | - // <option>を改行でつなげて、<select> で囲む。 | |
477 | - return ViewUtil::tag('select', $attributes, "\n".implode("\n", $options)."\n", true); | |
478 | - | |
479 | - // ラジオボタン・チェックボックスのグループ。 | |
480 | - case 'radios': | |
481 | - case 'checks': | |
482 | - return implode($glue, $options); | |
483 | - | |
484 | 419 | // 単体チェックボックスを作成する場合。 |
485 | 420 | case 'check': |
486 | 421 | case 'checkbox': |
@@ -490,11 +425,11 @@ | ||
490 | 425 | // <textarea> を作成する場合。 |
491 | 426 | case 'textarea': |
492 | 427 | $value = @ArrayUtil::eject($attributes, 'value'); |
493 | - return ViewUtil::tag('textarea', $attributes, (string)$value); // stringキャストしているのは、null の場合に閉じタグが出ないから。 | |
428 | + return ViewUtil::tag('textarea', $attributes, (string)$value); // stringキャストしているのは、null だったら閉じタグなしになってしまうため。 | |
494 | 429 | |
495 | 430 | // それ以外では <input> を生成する。 |
496 | 431 | default: |
497 | - $attributes['type'] = $type; | |
432 | + $attributes['type'] = $origtype; | |
498 | 433 | return ViewUtil::tag('input', $attributes); |
499 | 434 | } |
500 | 435 | } |
@@ -501,9 +436,103 @@ | ||
501 | 436 | |
502 | 437 | //----------------------------------------------------------------------------------------------------- |
503 | 438 | /** |
504 | - * 引数に指定された情報でセレクトボックスの <option> やチェックボックス・ラジオボタンなどの、選択肢を | |
505 | - * 示すHTML文字列を作成する。チェック・ラジオでは <label> も設定される。 | |
439 | + * 引数に指定された情報で、セレクトボックスやラジオボタンのような選択肢グループを表すHTML文字列を作成する。 | |
506 | 440 | * |
441 | + * @param 種別。select, radios, checks のいずれか。 | |
442 | + * @param select の場合は<select>タグの属性、radios, checks では各<input>タグの属性だが、以下のキーは特別に処理される。 | |
443 | + * choice 選択肢の配列。options を指定した場合はそちらが優先される。 | |
444 | + * キーでvalueを、値で選択肢名を指定する。選択肢名は次のキーで操作することも出来る。 | |
445 | + * label-format choice の選択肢名を生成するときの sprintf フォーマット文字列。%1$ が値、%2$ がキーとなる。 | |
446 | + * 省略した場合は "%1$s"。 | |
447 | + * value 最初に選択状態になっている選択肢のキー。 | |
448 | + * options choice を指定するならそちらから生成されるので省略できる。 | |
449 | + * select の場合は一つの <option> HTML文字列を1要素とした配列。 | |
450 | + * radios, checks の場合は一つの <input> HTML文字列を1要素とした配列。 | |
451 | + * glue radios, checks の場合に、各選択肢をつなげるHTML。省略時は "\n"。 | |
452 | + * placeholder select の場合は、一番先頭に位置する value="" の <option> の見出し文字列。 | |
453 | + * @return 作成したHTML。 | |
454 | + */ | |
455 | + public static function select($type, $attributes) { | |
456 | + | |
457 | + // 引数に指定されている choice, label-format, value, options, glue を取り出す。 | |
458 | + $value = @ArrayUtil::eject($attributes, 'value'); | |
459 | + $choices = @ArrayUtil::eject($attributes, 'choice'); | |
460 | + $format = @ArrayUtil::eject($attributes, 'label-format') ?? '%1$s'; | |
461 | + $options = @ArrayUtil::eject($attributes, 'options'); | |
462 | + $glue = @ArrayUtil::eject($attributes, 'glue') ?? "\n"; | |
463 | + | |
464 | + // 種別によって分岐。 | |
465 | + switch( strtolower($type) ) { | |
466 | + | |
467 | + case 'select': | |
468 | + | |
469 | + // options がない場合は choice や label-format などを見て生成する。 | |
470 | + if( !isset($options) ) { | |
471 | + | |
472 | + foreach($choices as $key => $label) { | |
473 | + $pars = array('type'=>'option', 'value'=>$key, 'label'=>sprintf($format, $label, $key), 'checkif'=>$value); | |
474 | + $options[] = self::choice($pars); | |
475 | + } | |
476 | + | |
477 | + // 引数に指定されている placeholder を取り出して、指定されている場合は最初の選択肢として追加する。 | |
478 | + $placeholder = @ArrayUtil::eject($attributes, 'placeholder'); | |
479 | + if( isset($placeholder) ) | |
480 | + array_unshift( $options, self::choice(array('type'=>'option', 'value'=>'', 'label'=>$placeholder)) ); | |
481 | + } | |
482 | + | |
483 | + // options を <select> で囲んでリターン。 | |
484 | + return ViewUtil::tag('select', $attributes, $options, true); | |
485 | + | |
486 | + case 'radios': | |
487 | + | |
488 | + // options がない場合は choice や label-format などを見て生成する。 | |
489 | + if( !isset($options) ) { | |
490 | + | |
491 | + // 引数に指定されている属性を生かす形で選択肢を生成していく。 | |
492 | + $attributes['type'] = 'radio'; | |
493 | + foreach($choices as $key => $label) { | |
494 | + $attributes['value'] = $key; | |
495 | + $attributes['label'] = sprintf($format, $label, $key); | |
496 | + $attributes['checkif'] = $value; | |
497 | + $options[] = self::choice($attributes); | |
498 | + } | |
499 | + } | |
500 | + | |
501 | + // glue で接続してリターン。 | |
502 | + return implode($glue, $options); | |
503 | + | |
504 | + case 'checks': | |
505 | + | |
506 | + // options がない場合は choice や label-format などを見て生成する。 | |
507 | + if( !isset($options) ) { | |
508 | + | |
509 | + // name を少し加工して、フォーム送信時にPHPが配列として処理するようにする。 | |
510 | + if(@$attributes['name']) | |
511 | + $attributes['name'] .= '[]'; | |
512 | + | |
513 | + // 引数に指定されている属性を生かす形で選択肢を生成していく。 | |
514 | + $attributes['type'] = 'check'; | |
515 | + foreach($choices as $key => $label) { | |
516 | + $attributes['value'] = $key; | |
517 | + $attributes['label'] = sprintf($format, $label, $key); | |
518 | + $attributes['checkif'] = (array)$value; | |
519 | + $options[] = self::choice($attributes); | |
520 | + } | |
521 | + } | |
522 | + | |
523 | + // glue で接続してリターン。 | |
524 | + return implode($glue, $options); | |
525 | + | |
526 | + default: | |
527 | + throw new ErrorException("定義されていない引数値 '{$type}' です。"); | |
528 | + } | |
529 | + } | |
530 | + | |
531 | + //----------------------------------------------------------------------------------------------------- | |
532 | + /** | |
533 | + * 引数に指定された情報でセレクトボックスの <option> やチェックボックス・ラジオボタンなどの、選択肢の一つを表すHTML文字列を作成する。 | |
534 | + * チェック・ラジオでは <label> も設定される。 | |
535 | + * | |
507 | 536 | * @param HTML要素の属性だが、以下のキーは特別に処理される。 |
508 | 537 | * type 以下のいずれか。省略時はラジオボタン |
509 | 538 | * radio ラジオボタン。デフォルト。 |
@@ -526,9 +555,9 @@ | ||
526 | 555 | // 選択肢名を取り出す。 |
527 | 556 | $label = @ArrayUtil::eject($attributes, 'label'); |
528 | 557 | |
529 | - // checkif の処理。 | |
558 | + // value属性の値が checkif の指定に含まれるなら選択状態にする。 | |
530 | 559 | $checkif = @ArrayUtil::eject($attributes, 'checkif'); |
531 | - $selected = isset($checkif) && in_array(@$attributes['value'], (array)$checkif); | |
560 | + $checked = in_array(@$attributes['value'], (array)$checkif); | |
532 | 561 | |
533 | 562 | // 種別に従って出力。 |
534 | 563 | switch( strtolower($type) ) { |
@@ -544,17 +573,17 @@ | ||
544 | 573 | $attributes['id'] = uniqid(); |
545 | 574 | } |
546 | 575 | |
547 | - // value属性の値が checkif の指定に含まれるなら選択状態にする。 | |
548 | - if( !isset($attributes['checked']) ) $attributes['checked'] = $selected; | |
576 | + // 選択状態にするかどうかを決定。 | |
577 | + if( !isset($attributes['checked']) ) $attributes['checked'] = $checked; | |
549 | 578 | |
550 | 579 | // <input> タグを作成。 |
551 | 580 | $attributes['type'] = $type; |
552 | 581 | $result = ViewUtil::tag('input', $attributes); |
553 | 582 | |
554 | - // 選択肢名があるならそれも続けて、<label> で囲む。 | |
583 | + // 選択肢名があるなら <label> で囲む。 | |
555 | 584 | if(strlen($label) > 0) { |
556 | 585 | |
557 | - // 同様に、<label> の for 属性も補う。 | |
586 | + // <label> の for 属性も補う。 | |
558 | 587 | // ただ、明示的に for 属性が指定されているならそちらを使う。jsでクローンするためにidやforを出したくないときなどに需要がある。 |
559 | 588 | $for = $attributes['for'] ?? $attributes['id']; |
560 | 589 | $pair = $result . ViewUtil::encode($label); |
@@ -565,8 +594,7 @@ | ||
565 | 594 | |
566 | 595 | case 'option': |
567 | 596 | |
568 | - // value属性の値が checkif の指定に含まれるなら選択状態にする。 | |
569 | - if( !isset($attributes['selected']) ) $attributes['selected'] = $selected; | |
597 | + if( !isset($attributes['selected']) ) $attributes['selected'] = $checked; | |
570 | 598 | |
571 | 599 | return ViewUtil::tag($type, $attributes, $label); |
572 | 600 |
@@ -749,8 +749,7 @@ | ||
749 | 749 | |
750 | 750 | //----------------------------------------------------------------------------------------------------- |
751 | 751 | /** |
752 | - * 引数に指定されたFormItemインスタンスの getInputHtml(), getChoiceHtml() を見ながら、フォームでの項目 | |
753 | - * を表すHTML文字列を作成する。 | |
752 | + * 引数に指定されたFormItemインスタンスの getInputHtml(), getChoiceHtml() を見ながら、フォームでの項目を表すHTML文字列を作成する。 | |
754 | 753 | * |
755 | 754 | * @param FormItemインスタンス |
756 | 755 | * @param ViewUtil::input() の引数と同じだが、基本的には type を指定するだけで良い。出力されるHTML要素に属性を設定したい場合はそれらを追加する |
@@ -845,14 +844,14 @@ | ||
845 | 844 | foreach($this['items'] as $item) { |
846 | 845 | $th = ViewUtil::tag('th', array(), $item['caption']); |
847 | 846 | $td = ViewUtil::tag('td', array(), $item->scaffold(), true); |
848 | - $rows[] = ViewUtil::tag('tr', array(), $th . "\n" . $td, true); | |
847 | + $rows[] = ViewUtil::tag('tr', array(), [$th, $td], true); | |
849 | 848 | } |
850 | 849 | |
851 | - return ViewUtil::tag('table', $params, implode("\n", $rows), true); | |
850 | + return ViewUtil::tag('table', $params, $rows, true); | |
852 | 851 | } |
853 | 852 | |
854 | 853 | |
855 | - // 派生クラス向けユーティリティ。一応publicにしてあるが需要はないだろう。 | |
854 | + // 一部のルールの処理を切り出したユーティリティメソッド。 | |
856 | 855 | //===================================================================================================== |
857 | 856 | |
858 | 857 | //----------------------------------------------------------------------------------------------------- |