@@ -8,17 +8,17 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Textarea extends Field |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The textarea's element |
|
| 13 | - * |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $element = 'textarea'; |
|
| 11 | + /** |
|
| 12 | + * The textarea's element |
|
| 13 | + * |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $element = 'textarea'; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The textarea's self-closing state |
|
| 20 | - * |
|
| 21 | - * @var boolean |
|
| 22 | - */ |
|
| 23 | - protected $isSelfClosing = false; |
|
| 18 | + /** |
|
| 19 | + * The textarea's self-closing state |
|
| 20 | + * |
|
| 21 | + * @var boolean |
|
| 22 | + */ |
|
| 23 | + protected $isSelfClosing = false; |
|
| 24 | 24 | } |
@@ -9,17 +9,17 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class Former extends Facade |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Get the registered component. |
|
| 14 | - * |
|
| 15 | - * @return string |
|
| 16 | - */ |
|
| 17 | - protected static function getFacadeAccessor() |
|
| 18 | - { |
|
| 19 | - if (!static::$app) { |
|
| 20 | - static::$app = FormerServiceProvider::make(); |
|
| 21 | - } |
|
| 12 | + /** |
|
| 13 | + * Get the registered component. |
|
| 14 | + * |
|
| 15 | + * @return string |
|
| 16 | + */ |
|
| 17 | + protected static function getFacadeAccessor() |
|
| 18 | + { |
|
| 19 | + if (!static::$app) { |
|
| 20 | + static::$app = FormerServiceProvider::make(); |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - return 'former'; |
|
| 24 | - } |
|
| 23 | + return 'former'; |
|
| 24 | + } |
|
| 25 | 25 | } |
@@ -8,141 +8,141 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | interface FrameworkInterface |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Filter buttons classes |
|
| 13 | - * |
|
| 14 | - * @param array $classes An array of classes |
|
| 15 | - * |
|
| 16 | - * @return array A filtered array |
|
| 17 | - */ |
|
| 18 | - public function filterButtonClasses($classes); |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Filter field classes |
|
| 22 | - * |
|
| 23 | - * @param array $classes An array of classes |
|
| 24 | - * |
|
| 25 | - * @return array A filtered array |
|
| 26 | - */ |
|
| 27 | - public function filterFieldClasses($classes); |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Add classes to a field |
|
| 31 | - * |
|
| 32 | - * @param Field $field |
|
| 33 | - * @param array $classes The possible classes to add |
|
| 34 | - * |
|
| 35 | - * @return Field |
|
| 36 | - */ |
|
| 37 | - public function getFieldClasses(Field $field, $classes); |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Add group classes |
|
| 41 | - * |
|
| 42 | - * @return string A list of group classes |
|
| 43 | - */ |
|
| 44 | - public function getGroupClasses(); |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Add label classes |
|
| 48 | - * |
|
| 49 | - * @return array An array of attributes with the label class |
|
| 50 | - */ |
|
| 51 | - public function getLabelClasses(); |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Add uneditable field classes |
|
| 55 | - * |
|
| 56 | - * @return array An array of attributes with the uneditable class |
|
| 57 | - */ |
|
| 58 | - public function getUneditableClasses(); |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Add plain text field classes |
|
| 62 | - * |
|
| 63 | - * @return array An array of attributes with the plain text class |
|
| 64 | - */ |
|
| 65 | - public function getPlainTextClasses(); |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Add form class |
|
| 69 | - * |
|
| 70 | - * @param string $type The type of form to add |
|
| 71 | - * |
|
| 72 | - * @return array |
|
| 73 | - */ |
|
| 74 | - public function getFormClasses($type); |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Add actions block class |
|
| 78 | - * |
|
| 79 | - * @return array |
|
| 80 | - */ |
|
| 81 | - public function getActionClasses(); |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Render an help text |
|
| 85 | - * |
|
| 86 | - * @param string $text |
|
| 87 | - * @param array $attributes |
|
| 88 | - * |
|
| 89 | - * @return string |
|
| 90 | - */ |
|
| 91 | - public function createHelp($text, $attributes = array()); |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Render a disabled field |
|
| 95 | - * |
|
| 96 | - * @param Field $field |
|
| 97 | - * |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function createDisabledField(Field $field); |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Render a plain text field |
|
| 104 | - * |
|
| 105 | - * @param Field $field |
|
| 106 | - * |
|
| 107 | - * @return string |
|
| 108 | - */ |
|
| 109 | - public function createPlainTextField(Field $field); |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Render an icon |
|
| 113 | - * |
|
| 114 | - * @param array $attributes Its attributes |
|
| 115 | - * |
|
| 116 | - * @return string |
|
| 117 | - */ |
|
| 118 | - public function createIcon($iconType, $attributes = array()); |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Wrap an item to be prepended or appended to the current field |
|
| 122 | - * |
|
| 123 | - * @param string $item |
|
| 124 | - * |
|
| 125 | - * @return string A wrapped item |
|
| 126 | - */ |
|
| 127 | - public function placeAround($item); |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * Wrap a field with prepended and appended items |
|
| 131 | - * |
|
| 132 | - * @param Field $field |
|
| 133 | - * @param array $prepend |
|
| 134 | - * @param array $append |
|
| 135 | - * |
|
| 136 | - * @return string A field concatented with prepended and/or appended items |
|
| 137 | - */ |
|
| 138 | - public function prependAppend($field, $prepend, $append); |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Wrap a field with potential additional tags |
|
| 142 | - * |
|
| 143 | - * @param Field $field |
|
| 144 | - * |
|
| 145 | - * @return string A wrapped field |
|
| 146 | - */ |
|
| 147 | - public function wrapField($field); |
|
| 11 | + /** |
|
| 12 | + * Filter buttons classes |
|
| 13 | + * |
|
| 14 | + * @param array $classes An array of classes |
|
| 15 | + * |
|
| 16 | + * @return array A filtered array |
|
| 17 | + */ |
|
| 18 | + public function filterButtonClasses($classes); |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Filter field classes |
|
| 22 | + * |
|
| 23 | + * @param array $classes An array of classes |
|
| 24 | + * |
|
| 25 | + * @return array A filtered array |
|
| 26 | + */ |
|
| 27 | + public function filterFieldClasses($classes); |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Add classes to a field |
|
| 31 | + * |
|
| 32 | + * @param Field $field |
|
| 33 | + * @param array $classes The possible classes to add |
|
| 34 | + * |
|
| 35 | + * @return Field |
|
| 36 | + */ |
|
| 37 | + public function getFieldClasses(Field $field, $classes); |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Add group classes |
|
| 41 | + * |
|
| 42 | + * @return string A list of group classes |
|
| 43 | + */ |
|
| 44 | + public function getGroupClasses(); |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Add label classes |
|
| 48 | + * |
|
| 49 | + * @return array An array of attributes with the label class |
|
| 50 | + */ |
|
| 51 | + public function getLabelClasses(); |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Add uneditable field classes |
|
| 55 | + * |
|
| 56 | + * @return array An array of attributes with the uneditable class |
|
| 57 | + */ |
|
| 58 | + public function getUneditableClasses(); |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Add plain text field classes |
|
| 62 | + * |
|
| 63 | + * @return array An array of attributes with the plain text class |
|
| 64 | + */ |
|
| 65 | + public function getPlainTextClasses(); |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Add form class |
|
| 69 | + * |
|
| 70 | + * @param string $type The type of form to add |
|
| 71 | + * |
|
| 72 | + * @return array |
|
| 73 | + */ |
|
| 74 | + public function getFormClasses($type); |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Add actions block class |
|
| 78 | + * |
|
| 79 | + * @return array |
|
| 80 | + */ |
|
| 81 | + public function getActionClasses(); |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Render an help text |
|
| 85 | + * |
|
| 86 | + * @param string $text |
|
| 87 | + * @param array $attributes |
|
| 88 | + * |
|
| 89 | + * @return string |
|
| 90 | + */ |
|
| 91 | + public function createHelp($text, $attributes = array()); |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Render a disabled field |
|
| 95 | + * |
|
| 96 | + * @param Field $field |
|
| 97 | + * |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function createDisabledField(Field $field); |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Render a plain text field |
|
| 104 | + * |
|
| 105 | + * @param Field $field |
|
| 106 | + * |
|
| 107 | + * @return string |
|
| 108 | + */ |
|
| 109 | + public function createPlainTextField(Field $field); |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Render an icon |
|
| 113 | + * |
|
| 114 | + * @param array $attributes Its attributes |
|
| 115 | + * |
|
| 116 | + * @return string |
|
| 117 | + */ |
|
| 118 | + public function createIcon($iconType, $attributes = array()); |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Wrap an item to be prepended or appended to the current field |
|
| 122 | + * |
|
| 123 | + * @param string $item |
|
| 124 | + * |
|
| 125 | + * @return string A wrapped item |
|
| 126 | + */ |
|
| 127 | + public function placeAround($item); |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * Wrap a field with prepended and appended items |
|
| 131 | + * |
|
| 132 | + * @param Field $field |
|
| 133 | + * @param array $prepend |
|
| 134 | + * @param array $append |
|
| 135 | + * |
|
| 136 | + * @return string A field concatented with prepended and/or appended items |
|
| 137 | + */ |
|
| 138 | + public function prependAppend($field, $prepend, $append); |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Wrap a field with potential additional tags |
|
| 142 | + * |
|
| 143 | + * @param Field $field |
|
| 144 | + * |
|
| 145 | + * @return string A wrapped field |
|
| 146 | + */ |
|
| 147 | + public function wrapField($field); |
|
| 148 | 148 | } |
@@ -6,10 +6,10 @@ |
||
| 6 | 6 | */ |
| 7 | 7 | interface FieldInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Renders the field |
|
| 11 | - * |
|
| 12 | - * @return string |
|
| 13 | - */ |
|
| 14 | - public function render(); |
|
| 9 | + /** |
|
| 10 | + * Renders the field |
|
| 11 | + * |
|
| 12 | + * @return string |
|
| 13 | + */ |
|
| 14 | + public function render(); |
|
| 15 | 15 | } |
@@ -7,128 +7,128 @@ |
||
| 7 | 7 | |
| 8 | 8 | class File |
| 9 | 9 | { |
| 10 | - private static $mimes = array( |
|
| 11 | - 'ai' => 'application/postscript', |
|
| 12 | - 'aif' => 'audio/x-aiff', |
|
| 13 | - 'aifc' => 'audio/x-aiff', |
|
| 14 | - 'aiff' => 'audio/x-aiff', |
|
| 15 | - 'avi' => 'video/x-msvideo', |
|
| 16 | - 'bin' => 'application/macbinary', |
|
| 17 | - 'bmp' => 'image/bmp', |
|
| 18 | - 'class' => 'application/octet-stream', |
|
| 19 | - 'cpt' => 'application/mac-compactpro', |
|
| 20 | - 'css' => 'text/css', |
|
| 21 | - 'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream'), |
|
| 22 | - 'dcr' => 'application/x-director', |
|
| 23 | - 'dir' => 'application/x-director', |
|
| 24 | - 'dll' => 'application/octet-stream', |
|
| 25 | - 'dms' => 'application/octet-stream', |
|
| 26 | - 'doc' => 'application/msword', |
|
| 27 | - 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
| 28 | - 'dvi' => 'application/x-dvi', |
|
| 29 | - 'dxr' => 'application/x-director', |
|
| 30 | - 'eml' => 'message/rfc822', |
|
| 31 | - 'eps' => 'application/postscript', |
|
| 32 | - 'exe' => array('application/octet-stream', 'application/x-msdownload'), |
|
| 33 | - 'gif' => 'image/gif', |
|
| 34 | - 'gtar' => 'application/x-gtar', |
|
| 35 | - 'gz' => 'application/x-gzip', |
|
| 36 | - 'hqx' => 'application/mac-binhex40', |
|
| 37 | - 'htm' => 'text/html', |
|
| 38 | - 'html' => 'text/html', |
|
| 39 | - 'jpe' => array('image/jpeg', 'image/pjpeg'), |
|
| 40 | - 'jpeg' => array('image/jpeg', 'image/pjpeg'), |
|
| 41 | - 'jpg' => array('image/jpeg', 'image/pjpeg'), |
|
| 42 | - 'js' => 'application/x-javascript', |
|
| 43 | - 'json' => array('application/json', 'text/json'), |
|
| 44 | - 'lha' => 'application/octet-stream', |
|
| 45 | - 'log' => array('text/plain', 'text/x-log'), |
|
| 46 | - 'lzh' => 'application/octet-stream', |
|
| 47 | - 'mid' => 'audio/midi', |
|
| 48 | - 'midi' => 'audio/midi', |
|
| 49 | - 'mif' => 'application/vnd.mif', |
|
| 50 | - 'mov' => 'video/quicktime', |
|
| 51 | - 'movie' => 'video/x-sgi-movie', |
|
| 52 | - 'mp2' => 'audio/mpeg', |
|
| 53 | - 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'), |
|
| 54 | - 'mpe' => 'video/mpeg', |
|
| 55 | - 'mpeg' => 'video/mpeg', |
|
| 56 | - 'mpg' => 'video/mpeg', |
|
| 57 | - 'mpga' => 'audio/mpeg', |
|
| 58 | - 'oda' => 'application/oda', |
|
| 59 | - 'odp' => 'application/vnd.oasis.opendocument.presentation', |
|
| 60 | - 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
|
| 61 | - 'odt' => 'application/vnd.oasis.opendocument.text', |
|
| 62 | - 'pdf' => array('application/pdf', 'application/x-download'), |
|
| 63 | - 'php' => array('application/x-httpd-php', 'text/x-php'), |
|
| 64 | - 'php3' => 'application/x-httpd-php', |
|
| 65 | - 'php4' => 'application/x-httpd-php', |
|
| 66 | - 'phps' => 'application/x-httpd-php-source', |
|
| 67 | - 'phtml' => 'application/x-httpd-php', |
|
| 68 | - 'png' => 'image/png', |
|
| 69 | - 'pps' => array('application/mspowerpoint', 'application/vnd.ms-powerpoint'), |
|
| 70 | - 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', |
|
| 71 | - 'ppt' => array('application/vnd.ms-powerpoint', 'application/powerpoint'), |
|
| 72 | - 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
| 73 | - 'ps' => 'application/postscript', |
|
| 74 | - 'psd' => 'application/x-photoshop', |
|
| 75 | - 'qt' => 'video/quicktime', |
|
| 76 | - 'ra' => 'audio/x-realaudio', |
|
| 77 | - 'ram' => 'audio/x-pn-realaudio', |
|
| 78 | - 'rm' => 'audio/x-pn-realaudio', |
|
| 79 | - 'rpm' => 'audio/x-pn-realaudio-plugin', |
|
| 80 | - 'rtf' => array('application/rtf', 'text/rtf'), |
|
| 81 | - 'rtx' => 'text/richtext', |
|
| 82 | - 'rv' => 'video/vnd.rn-realvideo', |
|
| 83 | - 'sea' => 'application/octet-stream', |
|
| 84 | - 'shtml' => 'text/html', |
|
| 85 | - 'sit' => 'application/x-stuffit', |
|
| 86 | - 'smi' => 'application/smil', |
|
| 87 | - 'smil' => 'application/smil', |
|
| 88 | - 'so' => 'application/octet-stream', |
|
| 89 | - 'swf' => 'application/x-shockwave-flash', |
|
| 90 | - 'tar' => 'application/x-tar', |
|
| 91 | - 'text' => 'text/plain', |
|
| 92 | - 'tgz' => array('application/x-tar', 'application/x-gzip-compressed'), |
|
| 93 | - 'tif' => 'image/tiff', |
|
| 94 | - 'tiff' => 'image/tiff', |
|
| 95 | - 'txt' => 'text/plain', |
|
| 96 | - 'wav' => 'audio/x-wav', |
|
| 97 | - 'wbxml' => 'application/wbxml', |
|
| 98 | - 'wmlc' => 'application/wmlc', |
|
| 99 | - 'word' => array('application/msword', 'application/octet-stream'), |
|
| 100 | - 'xht' => 'application/xhtml+xml', |
|
| 101 | - 'xhtml' => 'application/xhtml+xml', |
|
| 102 | - 'xl' => 'application/excel', |
|
| 103 | - 'xls' => array('application/vnd.ms-excel', 'application/excel', 'application/msexcel'), |
|
| 104 | - 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
|
| 105 | - 'xml' => 'text/xml', |
|
| 106 | - 'xsl' => 'text/xml', |
|
| 107 | - 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'), |
|
| 108 | - ); |
|
| 10 | + private static $mimes = array( |
|
| 11 | + 'ai' => 'application/postscript', |
|
| 12 | + 'aif' => 'audio/x-aiff', |
|
| 13 | + 'aifc' => 'audio/x-aiff', |
|
| 14 | + 'aiff' => 'audio/x-aiff', |
|
| 15 | + 'avi' => 'video/x-msvideo', |
|
| 16 | + 'bin' => 'application/macbinary', |
|
| 17 | + 'bmp' => 'image/bmp', |
|
| 18 | + 'class' => 'application/octet-stream', |
|
| 19 | + 'cpt' => 'application/mac-compactpro', |
|
| 20 | + 'css' => 'text/css', |
|
| 21 | + 'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream'), |
|
| 22 | + 'dcr' => 'application/x-director', |
|
| 23 | + 'dir' => 'application/x-director', |
|
| 24 | + 'dll' => 'application/octet-stream', |
|
| 25 | + 'dms' => 'application/octet-stream', |
|
| 26 | + 'doc' => 'application/msword', |
|
| 27 | + 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
|
| 28 | + 'dvi' => 'application/x-dvi', |
|
| 29 | + 'dxr' => 'application/x-director', |
|
| 30 | + 'eml' => 'message/rfc822', |
|
| 31 | + 'eps' => 'application/postscript', |
|
| 32 | + 'exe' => array('application/octet-stream', 'application/x-msdownload'), |
|
| 33 | + 'gif' => 'image/gif', |
|
| 34 | + 'gtar' => 'application/x-gtar', |
|
| 35 | + 'gz' => 'application/x-gzip', |
|
| 36 | + 'hqx' => 'application/mac-binhex40', |
|
| 37 | + 'htm' => 'text/html', |
|
| 38 | + 'html' => 'text/html', |
|
| 39 | + 'jpe' => array('image/jpeg', 'image/pjpeg'), |
|
| 40 | + 'jpeg' => array('image/jpeg', 'image/pjpeg'), |
|
| 41 | + 'jpg' => array('image/jpeg', 'image/pjpeg'), |
|
| 42 | + 'js' => 'application/x-javascript', |
|
| 43 | + 'json' => array('application/json', 'text/json'), |
|
| 44 | + 'lha' => 'application/octet-stream', |
|
| 45 | + 'log' => array('text/plain', 'text/x-log'), |
|
| 46 | + 'lzh' => 'application/octet-stream', |
|
| 47 | + 'mid' => 'audio/midi', |
|
| 48 | + 'midi' => 'audio/midi', |
|
| 49 | + 'mif' => 'application/vnd.mif', |
|
| 50 | + 'mov' => 'video/quicktime', |
|
| 51 | + 'movie' => 'video/x-sgi-movie', |
|
| 52 | + 'mp2' => 'audio/mpeg', |
|
| 53 | + 'mp3' => array('audio/mpeg', 'audio/mpg', 'audio/mpeg3', 'audio/mp3'), |
|
| 54 | + 'mpe' => 'video/mpeg', |
|
| 55 | + 'mpeg' => 'video/mpeg', |
|
| 56 | + 'mpg' => 'video/mpeg', |
|
| 57 | + 'mpga' => 'audio/mpeg', |
|
| 58 | + 'oda' => 'application/oda', |
|
| 59 | + 'odp' => 'application/vnd.oasis.opendocument.presentation', |
|
| 60 | + 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', |
|
| 61 | + 'odt' => 'application/vnd.oasis.opendocument.text', |
|
| 62 | + 'pdf' => array('application/pdf', 'application/x-download'), |
|
| 63 | + 'php' => array('application/x-httpd-php', 'text/x-php'), |
|
| 64 | + 'php3' => 'application/x-httpd-php', |
|
| 65 | + 'php4' => 'application/x-httpd-php', |
|
| 66 | + 'phps' => 'application/x-httpd-php-source', |
|
| 67 | + 'phtml' => 'application/x-httpd-php', |
|
| 68 | + 'png' => 'image/png', |
|
| 69 | + 'pps' => array('application/mspowerpoint', 'application/vnd.ms-powerpoint'), |
|
| 70 | + 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', |
|
| 71 | + 'ppt' => array('application/vnd.ms-powerpoint', 'application/powerpoint'), |
|
| 72 | + 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
|
| 73 | + 'ps' => 'application/postscript', |
|
| 74 | + 'psd' => 'application/x-photoshop', |
|
| 75 | + 'qt' => 'video/quicktime', |
|
| 76 | + 'ra' => 'audio/x-realaudio', |
|
| 77 | + 'ram' => 'audio/x-pn-realaudio', |
|
| 78 | + 'rm' => 'audio/x-pn-realaudio', |
|
| 79 | + 'rpm' => 'audio/x-pn-realaudio-plugin', |
|
| 80 | + 'rtf' => array('application/rtf', 'text/rtf'), |
|
| 81 | + 'rtx' => 'text/richtext', |
|
| 82 | + 'rv' => 'video/vnd.rn-realvideo', |
|
| 83 | + 'sea' => 'application/octet-stream', |
|
| 84 | + 'shtml' => 'text/html', |
|
| 85 | + 'sit' => 'application/x-stuffit', |
|
| 86 | + 'smi' => 'application/smil', |
|
| 87 | + 'smil' => 'application/smil', |
|
| 88 | + 'so' => 'application/octet-stream', |
|
| 89 | + 'swf' => 'application/x-shockwave-flash', |
|
| 90 | + 'tar' => 'application/x-tar', |
|
| 91 | + 'text' => 'text/plain', |
|
| 92 | + 'tgz' => array('application/x-tar', 'application/x-gzip-compressed'), |
|
| 93 | + 'tif' => 'image/tiff', |
|
| 94 | + 'tiff' => 'image/tiff', |
|
| 95 | + 'txt' => 'text/plain', |
|
| 96 | + 'wav' => 'audio/x-wav', |
|
| 97 | + 'wbxml' => 'application/wbxml', |
|
| 98 | + 'wmlc' => 'application/wmlc', |
|
| 99 | + 'word' => array('application/msword', 'application/octet-stream'), |
|
| 100 | + 'xht' => 'application/xhtml+xml', |
|
| 101 | + 'xhtml' => 'application/xhtml+xml', |
|
| 102 | + 'xl' => 'application/excel', |
|
| 103 | + 'xls' => array('application/vnd.ms-excel', 'application/excel', 'application/msexcel'), |
|
| 104 | + 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
|
| 105 | + 'xml' => 'text/xml', |
|
| 106 | + 'xsl' => 'text/xml', |
|
| 107 | + 'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'), |
|
| 108 | + ); |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Get a file MIME type by extension. |
|
| 112 | - * <code> |
|
| 113 | - * // Determine the MIME type for the .tar extension |
|
| 114 | - * $mime = File::mime('tar'); |
|
| 115 | - * // Return a default value if the MIME can't be determined |
|
| 116 | - * $mime = File::mime('ext', 'application/octet-stream'); |
|
| 117 | - * </code> |
|
| 118 | - * |
|
| 119 | - * @param string $extension |
|
| 120 | - * @param string $default |
|
| 121 | - * |
|
| 122 | - * @return string |
|
| 123 | - */ |
|
| 124 | - public static function mime($extension, $default = 'application/octet-stream') |
|
| 125 | - { |
|
| 126 | - $mimes = self::$mimes; |
|
| 110 | + /** |
|
| 111 | + * Get a file MIME type by extension. |
|
| 112 | + * <code> |
|
| 113 | + * // Determine the MIME type for the .tar extension |
|
| 114 | + * $mime = File::mime('tar'); |
|
| 115 | + * // Return a default value if the MIME can't be determined |
|
| 116 | + * $mime = File::mime('ext', 'application/octet-stream'); |
|
| 117 | + * </code> |
|
| 118 | + * |
|
| 119 | + * @param string $extension |
|
| 120 | + * @param string $default |
|
| 121 | + * |
|
| 122 | + * @return string |
|
| 123 | + */ |
|
| 124 | + public static function mime($extension, $default = 'application/octet-stream') |
|
| 125 | + { |
|
| 126 | + $mimes = self::$mimes; |
|
| 127 | 127 | |
| 128 | - if (!array_key_exists($extension, $mimes)) { |
|
| 129 | - return $default; |
|
| 130 | - } |
|
| 128 | + if (!array_key_exists($extension, $mimes)) { |
|
| 129 | + return $default; |
|
| 130 | + } |
|
| 131 | 131 | |
| 132 | - return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; |
|
| 133 | - } |
|
| 132 | + return (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension]; |
|
| 133 | + } |
|
| 134 | 134 | } |
@@ -12,128 +12,127 @@ |
||
| 12 | 12 | class File extends Field |
| 13 | 13 | { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The maximum file size |
|
| 17 | - * |
|
| 18 | - * @var integer |
|
| 19 | - */ |
|
| 20 | - private $maxSize; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * An array of mime groups to use as shortcuts |
|
| 24 | - * |
|
| 25 | - * @var array |
|
| 26 | - */ |
|
| 27 | - private $mimeGroups = array('audio', 'video', 'image'); |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * A list of properties to be injected in the attributes |
|
| 31 | - * |
|
| 32 | - * @var array |
|
| 33 | - */ |
|
| 34 | - protected $injectedProperties = array('type', 'name'); |
|
| 35 | - |
|
| 36 | - //////////////////////////////////////////////////////////////////// |
|
| 37 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 38 | - //////////////////////////////////////////////////////////////////// |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Easier arguments order for hidden fields |
|
| 42 | - * |
|
| 43 | - * @param Container $app The Illuminate Container |
|
| 44 | - * @param string $type file |
|
| 45 | - * @param string $name Field name |
|
| 46 | - * @param string $label Its label |
|
| 47 | - * @param string $value Its value |
|
| 48 | - * @param array $attributes Attributes |
|
| 49 | - */ |
|
| 50 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 51 | - { |
|
| 52 | - // Multiple files field |
|
| 53 | - if ($type == 'files') { |
|
| 54 | - $attributes['multiple'] = 'true'; |
|
| 55 | - $type = 'file'; |
|
| 56 | - $name = $name.'[]'; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Prints out the current tag |
|
| 64 | - * |
|
| 65 | - * @return string An input file tag |
|
| 66 | - */ |
|
| 67 | - public function render() |
|
| 68 | - { |
|
| 69 | - // Maximum file size |
|
| 70 | - $hidden = $this->maxSize |
|
| 71 | - ? HtmlInput::hidden('MAX_FILE_SIZE', $this->maxSize) |
|
| 72 | - : null; |
|
| 73 | - |
|
| 74 | - return $hidden.parent::render(); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - //////////////////////////////////////////////////////////////////// |
|
| 78 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 79 | - //////////////////////////////////////////////////////////////////// |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Set which types of files are accepted by the file input |
|
| 83 | - |
|
| 84 | - */ |
|
| 85 | - public function accept() |
|
| 86 | - { |
|
| 87 | - $mimes = array(); |
|
| 88 | - |
|
| 89 | - // Transform all extensions/groups to mime types |
|
| 90 | - foreach (func_get_args() as $mime) { |
|
| 91 | - |
|
| 92 | - // Shortcuts and extensions |
|
| 93 | - if (in_array($mime, $this->mimeGroups)) { |
|
| 94 | - $mime .= '/*'; |
|
| 95 | - } |
|
| 96 | - $mime = LaravelFile::mime($mime, $mime); |
|
| 97 | - |
|
| 98 | - $mimes[] = $mime; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - // Add accept attribute by concatenating the mimes |
|
| 102 | - $this->attributes['accept'] = implode(',', $mimes); |
|
| 103 | - |
|
| 104 | - return $this; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Set a maximum size for files |
|
| 109 | - * |
|
| 110 | - * @param integer $size A maximum size |
|
| 111 | - * @param string $units The size's unit |
|
| 112 | - */ |
|
| 113 | - public function max($size, $units = 'KB') |
|
| 114 | - { |
|
| 115 | - // Bytes or bits ? |
|
| 116 | - $unit = substr($units, -1); |
|
| 117 | - $base = 1024; |
|
| 118 | - if ($unit == 'b') { |
|
| 119 | - $size = $size / 8; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // Convert |
|
| 123 | - switch ($units[0]) { |
|
| 124 | - case 'K': |
|
| 125 | - $size = $size * $base; |
|
| 126 | - break; |
|
| 127 | - case 'M': |
|
| 128 | - $size = $size * pow($base, 2); |
|
| 129 | - break; |
|
| 130 | - case 'G': |
|
| 131 | - $size = $size * pow($base, 3); |
|
| 132 | - break; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $this->maxSize = (int) $size; |
|
| 136 | - |
|
| 137 | - return $this; |
|
| 138 | - } |
|
| 15 | + /** |
|
| 16 | + * The maximum file size |
|
| 17 | + * |
|
| 18 | + * @var integer |
|
| 19 | + */ |
|
| 20 | + private $maxSize; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * An array of mime groups to use as shortcuts |
|
| 24 | + * |
|
| 25 | + * @var array |
|
| 26 | + */ |
|
| 27 | + private $mimeGroups = array('audio', 'video', 'image'); |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * A list of properties to be injected in the attributes |
|
| 31 | + * |
|
| 32 | + * @var array |
|
| 33 | + */ |
|
| 34 | + protected $injectedProperties = array('type', 'name'); |
|
| 35 | + |
|
| 36 | + //////////////////////////////////////////////////////////////////// |
|
| 37 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 38 | + //////////////////////////////////////////////////////////////////// |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Easier arguments order for hidden fields |
|
| 42 | + * |
|
| 43 | + * @param Container $app The Illuminate Container |
|
| 44 | + * @param string $type file |
|
| 45 | + * @param string $name Field name |
|
| 46 | + * @param string $label Its label |
|
| 47 | + * @param string $value Its value |
|
| 48 | + * @param array $attributes Attributes |
|
| 49 | + */ |
|
| 50 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 51 | + { |
|
| 52 | + // Multiple files field |
|
| 53 | + if ($type == 'files') { |
|
| 54 | + $attributes['multiple'] = 'true'; |
|
| 55 | + $type = 'file'; |
|
| 56 | + $name = $name.'[]'; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Prints out the current tag |
|
| 64 | + * |
|
| 65 | + * @return string An input file tag |
|
| 66 | + */ |
|
| 67 | + public function render() |
|
| 68 | + { |
|
| 69 | + // Maximum file size |
|
| 70 | + $hidden = $this->maxSize |
|
| 71 | + ? HtmlInput::hidden('MAX_FILE_SIZE', $this->maxSize) |
|
| 72 | + : null; |
|
| 73 | + |
|
| 74 | + return $hidden.parent::render(); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + //////////////////////////////////////////////////////////////////// |
|
| 78 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 79 | + //////////////////////////////////////////////////////////////////// |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Set which types of files are accepted by the file input |
|
| 83 | + */ |
|
| 84 | + public function accept() |
|
| 85 | + { |
|
| 86 | + $mimes = array(); |
|
| 87 | + |
|
| 88 | + // Transform all extensions/groups to mime types |
|
| 89 | + foreach (func_get_args() as $mime) { |
|
| 90 | + |
|
| 91 | + // Shortcuts and extensions |
|
| 92 | + if (in_array($mime, $this->mimeGroups)) { |
|
| 93 | + $mime .= '/*'; |
|
| 94 | + } |
|
| 95 | + $mime = LaravelFile::mime($mime, $mime); |
|
| 96 | + |
|
| 97 | + $mimes[] = $mime; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // Add accept attribute by concatenating the mimes |
|
| 101 | + $this->attributes['accept'] = implode(',', $mimes); |
|
| 102 | + |
|
| 103 | + return $this; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Set a maximum size for files |
|
| 108 | + * |
|
| 109 | + * @param integer $size A maximum size |
|
| 110 | + * @param string $units The size's unit |
|
| 111 | + */ |
|
| 112 | + public function max($size, $units = 'KB') |
|
| 113 | + { |
|
| 114 | + // Bytes or bits ? |
|
| 115 | + $unit = substr($units, -1); |
|
| 116 | + $base = 1024; |
|
| 117 | + if ($unit == 'b') { |
|
| 118 | + $size = $size / 8; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // Convert |
|
| 122 | + switch ($units[0]) { |
|
| 123 | + case 'K': |
|
| 124 | + $size = $size * $base; |
|
| 125 | + break; |
|
| 126 | + case 'M': |
|
| 127 | + $size = $size * pow($base, 2); |
|
| 128 | + break; |
|
| 129 | + case 'G': |
|
| 130 | + $size = $size * pow($base, 3); |
|
| 131 | + break; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + $this->maxSize = (int) $size; |
|
| 135 | + |
|
| 136 | + return $this; |
|
| 137 | + } |
|
| 139 | 138 | } |
@@ -12,106 +12,106 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class Elements |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * The Container |
|
| 17 | - * |
|
| 18 | - * @var Container |
|
| 19 | - */ |
|
| 20 | - protected $app; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * The Session instance |
|
| 24 | - * |
|
| 25 | - * @var Session |
|
| 26 | - */ |
|
| 27 | - protected $session; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Build a new Element |
|
| 31 | - * |
|
| 32 | - * @param Container $app |
|
| 33 | - */ |
|
| 34 | - public function __construct(Container $app, $session) |
|
| 35 | - { |
|
| 36 | - $this->app = $app; |
|
| 37 | - $this->session = $session; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Generate a hidden field containing the current CSRF token. |
|
| 42 | - * |
|
| 43 | - * @return string |
|
| 44 | - */ |
|
| 45 | - public function token() |
|
| 46 | - { |
|
| 47 | - $csrf = method_exists($this->session, 'getToken') ? $this->session->getToken() : $this->session->token(); |
|
| 48 | - |
|
| 49 | - return (string) $this->app['former']->hidden('_token', $csrf); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Creates a label tag |
|
| 54 | - * |
|
| 55 | - * @param string $label The label content |
|
| 56 | - * @param string $for The field the label's for |
|
| 57 | - * @param array $attributes The label's attributes |
|
| 58 | - * |
|
| 59 | - * @return Element A <label> tag |
|
| 60 | - */ |
|
| 61 | - public function label($label, $for = null, $attributes = array()) |
|
| 62 | - { |
|
| 63 | - if (!$label instanceof Htmlable) { |
|
| 64 | - $oldLabel = (string) $label; |
|
| 65 | - $label = Helpers::translate($oldLabel); |
|
| 66 | - |
|
| 67 | - // If there was no change to the label, |
|
| 68 | - // then a Laravel translation did not occur |
|
| 69 | - if (lcfirst($label) == $oldLabel) { |
|
| 70 | - $label = str_replace('_', ' ', $label); |
|
| 71 | - } |
|
| 72 | - } else { |
|
| 73 | - $label = (string) $label->toHtml(); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $attributes['for'] = $for; |
|
| 77 | - $this->app['former']->labels[] = $for; |
|
| 78 | - |
|
| 79 | - return Element::create('label', $label, $attributes); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Creates a form legend |
|
| 84 | - * |
|
| 85 | - * @param string $legend The text |
|
| 86 | - * @param array $attributes Its attributes |
|
| 87 | - * |
|
| 88 | - * @return Element A <legend> tag |
|
| 89 | - */ |
|
| 90 | - public function legend($legend, $attributes = array()) |
|
| 91 | - { |
|
| 92 | - $legend = Helpers::translate($legend); |
|
| 93 | - |
|
| 94 | - return Element::create('legend', $legend, $attributes); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Close a field group |
|
| 99 | - * |
|
| 100 | - * @return string |
|
| 101 | - */ |
|
| 102 | - public function closeGroup() |
|
| 103 | - { |
|
| 104 | - $closing = ''; |
|
| 105 | - if (Group::$opened && isset(Group::$openGroup)) { |
|
| 106 | - $closing = Group::$openGroup->close(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // Close custom group |
|
| 110 | - Group::$opened = false; |
|
| 111 | - |
|
| 112 | - // Reset custom group reference |
|
| 113 | - Group::$openGroup = null; |
|
| 114 | - |
|
| 115 | - return $closing; |
|
| 116 | - } |
|
| 15 | + /** |
|
| 16 | + * The Container |
|
| 17 | + * |
|
| 18 | + * @var Container |
|
| 19 | + */ |
|
| 20 | + protected $app; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * The Session instance |
|
| 24 | + * |
|
| 25 | + * @var Session |
|
| 26 | + */ |
|
| 27 | + protected $session; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Build a new Element |
|
| 31 | + * |
|
| 32 | + * @param Container $app |
|
| 33 | + */ |
|
| 34 | + public function __construct(Container $app, $session) |
|
| 35 | + { |
|
| 36 | + $this->app = $app; |
|
| 37 | + $this->session = $session; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Generate a hidden field containing the current CSRF token. |
|
| 42 | + * |
|
| 43 | + * @return string |
|
| 44 | + */ |
|
| 45 | + public function token() |
|
| 46 | + { |
|
| 47 | + $csrf = method_exists($this->session, 'getToken') ? $this->session->getToken() : $this->session->token(); |
|
| 48 | + |
|
| 49 | + return (string) $this->app['former']->hidden('_token', $csrf); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Creates a label tag |
|
| 54 | + * |
|
| 55 | + * @param string $label The label content |
|
| 56 | + * @param string $for The field the label's for |
|
| 57 | + * @param array $attributes The label's attributes |
|
| 58 | + * |
|
| 59 | + * @return Element A <label> tag |
|
| 60 | + */ |
|
| 61 | + public function label($label, $for = null, $attributes = array()) |
|
| 62 | + { |
|
| 63 | + if (!$label instanceof Htmlable) { |
|
| 64 | + $oldLabel = (string) $label; |
|
| 65 | + $label = Helpers::translate($oldLabel); |
|
| 66 | + |
|
| 67 | + // If there was no change to the label, |
|
| 68 | + // then a Laravel translation did not occur |
|
| 69 | + if (lcfirst($label) == $oldLabel) { |
|
| 70 | + $label = str_replace('_', ' ', $label); |
|
| 71 | + } |
|
| 72 | + } else { |
|
| 73 | + $label = (string) $label->toHtml(); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $attributes['for'] = $for; |
|
| 77 | + $this->app['former']->labels[] = $for; |
|
| 78 | + |
|
| 79 | + return Element::create('label', $label, $attributes); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Creates a form legend |
|
| 84 | + * |
|
| 85 | + * @param string $legend The text |
|
| 86 | + * @param array $attributes Its attributes |
|
| 87 | + * |
|
| 88 | + * @return Element A <legend> tag |
|
| 89 | + */ |
|
| 90 | + public function legend($legend, $attributes = array()) |
|
| 91 | + { |
|
| 92 | + $legend = Helpers::translate($legend); |
|
| 93 | + |
|
| 94 | + return Element::create('legend', $legend, $attributes); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Close a field group |
|
| 99 | + * |
|
| 100 | + * @return string |
|
| 101 | + */ |
|
| 102 | + public function closeGroup() |
|
| 103 | + { |
|
| 104 | + $closing = ''; |
|
| 105 | + if (Group::$opened && isset(Group::$openGroup)) { |
|
| 106 | + $closing = Group::$openGroup->close(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // Close custom group |
|
| 110 | + Group::$opened = false; |
|
| 111 | + |
|
| 112 | + // Reset custom group reference |
|
| 113 | + Group::$openGroup = null; |
|
| 114 | + |
|
| 115 | + return $closing; |
|
| 116 | + } |
|
| 117 | 117 | } |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | * @param boolean $isRequired |
| 197 | 197 | * @return $this |
| 198 | 198 | */ |
| 199 | - public function required($isRequired=true) |
|
| 199 | + public function required($isRequired = true) |
|
| 200 | 200 | { |
| 201 | 201 | if ($isRequired) { |
| 202 | 202 | $this->attributes['required'] = true; |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | |
| 293 | 293 | // If we have a rule with a value |
| 294 | 294 | if (($colon = strpos($rule, ':')) !== false) { |
| 295 | - $rulename = substr($rule, 0, $colon) ; |
|
| 295 | + $rulename = substr($rule, 0, $colon); |
|
| 296 | 296 | |
| 297 | 297 | /** |
| 298 | 298 | * Regular expressions may contain commas and should not be divided by str_getcsv. |
@@ -16,281 +16,281 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | abstract class Field extends FormerObject implements FieldInterface |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * The IoC Container |
|
| 21 | - * |
|
| 22 | - * @var Container |
|
| 23 | - */ |
|
| 24 | - protected $app; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * The Form instance |
|
| 28 | - * |
|
| 29 | - * @var Former\Form |
|
| 30 | - */ |
|
| 31 | - protected $form; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * A label for the field (if not using Bootstrap) |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - protected $label; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * The field's group |
|
| 42 | - * |
|
| 43 | - * @var Group |
|
| 44 | - */ |
|
| 45 | - protected $group; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The field's default element |
|
| 49 | - * |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $element = 'input'; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Whether the Field is self-closing or not |
|
| 56 | - * |
|
| 57 | - * @var boolean |
|
| 58 | - */ |
|
| 59 | - protected $isSelfClosing = true; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * The field's bind destination |
|
| 63 | - * |
|
| 64 | - * @var string |
|
| 65 | - */ |
|
| 66 | - protected $bind; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Renders with floating label |
|
| 70 | - * |
|
| 71 | - * @var boolean |
|
| 72 | - */ |
|
| 73 | - protected $floatingLabel = false; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Get the current framework instance |
|
| 77 | - * |
|
| 78 | - * @return Framework |
|
| 79 | - */ |
|
| 80 | - protected function currentFramework() |
|
| 81 | - { |
|
| 82 | - if ($this->app->bound('former.form.framework')) { |
|
| 83 | - return $this->app['former.form.framework']; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - return $this->app['former.framework']; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - //////////////////////////////////////////////////////////////////// |
|
| 90 | - ///////////////////////////// INTERFACE //////////////////////////// |
|
| 91 | - //////////////////////////////////////////////////////////////////// |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Set up a Field instance |
|
| 95 | - * |
|
| 96 | - * @param string $type A field type |
|
| 97 | - */ |
|
| 98 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 99 | - { |
|
| 100 | - // Set base parameters |
|
| 101 | - $this->app = $app; |
|
| 102 | - $this->type = $type; |
|
| 103 | - $this->value = $value; |
|
| 104 | - $this->setAttributes($attributes); |
|
| 105 | - $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null; |
|
| 106 | - |
|
| 107 | - // Compute and translate label |
|
| 108 | - $this->automaticLabels($name, $label); |
|
| 109 | - |
|
| 110 | - // Repopulate field |
|
| 111 | - if ($type != 'password' && $name !== '_token') { |
|
| 112 | - $this->value = $this->repopulate(); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - // Apply Live validation rules |
|
| 116 | - if ($this->app['former']->getOption('live_validation')) { |
|
| 117 | - $rules = new LiveValidation($this); |
|
| 118 | - $rules->apply($this->getRules()); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - // Bind the Group class |
|
| 122 | - $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group'; |
|
| 123 | - $groupClass = Former::FORMSPACE.$groupClass; |
|
| 124 | - |
|
| 125 | - $this->group = new $groupClass($this->app, $this->label); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Redirect calls to the group if necessary |
|
| 130 | - * |
|
| 131 | - * @param string $method |
|
| 132 | - */ |
|
| 133 | - public function __call($method, $parameters) |
|
| 134 | - { |
|
| 135 | - // Translate attributes |
|
| 136 | - $translatable = $this->app['former']->getOption('translatable', array()); |
|
| 137 | - if (in_array($method, $translatable) and isset($parameters[0])) { |
|
| 138 | - $parameters[0] = Helpers::translate($parameters[0]); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - // Redirect calls to the Control Group |
|
| 142 | - if ((!empty($this->group) && method_exists($this->group, $method)) or Str::startsWith($method, 'onGroup')) { |
|
| 143 | - $method = str_replace('onGroup', '', $method); |
|
| 144 | - $method = lcfirst($method); |
|
| 145 | - |
|
| 146 | - call_user_func_array(array($this->group, $method), $parameters); |
|
| 147 | - |
|
| 148 | - return $this; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return parent::__call($method, $parameters); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Prints out the field, wrapped in its group |
|
| 156 | - * |
|
| 157 | - * @return string |
|
| 158 | - */ |
|
| 159 | - public function wrapAndRender() |
|
| 160 | - { |
|
| 161 | - // Dry syntax (hidden fields, plain fields) |
|
| 162 | - if ($this->isUnwrappable()) { |
|
| 163 | - $html = $this->render(); |
|
| 164 | - // Control group syntax |
|
| 165 | - } elseif (Form::hasInstanceOpened()) { |
|
| 166 | - $html = $this->group->wrapField($this); |
|
| 167 | - // Classic syntax |
|
| 168 | - } else { |
|
| 169 | - $html = $this->currentFramework()->createLabelOf($this); |
|
| 170 | - $html .= $this->render(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - return $html; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * Prints out the field |
|
| 178 | - * |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - public function __toString() |
|
| 182 | - { |
|
| 183 | - return $this->wrapAndRender(); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - //////////////////////////////////////////////////////////////////// |
|
| 187 | - ////////////////////////// PUBLIC INTERFACE //////////////////////// |
|
| 188 | - //////////////////////////////////////////////////////////////////// |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Whether the current field is required or not |
|
| 192 | - * |
|
| 193 | - * @return boolean |
|
| 194 | - */ |
|
| 195 | - public function isRequired() |
|
| 196 | - { |
|
| 197 | - return isset($this->attributes['required']); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Set required live validation attribute |
|
| 202 | - * |
|
| 203 | - * @param boolean $isRequired |
|
| 204 | - * @return $this |
|
| 205 | - */ |
|
| 206 | - public function required($isRequired=true) |
|
| 207 | - { |
|
| 208 | - if ($isRequired) { |
|
| 209 | - $this->attributes['required'] = true; |
|
| 210 | - } else { |
|
| 211 | - unset($this->attributes['required']); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - return $this; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Check if a field is unwrappable (no label) |
|
| 219 | - * |
|
| 220 | - * @return boolean |
|
| 221 | - */ |
|
| 222 | - public function isUnwrappable() |
|
| 223 | - { |
|
| 224 | - return |
|
| 225 | - ($this->form and $this->currentFramework()->is('Nude')) or |
|
| 226 | - ($this->form and $this->isOfType('inline')) or |
|
| 227 | - $this->isButton() or |
|
| 228 | - $this->isOfType('hidden') or |
|
| 229 | - \Former\Form\Group::$opened or |
|
| 230 | - $this->group and $this->group->isRaw(); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Check if field is a checkbox or a radio |
|
| 235 | - * |
|
| 236 | - * @return boolean |
|
| 237 | - */ |
|
| 238 | - public function isCheckable() |
|
| 239 | - { |
|
| 240 | - return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios', 'switch', 'switches'); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Check if the field is a button |
|
| 245 | - * |
|
| 246 | - * @return boolean |
|
| 247 | - */ |
|
| 248 | - public function isButton() |
|
| 249 | - { |
|
| 250 | - return false; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Check if the field get a floating label |
|
| 255 | - * |
|
| 256 | - * @return boolean |
|
| 257 | - */ |
|
| 258 | - public function withFloatingLabel() |
|
| 259 | - { |
|
| 260 | - return $this->floatingLabel; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Get the rules applied to the current field |
|
| 265 | - * |
|
| 266 | - * @return array An array of rules |
|
| 267 | - */ |
|
| 268 | - public function getRules() |
|
| 269 | - { |
|
| 270 | - return $this->app['former']->getRules($this->name); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - //////////////////////////////////////////////////////////////////// |
|
| 274 | - //////////////////////// SETTERS AND GETTERS /////////////////////// |
|
| 275 | - //////////////////////////////////////////////////////////////////// |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Apply a Live Validation rule by chaining |
|
| 279 | - * |
|
| 280 | - * @param string $rule The rule |
|
| 281 | - */ |
|
| 282 | - public function rule($rule) |
|
| 283 | - { |
|
| 284 | - $parameters = func_get_args(); |
|
| 285 | - array_shift($parameters); |
|
| 286 | - |
|
| 287 | - $live = new LiveValidation($this); |
|
| 288 | - $live->apply(array( |
|
| 289 | - $rule => $parameters, |
|
| 290 | - )); |
|
| 291 | - |
|
| 292 | - return $this; |
|
| 293 | - } |
|
| 19 | + /** |
|
| 20 | + * The IoC Container |
|
| 21 | + * |
|
| 22 | + * @var Container |
|
| 23 | + */ |
|
| 24 | + protected $app; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * The Form instance |
|
| 28 | + * |
|
| 29 | + * @var Former\Form |
|
| 30 | + */ |
|
| 31 | + protected $form; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * A label for the field (if not using Bootstrap) |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + protected $label; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * The field's group |
|
| 42 | + * |
|
| 43 | + * @var Group |
|
| 44 | + */ |
|
| 45 | + protected $group; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The field's default element |
|
| 49 | + * |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $element = 'input'; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Whether the Field is self-closing or not |
|
| 56 | + * |
|
| 57 | + * @var boolean |
|
| 58 | + */ |
|
| 59 | + protected $isSelfClosing = true; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * The field's bind destination |
|
| 63 | + * |
|
| 64 | + * @var string |
|
| 65 | + */ |
|
| 66 | + protected $bind; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Renders with floating label |
|
| 70 | + * |
|
| 71 | + * @var boolean |
|
| 72 | + */ |
|
| 73 | + protected $floatingLabel = false; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Get the current framework instance |
|
| 77 | + * |
|
| 78 | + * @return Framework |
|
| 79 | + */ |
|
| 80 | + protected function currentFramework() |
|
| 81 | + { |
|
| 82 | + if ($this->app->bound('former.form.framework')) { |
|
| 83 | + return $this->app['former.form.framework']; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + return $this->app['former.framework']; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + //////////////////////////////////////////////////////////////////// |
|
| 90 | + ///////////////////////////// INTERFACE //////////////////////////// |
|
| 91 | + //////////////////////////////////////////////////////////////////// |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Set up a Field instance |
|
| 95 | + * |
|
| 96 | + * @param string $type A field type |
|
| 97 | + */ |
|
| 98 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 99 | + { |
|
| 100 | + // Set base parameters |
|
| 101 | + $this->app = $app; |
|
| 102 | + $this->type = $type; |
|
| 103 | + $this->value = $value; |
|
| 104 | + $this->setAttributes($attributes); |
|
| 105 | + $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null; |
|
| 106 | + |
|
| 107 | + // Compute and translate label |
|
| 108 | + $this->automaticLabels($name, $label); |
|
| 109 | + |
|
| 110 | + // Repopulate field |
|
| 111 | + if ($type != 'password' && $name !== '_token') { |
|
| 112 | + $this->value = $this->repopulate(); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + // Apply Live validation rules |
|
| 116 | + if ($this->app['former']->getOption('live_validation')) { |
|
| 117 | + $rules = new LiveValidation($this); |
|
| 118 | + $rules->apply($this->getRules()); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + // Bind the Group class |
|
| 122 | + $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group'; |
|
| 123 | + $groupClass = Former::FORMSPACE.$groupClass; |
|
| 124 | + |
|
| 125 | + $this->group = new $groupClass($this->app, $this->label); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Redirect calls to the group if necessary |
|
| 130 | + * |
|
| 131 | + * @param string $method |
|
| 132 | + */ |
|
| 133 | + public function __call($method, $parameters) |
|
| 134 | + { |
|
| 135 | + // Translate attributes |
|
| 136 | + $translatable = $this->app['former']->getOption('translatable', array()); |
|
| 137 | + if (in_array($method, $translatable) and isset($parameters[0])) { |
|
| 138 | + $parameters[0] = Helpers::translate($parameters[0]); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + // Redirect calls to the Control Group |
|
| 142 | + if ((!empty($this->group) && method_exists($this->group, $method)) or Str::startsWith($method, 'onGroup')) { |
|
| 143 | + $method = str_replace('onGroup', '', $method); |
|
| 144 | + $method = lcfirst($method); |
|
| 145 | + |
|
| 146 | + call_user_func_array(array($this->group, $method), $parameters); |
|
| 147 | + |
|
| 148 | + return $this; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return parent::__call($method, $parameters); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Prints out the field, wrapped in its group |
|
| 156 | + * |
|
| 157 | + * @return string |
|
| 158 | + */ |
|
| 159 | + public function wrapAndRender() |
|
| 160 | + { |
|
| 161 | + // Dry syntax (hidden fields, plain fields) |
|
| 162 | + if ($this->isUnwrappable()) { |
|
| 163 | + $html = $this->render(); |
|
| 164 | + // Control group syntax |
|
| 165 | + } elseif (Form::hasInstanceOpened()) { |
|
| 166 | + $html = $this->group->wrapField($this); |
|
| 167 | + // Classic syntax |
|
| 168 | + } else { |
|
| 169 | + $html = $this->currentFramework()->createLabelOf($this); |
|
| 170 | + $html .= $this->render(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + return $html; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * Prints out the field |
|
| 178 | + * |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + public function __toString() |
|
| 182 | + { |
|
| 183 | + return $this->wrapAndRender(); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + //////////////////////////////////////////////////////////////////// |
|
| 187 | + ////////////////////////// PUBLIC INTERFACE //////////////////////// |
|
| 188 | + //////////////////////////////////////////////////////////////////// |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Whether the current field is required or not |
|
| 192 | + * |
|
| 193 | + * @return boolean |
|
| 194 | + */ |
|
| 195 | + public function isRequired() |
|
| 196 | + { |
|
| 197 | + return isset($this->attributes['required']); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Set required live validation attribute |
|
| 202 | + * |
|
| 203 | + * @param boolean $isRequired |
|
| 204 | + * @return $this |
|
| 205 | + */ |
|
| 206 | + public function required($isRequired=true) |
|
| 207 | + { |
|
| 208 | + if ($isRequired) { |
|
| 209 | + $this->attributes['required'] = true; |
|
| 210 | + } else { |
|
| 211 | + unset($this->attributes['required']); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + return $this; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Check if a field is unwrappable (no label) |
|
| 219 | + * |
|
| 220 | + * @return boolean |
|
| 221 | + */ |
|
| 222 | + public function isUnwrappable() |
|
| 223 | + { |
|
| 224 | + return |
|
| 225 | + ($this->form and $this->currentFramework()->is('Nude')) or |
|
| 226 | + ($this->form and $this->isOfType('inline')) or |
|
| 227 | + $this->isButton() or |
|
| 228 | + $this->isOfType('hidden') or |
|
| 229 | + \Former\Form\Group::$opened or |
|
| 230 | + $this->group and $this->group->isRaw(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Check if field is a checkbox or a radio |
|
| 235 | + * |
|
| 236 | + * @return boolean |
|
| 237 | + */ |
|
| 238 | + public function isCheckable() |
|
| 239 | + { |
|
| 240 | + return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios', 'switch', 'switches'); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Check if the field is a button |
|
| 245 | + * |
|
| 246 | + * @return boolean |
|
| 247 | + */ |
|
| 248 | + public function isButton() |
|
| 249 | + { |
|
| 250 | + return false; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Check if the field get a floating label |
|
| 255 | + * |
|
| 256 | + * @return boolean |
|
| 257 | + */ |
|
| 258 | + public function withFloatingLabel() |
|
| 259 | + { |
|
| 260 | + return $this->floatingLabel; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Get the rules applied to the current field |
|
| 265 | + * |
|
| 266 | + * @return array An array of rules |
|
| 267 | + */ |
|
| 268 | + public function getRules() |
|
| 269 | + { |
|
| 270 | + return $this->app['former']->getRules($this->name); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + //////////////////////////////////////////////////////////////////// |
|
| 274 | + //////////////////////// SETTERS AND GETTERS /////////////////////// |
|
| 275 | + //////////////////////////////////////////////////////////////////// |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Apply a Live Validation rule by chaining |
|
| 279 | + * |
|
| 280 | + * @param string $rule The rule |
|
| 281 | + */ |
|
| 282 | + public function rule($rule) |
|
| 283 | + { |
|
| 284 | + $parameters = func_get_args(); |
|
| 285 | + array_shift($parameters); |
|
| 286 | + |
|
| 287 | + $live = new LiveValidation($this); |
|
| 288 | + $live->apply(array( |
|
| 289 | + $rule => $parameters, |
|
| 290 | + )); |
|
| 291 | + |
|
| 292 | + return $this; |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | 295 | /** |
| 296 | 296 | * Apply multiple rules passed as a string. |
@@ -312,9 +312,9 @@ discard block |
||
| 312 | 312 | $rulename = substr($rule, 0, $colon) ; |
| 313 | 313 | |
| 314 | 314 | /** |
| 315 | - * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 316 | - * For regular expressions we are just using the complete expression as a parameter. |
|
| 317 | - */ |
|
| 315 | + * Regular expressions may contain commas and should not be divided by str_getcsv. |
|
| 316 | + * For regular expressions we are just using the complete expression as a parameter. |
|
| 317 | + */ |
|
| 318 | 318 | if ($rulename !== 'regex') { |
| 319 | 319 | $parameters = str_getcsv(substr($rule, $colon + 1)); |
| 320 | 320 | } else { |
@@ -336,159 +336,159 @@ discard block |
||
| 336 | 336 | return $this; |
| 337 | 337 | } |
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * Adds a label to the group/field |
|
| 341 | - * |
|
| 342 | - * @param string $text A label |
|
| 343 | - * @param array $attributes The label's attributes |
|
| 344 | - * |
|
| 345 | - * @return Field A field |
|
| 346 | - */ |
|
| 347 | - public function label($text, $attributes = array()) |
|
| 348 | - { |
|
| 349 | - // Create the Label element |
|
| 350 | - $for = $this->id ?: $this->name; |
|
| 351 | - $label = $this->app['former']->label($text, $for, $attributes); |
|
| 352 | - |
|
| 353 | - // Set label |
|
| 354 | - $this->label = $label; |
|
| 355 | - if ($this->group) { |
|
| 356 | - $this->group->setLabel($label); |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - return $this; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Set the Field value no matter what |
|
| 364 | - * |
|
| 365 | - * @param string $value A new value |
|
| 366 | - */ |
|
| 367 | - public function forceValue($value) |
|
| 368 | - { |
|
| 369 | - $this->value = $value; |
|
| 370 | - |
|
| 371 | - return $this; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * Classic setting of attribute, won't overwrite any populate() attempt |
|
| 376 | - * |
|
| 377 | - * @param string $value A new value |
|
| 378 | - */ |
|
| 379 | - public function value($value) |
|
| 380 | - { |
|
| 381 | - // Check if we already have a value stored for this field or in POST data |
|
| 382 | - $already = $this->repopulate(); |
|
| 383 | - |
|
| 384 | - if (!$already) { |
|
| 385 | - $this->value = $value; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - return $this; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Change the field's name |
|
| 393 | - * |
|
| 394 | - * @param string $name The new name |
|
| 395 | - */ |
|
| 396 | - public function name($name) |
|
| 397 | - { |
|
| 398 | - $this->name = $name; |
|
| 399 | - |
|
| 400 | - // Also relink the label to the new name |
|
| 401 | - $this->label($name); |
|
| 402 | - |
|
| 403 | - return $this; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Set the field as floating label |
|
| 408 | - */ |
|
| 409 | - public function floatingLabel($isFloatingLabel = true) |
|
| 410 | - { |
|
| 411 | - $this->floatingLabel = $isFloatingLabel; |
|
| 412 | - |
|
| 413 | - return $this; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Get the field's labels |
|
| 418 | - * |
|
| 419 | - * @return string |
|
| 420 | - */ |
|
| 421 | - public function getLabel() |
|
| 422 | - { |
|
| 423 | - return $this->label; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Change the field's bind destination |
|
| 428 | - * |
|
| 429 | - * @param $destination |
|
| 430 | - */ |
|
| 431 | - public function bind($destination) { |
|
| 432 | - $this->bind = $destination; |
|
| 433 | - if ($this->type != 'password') { |
|
| 434 | - $this->value = $this->repopulate(); |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - return $this; |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - //////////////////////////////////////////////////////////////////// |
|
| 441 | - //////////////////////////////// HELPERS /////////////////////////// |
|
| 442 | - //////////////////////////////////////////////////////////////////// |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Use values stored in Former to populate the current field |
|
| 446 | - */ |
|
| 447 | - private function repopulate($fallback = null) |
|
| 448 | - { |
|
| 449 | - // Get values from POST, populated, and manually set value |
|
| 450 | - $post = $this->app['former']->getPost($this->name); |
|
| 451 | - $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator']; |
|
| 452 | - $populate = $populator->get($this->bind ?: $this->name); |
|
| 453 | - |
|
| 454 | - // Assign a priority to each |
|
| 455 | - if (!is_null($post)) { |
|
| 456 | - return $post; |
|
| 457 | - } |
|
| 458 | - if (!is_null($populate)) { |
|
| 459 | - return $populate; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - return $fallback ?: $this->value; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Ponders a label and a field name, and tries to get the best out of it |
|
| 467 | - * |
|
| 468 | - * @param string $label A label |
|
| 469 | - * @param string $name A field name |
|
| 470 | - * |
|
| 471 | - * @return false|null A label and a field name |
|
| 472 | - */ |
|
| 473 | - private function automaticLabels($name, $label) |
|
| 474 | - { |
|
| 475 | - // Disabled automatic labels |
|
| 476 | - if (!$this->app['former']->getOption('automatic_label')) { |
|
| 477 | - $this->name = $name; |
|
| 478 | - $this->label($label); |
|
| 479 | - |
|
| 480 | - return false; |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - // Check for the two possibilities |
|
| 484 | - if ($label and is_null($name)) { |
|
| 485 | - $name = Str::slug($label); |
|
| 486 | - } elseif (is_null($label) and $name) { |
|
| 487 | - $label = preg_replace('/\[\]$/', '', $name); |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - // Save values |
|
| 491 | - $this->name = $name; |
|
| 492 | - $this->label($label); |
|
| 493 | - } |
|
| 339 | + /** |
|
| 340 | + * Adds a label to the group/field |
|
| 341 | + * |
|
| 342 | + * @param string $text A label |
|
| 343 | + * @param array $attributes The label's attributes |
|
| 344 | + * |
|
| 345 | + * @return Field A field |
|
| 346 | + */ |
|
| 347 | + public function label($text, $attributes = array()) |
|
| 348 | + { |
|
| 349 | + // Create the Label element |
|
| 350 | + $for = $this->id ?: $this->name; |
|
| 351 | + $label = $this->app['former']->label($text, $for, $attributes); |
|
| 352 | + |
|
| 353 | + // Set label |
|
| 354 | + $this->label = $label; |
|
| 355 | + if ($this->group) { |
|
| 356 | + $this->group->setLabel($label); |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + return $this; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Set the Field value no matter what |
|
| 364 | + * |
|
| 365 | + * @param string $value A new value |
|
| 366 | + */ |
|
| 367 | + public function forceValue($value) |
|
| 368 | + { |
|
| 369 | + $this->value = $value; |
|
| 370 | + |
|
| 371 | + return $this; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * Classic setting of attribute, won't overwrite any populate() attempt |
|
| 376 | + * |
|
| 377 | + * @param string $value A new value |
|
| 378 | + */ |
|
| 379 | + public function value($value) |
|
| 380 | + { |
|
| 381 | + // Check if we already have a value stored for this field or in POST data |
|
| 382 | + $already = $this->repopulate(); |
|
| 383 | + |
|
| 384 | + if (!$already) { |
|
| 385 | + $this->value = $value; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + return $this; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Change the field's name |
|
| 393 | + * |
|
| 394 | + * @param string $name The new name |
|
| 395 | + */ |
|
| 396 | + public function name($name) |
|
| 397 | + { |
|
| 398 | + $this->name = $name; |
|
| 399 | + |
|
| 400 | + // Also relink the label to the new name |
|
| 401 | + $this->label($name); |
|
| 402 | + |
|
| 403 | + return $this; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Set the field as floating label |
|
| 408 | + */ |
|
| 409 | + public function floatingLabel($isFloatingLabel = true) |
|
| 410 | + { |
|
| 411 | + $this->floatingLabel = $isFloatingLabel; |
|
| 412 | + |
|
| 413 | + return $this; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Get the field's labels |
|
| 418 | + * |
|
| 419 | + * @return string |
|
| 420 | + */ |
|
| 421 | + public function getLabel() |
|
| 422 | + { |
|
| 423 | + return $this->label; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Change the field's bind destination |
|
| 428 | + * |
|
| 429 | + * @param $destination |
|
| 430 | + */ |
|
| 431 | + public function bind($destination) { |
|
| 432 | + $this->bind = $destination; |
|
| 433 | + if ($this->type != 'password') { |
|
| 434 | + $this->value = $this->repopulate(); |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + return $this; |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + //////////////////////////////////////////////////////////////////// |
|
| 441 | + //////////////////////////////// HELPERS /////////////////////////// |
|
| 442 | + //////////////////////////////////////////////////////////////////// |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Use values stored in Former to populate the current field |
|
| 446 | + */ |
|
| 447 | + private function repopulate($fallback = null) |
|
| 448 | + { |
|
| 449 | + // Get values from POST, populated, and manually set value |
|
| 450 | + $post = $this->app['former']->getPost($this->name); |
|
| 451 | + $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator']; |
|
| 452 | + $populate = $populator->get($this->bind ?: $this->name); |
|
| 453 | + |
|
| 454 | + // Assign a priority to each |
|
| 455 | + if (!is_null($post)) { |
|
| 456 | + return $post; |
|
| 457 | + } |
|
| 458 | + if (!is_null($populate)) { |
|
| 459 | + return $populate; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + return $fallback ?: $this->value; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Ponders a label and a field name, and tries to get the best out of it |
|
| 467 | + * |
|
| 468 | + * @param string $label A label |
|
| 469 | + * @param string $name A field name |
|
| 470 | + * |
|
| 471 | + * @return false|null A label and a field name |
|
| 472 | + */ |
|
| 473 | + private function automaticLabels($name, $label) |
|
| 474 | + { |
|
| 475 | + // Disabled automatic labels |
|
| 476 | + if (!$this->app['former']->getOption('automatic_label')) { |
|
| 477 | + $this->name = $name; |
|
| 478 | + $this->label($label); |
|
| 479 | + |
|
| 480 | + return false; |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + // Check for the two possibilities |
|
| 484 | + if ($label and is_null($name)) { |
|
| 485 | + $name = Str::slug($label); |
|
| 486 | + } elseif (is_null($label) and $name) { |
|
| 487 | + $label = preg_replace('/\[\]$/', '', $name); |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + // Save values |
|
| 491 | + $this->name = $name; |
|
| 492 | + $this->label($label); |
|
| 493 | + } |
|
| 494 | 494 | } |
@@ -188,6 +188,9 @@ discard block |
||
| 188 | 188 | return $element; |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | + /** |
|
| 192 | + * @param string $choiceType |
|
| 193 | + */ |
|
| 191 | 194 | public function getCheckables($choiceType) |
| 192 | 195 | { |
| 193 | 196 | if (!(is_array($this->value) || $this->value instanceof \ArrayAccess)) { |
@@ -292,7 +295,7 @@ discard block |
||
| 292 | 295 | /** |
| 293 | 296 | * Sets the element's type based on options |
| 294 | 297 | * |
| 295 | - * @return this |
|
| 298 | + * @return Choice |
|
| 296 | 299 | */ |
| 297 | 300 | protected function setChoiceType() |
| 298 | 301 | { |
@@ -309,7 +312,6 @@ discard block |
||
| 309 | 312 | /** |
| 310 | 313 | * Select a value in the field's children |
| 311 | 314 | * |
| 312 | - * @param mixed $value |
|
| 313 | 315 | * |
| 314 | 316 | * @return bool |
| 315 | 317 | */ |
@@ -333,10 +335,6 @@ discard block |
||
| 333 | 335 | /** |
| 334 | 336 | * Set the choices |
| 335 | 337 | * |
| 336 | - * @param array $_choices The choices as an array |
|
| 337 | - * @param mixed $selected Facultative selected entry |
|
| 338 | - * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 339 | - * the option's values instead of the array's keys |
|
| 340 | 338 | */ |
| 341 | 339 | public function addChoice($value, $key = null) |
| 342 | 340 | { |
@@ -349,7 +347,6 @@ discard block |
||
| 349 | 347 | * Set the choices |
| 350 | 348 | * |
| 351 | 349 | * @param array $_choices The choices as an array |
| 352 | - * @param mixed $selected Facultative selected entry |
|
| 353 | 350 | * @param boolean $valuesAsKeys Whether the array's values should be used as |
| 354 | 351 | * the option's values instead of the array's keys |
| 355 | 352 | */ |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | $attributes['disabled'] = true; |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | - if(isset($attributes['name'])) { |
|
| 220 | + if (isset($attributes['name'])) { |
|
| 221 | 221 | $name = $attributes['name']; |
| 222 | 222 | unset($attributes['name']); |
| 223 | 223 | } else { |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | $name .= '[]'; |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | - if(!isset($attributes['id'])) { |
|
| 230 | + if (!isset($attributes['id'])) { |
|
| 231 | 231 | $attributes['id'] = $this->id.'_'.count($children); |
| 232 | 232 | } |
| 233 | 233 | |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | $attributes = array('value' => '', 'disabled' => 'disabled'); |
| 285 | - if (!(array)$this->value) { |
|
| 285 | + if (!(array) $this->value) { |
|
| 286 | 286 | $attributes['selected'] = 'selected'; |
| 287 | 287 | } |
| 288 | 288 | |
@@ -315,11 +315,11 @@ discard block |
||
| 315 | 315 | */ |
| 316 | 316 | protected function hasValue($choiceValue) |
| 317 | 317 | { |
| 318 | - foreach ((array)$this->value as $key => $value) { |
|
| 318 | + foreach ((array) $this->value as $key => $value) { |
|
| 319 | 319 | if (is_object($value) && method_exists($value, 'getKey')) { |
| 320 | 320 | $value = $value->getKey(); |
| 321 | 321 | } |
| 322 | - if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int)$value) { |
|
| 322 | + if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int) $value) { |
|
| 323 | 323 | return true; |
| 324 | 324 | } |
| 325 | 325 | } |
@@ -13,477 +13,477 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Choice extends Field |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Renders the checkables as inline |
|
| 18 | - * |
|
| 19 | - * @var boolean |
|
| 20 | - */ |
|
| 21 | - protected $inline = false; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The choice's placeholder |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - private $placeholder = null; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * The choice's options |
|
| 32 | - * |
|
| 33 | - * @var array |
|
| 34 | - */ |
|
| 35 | - protected $options = [ |
|
| 16 | + /** |
|
| 17 | + * Renders the checkables as inline |
|
| 18 | + * |
|
| 19 | + * @var boolean |
|
| 20 | + */ |
|
| 21 | + protected $inline = false; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The choice's placeholder |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + private $placeholder = null; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * The choice's options |
|
| 32 | + * |
|
| 33 | + * @var array |
|
| 34 | + */ |
|
| 35 | + protected $options = [ |
|
| 36 | 36 | 'isMultiple' => false, |
| 37 | 37 | 'isExpanded' => false, |
| 38 | 38 | ]; |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * The choice's choices |
|
| 42 | - * |
|
| 43 | - * @var array |
|
| 44 | - */ |
|
| 45 | - protected $choices = []; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * The choice's element |
|
| 49 | - * |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 52 | - protected $element = 'choice'; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * The choice's self-closing state |
|
| 56 | - * |
|
| 57 | - * @var boolean |
|
| 58 | - */ |
|
| 59 | - protected $isSelfClosing = false; |
|
| 60 | - |
|
| 61 | - //////////////////////////////////////////////////////////////////// |
|
| 62 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 63 | - //////////////////////////////////////////////////////////////////// |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Easier arguments order for selects |
|
| 67 | - * |
|
| 68 | - * @param Container $app The Container instance |
|
| 69 | - * @param string $type select |
|
| 70 | - * @param string $name Field name |
|
| 71 | - * @param string $label Its label |
|
| 72 | - * @param array $choices The choice's choices |
|
| 73 | - * @param string $selected The selected choice(s) |
|
| 74 | - * @param array $attributes Attributes |
|
| 75 | - */ |
|
| 76 | - public function __construct(Container $app, $type, $name, $label, $choices, $selected, $attributes) |
|
| 77 | - { |
|
| 78 | - if ($selected) { |
|
| 79 | - $this->value = $selected; |
|
| 80 | - } |
|
| 81 | - if ($choices) { |
|
| 82 | - $this->choices($choices); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - parent::__construct($app, $type, $name, $label, $selected, $attributes); |
|
| 86 | - |
|
| 87 | - $this->setChoiceType(); |
|
| 88 | - |
|
| 89 | - // Nested models population |
|
| 90 | - if (Str::contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) { |
|
| 91 | - $this->fromQuery($this->value); |
|
| 92 | - $this->value = $selected ?: null; |
|
| 93 | - } |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Renders the choice |
|
| 98 | - * |
|
| 99 | - * @return string A <select> tag |
|
| 100 | - */ |
|
| 101 | - public function render() |
|
| 102 | - { |
|
| 103 | - $choiceType = $this->getType(); |
|
| 104 | - $this->setFieldClasses(); |
|
| 105 | - |
|
| 106 | - if (!isset($this->attributes['id'])) { |
|
| 107 | - $this->setAttribute('id', $this->name); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - switch ($choiceType) { |
|
| 111 | - case 'select': |
|
| 112 | - $field = $this->getSelect(); |
|
| 113 | - break; |
|
| 114 | - case 'checkbox': |
|
| 115 | - case 'radio': |
|
| 116 | - $field = $this->getCheckables($choiceType); |
|
| 117 | - break; |
|
| 118 | - } |
|
| 119 | - $this->value = null; |
|
| 120 | - $content = $field->render(); |
|
| 121 | - return $content; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - public function getSelect() |
|
| 125 | - { |
|
| 126 | - $field = Element::create('select', null, $this->attributes); |
|
| 127 | - |
|
| 128 | - $name = $this->name; |
|
| 129 | - if ($this->options['isMultiple']) { |
|
| 130 | - $field->multiple(); |
|
| 131 | - $name .= '[]'; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - $field->setAttribute('name', $name); |
|
| 135 | - |
|
| 136 | - $field->nest($this->getOptions()); |
|
| 137 | - |
|
| 138 | - return $field; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - public function getOptions() |
|
| 142 | - { |
|
| 143 | - $children = []; |
|
| 144 | - |
|
| 145 | - // Add placeholder text if any |
|
| 146 | - if ($placeholder = $this->getPlaceholder()) { |
|
| 147 | - $children[] = $placeholder; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - foreach ($this->choices as $key => $value) { |
|
| 151 | - if (is_array($value) and !isset($value['value'])) { |
|
| 152 | - $children[] = $this->getOptGroup($key, $value); |
|
| 153 | - } else { |
|
| 154 | - $children[] = $this->getOption($key, $value); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - return $children; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - public function getOptGroup($label, $options) |
|
| 161 | - { |
|
| 162 | - $element = Element::create('optgroup')->label($label); |
|
| 163 | - $children = []; |
|
| 164 | - foreach ($options as $key => $value) { |
|
| 165 | - $option = $this->getOption($key, $value); |
|
| 166 | - $children[] = $option; |
|
| 167 | - } |
|
| 168 | - $element->nest($children); |
|
| 169 | - |
|
| 170 | - return $element; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - public function getOption($key, $value) |
|
| 174 | - { |
|
| 175 | - if (is_array($value) and isset($value['value'])) { |
|
| 176 | - $attributes = $value; |
|
| 177 | - $text = $key; |
|
| 178 | - $key = null; |
|
| 179 | - } else { |
|
| 180 | - $attributes = array('value' => $key); |
|
| 181 | - $text = $value; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - $element = Element::create('option', $text, $attributes); |
|
| 185 | - if ($this->hasValue($attributes['value'])) { |
|
| 186 | - $element->selected('selected'); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - return $element; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - public function getCheckables($choiceType) |
|
| 193 | - { |
|
| 194 | - if (!(is_array($this->value) || $this->value instanceof \ArrayAccess)) { |
|
| 195 | - $this->value = explode(',', $this->value); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - $disabled = isset($this->attributes['disabled']); |
|
| 199 | - unset($this->attributes['disabled']); |
|
| 200 | - |
|
| 201 | - $field = Element::create('div', null, $this->attributes); |
|
| 202 | - |
|
| 203 | - $children = []; |
|
| 204 | - foreach ($this->choices as $key => $value) { |
|
| 205 | - $attributes = []; |
|
| 206 | - |
|
| 207 | - if (is_array($value) and isset($value['value'])) { |
|
| 208 | - $attributes = $value; |
|
| 209 | - $label = $key; |
|
| 210 | - $inputValue = $value['value']; |
|
| 211 | - } else { |
|
| 212 | - $attributes = []; |
|
| 213 | - $label = $value; |
|
| 214 | - $inputValue = $key; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if ($disabled) { |
|
| 218 | - $attributes['disabled'] = true; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - if(isset($attributes['name'])) { |
|
| 222 | - $name = $attributes['name']; |
|
| 223 | - unset($attributes['name']); |
|
| 224 | - } else { |
|
| 225 | - $name = $this->name; |
|
| 226 | - } |
|
| 227 | - if ($this->options['isMultiple']) { |
|
| 228 | - $name .= '[]'; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - if(!isset($attributes['id'])) { |
|
| 232 | - $attributes['id'] = $this->id.'_'.count($children); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - // If inline items, add class |
|
| 236 | - $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 237 | - |
|
| 238 | - // In Bootsrap 3, don't append the the checkable type (radio/checkbox) as a class if |
|
| 239 | - // rendering inline. |
|
| 240 | - $class = $this->app['former']->framework() == 'TwitterBootstrap3' ? trim($isInline) : $choiceType.$isInline; |
|
| 241 | - |
|
| 242 | - $element = Input::create($choiceType, $name, $inputValue, $attributes); |
|
| 243 | - |
|
| 244 | - // $element->setAttribute('name', $name); |
|
| 245 | - |
|
| 246 | - if ($this->hasValue($inputValue)) { |
|
| 247 | - $element->checked('checked'); |
|
| 248 | - } |
|
| 249 | - // $attributes['value'] = $key; |
|
| 250 | - if (!$label) { |
|
| 251 | - $element = (is_object($field)) ? $field->render() : $field; |
|
| 252 | - } else { |
|
| 253 | - $rendered = $element->render(); |
|
| 254 | - $labelElement = Element::create('label', $rendered.$label); |
|
| 255 | - $element = $labelElement->for($attributes['id'])->class($class); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 259 | - if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 260 | - $wrapper = Element::create('div', $element->render())->class($choiceType); |
|
| 261 | - if ($disabled) { |
|
| 262 | - $wrapper->addClass('disabled'); |
|
| 263 | - } |
|
| 264 | - $element = $wrapper; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - $children[] = $element; |
|
| 268 | - } |
|
| 269 | - $field->nest($children); |
|
| 270 | - |
|
| 271 | - return $field; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * Get the choice's placeholder |
|
| 276 | - * |
|
| 277 | - * @return Element |
|
| 278 | - */ |
|
| 279 | - protected function getPlaceholder() |
|
| 280 | - { |
|
| 281 | - if (!$this->placeholder) { |
|
| 282 | - return false; |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - $attributes = array('value' => '', 'disabled' => 'disabled'); |
|
| 286 | - if (!(array)$this->value) { |
|
| 287 | - $attributes['selected'] = 'selected'; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - return Element::create('option', $this->placeholder, $attributes); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Sets the element's type based on options |
|
| 295 | - * |
|
| 296 | - * @return this |
|
| 297 | - */ |
|
| 298 | - protected function setChoiceType() |
|
| 299 | - { |
|
| 300 | - if ($this->options['isExpanded'] && !$this->options['isMultiple']) { |
|
| 301 | - $this->setType('radio'); |
|
| 302 | - } elseif ($this->options['isExpanded'] && $this->options['isMultiple']) { |
|
| 303 | - $this->setType('checkbox'); |
|
| 304 | - } else { |
|
| 305 | - $this->setType('select'); |
|
| 306 | - } |
|
| 307 | - return $this; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Select a value in the field's children |
|
| 312 | - * |
|
| 313 | - * @param mixed $value |
|
| 314 | - * |
|
| 315 | - * @return bool |
|
| 316 | - */ |
|
| 317 | - protected function hasValue($choiceValue) |
|
| 318 | - { |
|
| 319 | - foreach ((array)$this->value as $key => $value) { |
|
| 320 | - if (is_object($value) && method_exists($value, 'getKey')) { |
|
| 321 | - $value = $value->getKey(); |
|
| 322 | - } |
|
| 323 | - if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int)$value) { |
|
| 324 | - return true; |
|
| 325 | - } |
|
| 326 | - } |
|
| 327 | - return false; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - //////////////////////////////////////////////////////////////////// |
|
| 331 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 332 | - //////////////////////////////////////////////////////////////////// |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Set the choices |
|
| 336 | - * |
|
| 337 | - * @param array $_choices The choices as an array |
|
| 338 | - * @param mixed $selected Facultative selected entry |
|
| 339 | - * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 340 | - * the option's values instead of the array's keys |
|
| 341 | - */ |
|
| 342 | - public function addChoice($value, $key = null) |
|
| 343 | - { |
|
| 344 | - $this->choices[$key ?? $value] = $value; |
|
| 345 | - |
|
| 346 | - return $this; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Set the choices |
|
| 351 | - * |
|
| 352 | - * @param array $_choices The choices as an array |
|
| 353 | - * @param mixed $selected Facultative selected entry |
|
| 354 | - * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 355 | - * the option's values instead of the array's keys |
|
| 356 | - */ |
|
| 357 | - public function choices($_choices, $valuesAsKeys = false) |
|
| 358 | - { |
|
| 359 | - $choices = (array) $_choices; |
|
| 360 | - |
|
| 361 | - // If valuesAsKeys is true, use the values as keys |
|
| 362 | - if ($valuesAsKeys) { |
|
| 363 | - foreach ($choices as $value) { |
|
| 364 | - $this->addChoice($value, $value); |
|
| 365 | - } |
|
| 366 | - } else { |
|
| 367 | - foreach ($choices as $key => $value) { |
|
| 368 | - $this->addChoice($value, $key); |
|
| 369 | - } |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - return $this; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Creates a list of choices from a range |
|
| 377 | - * |
|
| 378 | - * @param integer $from |
|
| 379 | - * @param integer $to |
|
| 380 | - * @param integer $step |
|
| 381 | - */ |
|
| 382 | - public function range($from, $to, $step = 1) |
|
| 383 | - { |
|
| 384 | - $range = range($from, $to, $step); |
|
| 385 | - $this->choices($range, true); |
|
| 386 | - |
|
| 387 | - return $this; |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Use the results from a Fluent/Eloquent query as choices |
|
| 392 | - * |
|
| 393 | - * @param array $results An array of Eloquent models |
|
| 394 | - * @param string|function $text The value to use as text |
|
| 395 | - * @param string|array $attributes The data to use as attributes |
|
| 396 | - * @param string $selected The default selected item |
|
| 397 | - */ |
|
| 398 | - public function fromQuery($results, $text = null, $attributes = null, $selected = null) |
|
| 399 | - { |
|
| 400 | - $this->choices(Helpers::queryToArray($results, $text, $attributes))->value($selected); |
|
| 401 | - |
|
| 402 | - return $this; |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Add a placeholder to the current select |
|
| 407 | - * |
|
| 408 | - * @param string $placeholder The placeholder text |
|
| 409 | - */ |
|
| 410 | - public function placeholder($placeholder) |
|
| 411 | - { |
|
| 412 | - $this->placeholder = Helpers::translate($placeholder); |
|
| 413 | - |
|
| 414 | - return $this; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - /** |
|
| 418 | - * Set isMultiple |
|
| 419 | - * |
|
| 420 | - * @param boolean $isMultiple |
|
| 421 | - * @return $this |
|
| 422 | - */ |
|
| 423 | - public function multiple($isMultiple = true) |
|
| 424 | - { |
|
| 425 | - $this->options['isMultiple'] = $isMultiple; |
|
| 426 | - $this->setChoiceType(); |
|
| 427 | - |
|
| 428 | - return $this; |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Set isExpanded |
|
| 433 | - * |
|
| 434 | - * @param boolean $isExpanded |
|
| 435 | - * @return $this |
|
| 436 | - */ |
|
| 437 | - public function expanded($isExpanded = true) |
|
| 438 | - { |
|
| 439 | - $this->options['isExpanded'] = $isExpanded; |
|
| 440 | - $this->setChoiceType(); |
|
| 441 | - |
|
| 442 | - return $this; |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Set the choices as inline (for expanded items) |
|
| 447 | - */ |
|
| 448 | - public function inline($isInline = true) |
|
| 449 | - { |
|
| 450 | - $this->inline = $isInline; |
|
| 451 | - |
|
| 452 | - return $this; |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Set the choices as stacked (for expanded items) |
|
| 457 | - */ |
|
| 458 | - public function stacked($isStacked = true) |
|
| 459 | - { |
|
| 460 | - $this->inline = !$isStacked; |
|
| 461 | - |
|
| 462 | - return $this; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - /** |
|
| 466 | - * Check if field is a checkbox or a radio |
|
| 467 | - * |
|
| 468 | - * @return boolean |
|
| 469 | - */ |
|
| 470 | - public function isCheckable() |
|
| 471 | - { |
|
| 472 | - return $this->options['isExpanded']; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - //////////////////////////////////////////////////////////////////// |
|
| 476 | - ////////////////////////////// HELPERS ///////////////////////////// |
|
| 477 | - //////////////////////////////////////////////////////////////////// |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Returns the current choices in memory for manipulations |
|
| 481 | - * |
|
| 482 | - * @return array The current choices array |
|
| 483 | - */ |
|
| 484 | - public function getChoices() |
|
| 485 | - { |
|
| 486 | - return $this->choices; |
|
| 487 | - } |
|
| 40 | + /** |
|
| 41 | + * The choice's choices |
|
| 42 | + * |
|
| 43 | + * @var array |
|
| 44 | + */ |
|
| 45 | + protected $choices = []; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * The choice's element |
|
| 49 | + * |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | + protected $element = 'choice'; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * The choice's self-closing state |
|
| 56 | + * |
|
| 57 | + * @var boolean |
|
| 58 | + */ |
|
| 59 | + protected $isSelfClosing = false; |
|
| 60 | + |
|
| 61 | + //////////////////////////////////////////////////////////////////// |
|
| 62 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 63 | + //////////////////////////////////////////////////////////////////// |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Easier arguments order for selects |
|
| 67 | + * |
|
| 68 | + * @param Container $app The Container instance |
|
| 69 | + * @param string $type select |
|
| 70 | + * @param string $name Field name |
|
| 71 | + * @param string $label Its label |
|
| 72 | + * @param array $choices The choice's choices |
|
| 73 | + * @param string $selected The selected choice(s) |
|
| 74 | + * @param array $attributes Attributes |
|
| 75 | + */ |
|
| 76 | + public function __construct(Container $app, $type, $name, $label, $choices, $selected, $attributes) |
|
| 77 | + { |
|
| 78 | + if ($selected) { |
|
| 79 | + $this->value = $selected; |
|
| 80 | + } |
|
| 81 | + if ($choices) { |
|
| 82 | + $this->choices($choices); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + parent::__construct($app, $type, $name, $label, $selected, $attributes); |
|
| 86 | + |
|
| 87 | + $this->setChoiceType(); |
|
| 88 | + |
|
| 89 | + // Nested models population |
|
| 90 | + if (Str::contains($this->name, '.') and is_array($this->value) and !empty($this->value) and is_string($this->value[key($this->value)])) { |
|
| 91 | + $this->fromQuery($this->value); |
|
| 92 | + $this->value = $selected ?: null; |
|
| 93 | + } |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Renders the choice |
|
| 98 | + * |
|
| 99 | + * @return string A <select> tag |
|
| 100 | + */ |
|
| 101 | + public function render() |
|
| 102 | + { |
|
| 103 | + $choiceType = $this->getType(); |
|
| 104 | + $this->setFieldClasses(); |
|
| 105 | + |
|
| 106 | + if (!isset($this->attributes['id'])) { |
|
| 107 | + $this->setAttribute('id', $this->name); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + switch ($choiceType) { |
|
| 111 | + case 'select': |
|
| 112 | + $field = $this->getSelect(); |
|
| 113 | + break; |
|
| 114 | + case 'checkbox': |
|
| 115 | + case 'radio': |
|
| 116 | + $field = $this->getCheckables($choiceType); |
|
| 117 | + break; |
|
| 118 | + } |
|
| 119 | + $this->value = null; |
|
| 120 | + $content = $field->render(); |
|
| 121 | + return $content; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + public function getSelect() |
|
| 125 | + { |
|
| 126 | + $field = Element::create('select', null, $this->attributes); |
|
| 127 | + |
|
| 128 | + $name = $this->name; |
|
| 129 | + if ($this->options['isMultiple']) { |
|
| 130 | + $field->multiple(); |
|
| 131 | + $name .= '[]'; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + $field->setAttribute('name', $name); |
|
| 135 | + |
|
| 136 | + $field->nest($this->getOptions()); |
|
| 137 | + |
|
| 138 | + return $field; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + public function getOptions() |
|
| 142 | + { |
|
| 143 | + $children = []; |
|
| 144 | + |
|
| 145 | + // Add placeholder text if any |
|
| 146 | + if ($placeholder = $this->getPlaceholder()) { |
|
| 147 | + $children[] = $placeholder; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + foreach ($this->choices as $key => $value) { |
|
| 151 | + if (is_array($value) and !isset($value['value'])) { |
|
| 152 | + $children[] = $this->getOptGroup($key, $value); |
|
| 153 | + } else { |
|
| 154 | + $children[] = $this->getOption($key, $value); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + return $children; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + public function getOptGroup($label, $options) |
|
| 161 | + { |
|
| 162 | + $element = Element::create('optgroup')->label($label); |
|
| 163 | + $children = []; |
|
| 164 | + foreach ($options as $key => $value) { |
|
| 165 | + $option = $this->getOption($key, $value); |
|
| 166 | + $children[] = $option; |
|
| 167 | + } |
|
| 168 | + $element->nest($children); |
|
| 169 | + |
|
| 170 | + return $element; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + public function getOption($key, $value) |
|
| 174 | + { |
|
| 175 | + if (is_array($value) and isset($value['value'])) { |
|
| 176 | + $attributes = $value; |
|
| 177 | + $text = $key; |
|
| 178 | + $key = null; |
|
| 179 | + } else { |
|
| 180 | + $attributes = array('value' => $key); |
|
| 181 | + $text = $value; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + $element = Element::create('option', $text, $attributes); |
|
| 185 | + if ($this->hasValue($attributes['value'])) { |
|
| 186 | + $element->selected('selected'); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + return $element; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + public function getCheckables($choiceType) |
|
| 193 | + { |
|
| 194 | + if (!(is_array($this->value) || $this->value instanceof \ArrayAccess)) { |
|
| 195 | + $this->value = explode(',', $this->value); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + $disabled = isset($this->attributes['disabled']); |
|
| 199 | + unset($this->attributes['disabled']); |
|
| 200 | + |
|
| 201 | + $field = Element::create('div', null, $this->attributes); |
|
| 202 | + |
|
| 203 | + $children = []; |
|
| 204 | + foreach ($this->choices as $key => $value) { |
|
| 205 | + $attributes = []; |
|
| 206 | + |
|
| 207 | + if (is_array($value) and isset($value['value'])) { |
|
| 208 | + $attributes = $value; |
|
| 209 | + $label = $key; |
|
| 210 | + $inputValue = $value['value']; |
|
| 211 | + } else { |
|
| 212 | + $attributes = []; |
|
| 213 | + $label = $value; |
|
| 214 | + $inputValue = $key; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if ($disabled) { |
|
| 218 | + $attributes['disabled'] = true; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + if(isset($attributes['name'])) { |
|
| 222 | + $name = $attributes['name']; |
|
| 223 | + unset($attributes['name']); |
|
| 224 | + } else { |
|
| 225 | + $name = $this->name; |
|
| 226 | + } |
|
| 227 | + if ($this->options['isMultiple']) { |
|
| 228 | + $name .= '[]'; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + if(!isset($attributes['id'])) { |
|
| 232 | + $attributes['id'] = $this->id.'_'.count($children); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + // If inline items, add class |
|
| 236 | + $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 237 | + |
|
| 238 | + // In Bootsrap 3, don't append the the checkable type (radio/checkbox) as a class if |
|
| 239 | + // rendering inline. |
|
| 240 | + $class = $this->app['former']->framework() == 'TwitterBootstrap3' ? trim($isInline) : $choiceType.$isInline; |
|
| 241 | + |
|
| 242 | + $element = Input::create($choiceType, $name, $inputValue, $attributes); |
|
| 243 | + |
|
| 244 | + // $element->setAttribute('name', $name); |
|
| 245 | + |
|
| 246 | + if ($this->hasValue($inputValue)) { |
|
| 247 | + $element->checked('checked'); |
|
| 248 | + } |
|
| 249 | + // $attributes['value'] = $key; |
|
| 250 | + if (!$label) { |
|
| 251 | + $element = (is_object($field)) ? $field->render() : $field; |
|
| 252 | + } else { |
|
| 253 | + $rendered = $element->render(); |
|
| 254 | + $labelElement = Element::create('label', $rendered.$label); |
|
| 255 | + $element = $labelElement->for($attributes['id'])->class($class); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 259 | + if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 260 | + $wrapper = Element::create('div', $element->render())->class($choiceType); |
|
| 261 | + if ($disabled) { |
|
| 262 | + $wrapper->addClass('disabled'); |
|
| 263 | + } |
|
| 264 | + $element = $wrapper; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + $children[] = $element; |
|
| 268 | + } |
|
| 269 | + $field->nest($children); |
|
| 270 | + |
|
| 271 | + return $field; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * Get the choice's placeholder |
|
| 276 | + * |
|
| 277 | + * @return Element |
|
| 278 | + */ |
|
| 279 | + protected function getPlaceholder() |
|
| 280 | + { |
|
| 281 | + if (!$this->placeholder) { |
|
| 282 | + return false; |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + $attributes = array('value' => '', 'disabled' => 'disabled'); |
|
| 286 | + if (!(array)$this->value) { |
|
| 287 | + $attributes['selected'] = 'selected'; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + return Element::create('option', $this->placeholder, $attributes); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Sets the element's type based on options |
|
| 295 | + * |
|
| 296 | + * @return this |
|
| 297 | + */ |
|
| 298 | + protected function setChoiceType() |
|
| 299 | + { |
|
| 300 | + if ($this->options['isExpanded'] && !$this->options['isMultiple']) { |
|
| 301 | + $this->setType('radio'); |
|
| 302 | + } elseif ($this->options['isExpanded'] && $this->options['isMultiple']) { |
|
| 303 | + $this->setType('checkbox'); |
|
| 304 | + } else { |
|
| 305 | + $this->setType('select'); |
|
| 306 | + } |
|
| 307 | + return $this; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Select a value in the field's children |
|
| 312 | + * |
|
| 313 | + * @param mixed $value |
|
| 314 | + * |
|
| 315 | + * @return bool |
|
| 316 | + */ |
|
| 317 | + protected function hasValue($choiceValue) |
|
| 318 | + { |
|
| 319 | + foreach ((array)$this->value as $key => $value) { |
|
| 320 | + if (is_object($value) && method_exists($value, 'getKey')) { |
|
| 321 | + $value = $value->getKey(); |
|
| 322 | + } |
|
| 323 | + if ($choiceValue === $value || is_numeric($value) && $choiceValue === (int)$value) { |
|
| 324 | + return true; |
|
| 325 | + } |
|
| 326 | + } |
|
| 327 | + return false; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + //////////////////////////////////////////////////////////////////// |
|
| 331 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 332 | + //////////////////////////////////////////////////////////////////// |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Set the choices |
|
| 336 | + * |
|
| 337 | + * @param array $_choices The choices as an array |
|
| 338 | + * @param mixed $selected Facultative selected entry |
|
| 339 | + * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 340 | + * the option's values instead of the array's keys |
|
| 341 | + */ |
|
| 342 | + public function addChoice($value, $key = null) |
|
| 343 | + { |
|
| 344 | + $this->choices[$key ?? $value] = $value; |
|
| 345 | + |
|
| 346 | + return $this; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Set the choices |
|
| 351 | + * |
|
| 352 | + * @param array $_choices The choices as an array |
|
| 353 | + * @param mixed $selected Facultative selected entry |
|
| 354 | + * @param boolean $valuesAsKeys Whether the array's values should be used as |
|
| 355 | + * the option's values instead of the array's keys |
|
| 356 | + */ |
|
| 357 | + public function choices($_choices, $valuesAsKeys = false) |
|
| 358 | + { |
|
| 359 | + $choices = (array) $_choices; |
|
| 360 | + |
|
| 361 | + // If valuesAsKeys is true, use the values as keys |
|
| 362 | + if ($valuesAsKeys) { |
|
| 363 | + foreach ($choices as $value) { |
|
| 364 | + $this->addChoice($value, $value); |
|
| 365 | + } |
|
| 366 | + } else { |
|
| 367 | + foreach ($choices as $key => $value) { |
|
| 368 | + $this->addChoice($value, $key); |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + return $this; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Creates a list of choices from a range |
|
| 377 | + * |
|
| 378 | + * @param integer $from |
|
| 379 | + * @param integer $to |
|
| 380 | + * @param integer $step |
|
| 381 | + */ |
|
| 382 | + public function range($from, $to, $step = 1) |
|
| 383 | + { |
|
| 384 | + $range = range($from, $to, $step); |
|
| 385 | + $this->choices($range, true); |
|
| 386 | + |
|
| 387 | + return $this; |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Use the results from a Fluent/Eloquent query as choices |
|
| 392 | + * |
|
| 393 | + * @param array $results An array of Eloquent models |
|
| 394 | + * @param string|function $text The value to use as text |
|
| 395 | + * @param string|array $attributes The data to use as attributes |
|
| 396 | + * @param string $selected The default selected item |
|
| 397 | + */ |
|
| 398 | + public function fromQuery($results, $text = null, $attributes = null, $selected = null) |
|
| 399 | + { |
|
| 400 | + $this->choices(Helpers::queryToArray($results, $text, $attributes))->value($selected); |
|
| 401 | + |
|
| 402 | + return $this; |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Add a placeholder to the current select |
|
| 407 | + * |
|
| 408 | + * @param string $placeholder The placeholder text |
|
| 409 | + */ |
|
| 410 | + public function placeholder($placeholder) |
|
| 411 | + { |
|
| 412 | + $this->placeholder = Helpers::translate($placeholder); |
|
| 413 | + |
|
| 414 | + return $this; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + /** |
|
| 418 | + * Set isMultiple |
|
| 419 | + * |
|
| 420 | + * @param boolean $isMultiple |
|
| 421 | + * @return $this |
|
| 422 | + */ |
|
| 423 | + public function multiple($isMultiple = true) |
|
| 424 | + { |
|
| 425 | + $this->options['isMultiple'] = $isMultiple; |
|
| 426 | + $this->setChoiceType(); |
|
| 427 | + |
|
| 428 | + return $this; |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Set isExpanded |
|
| 433 | + * |
|
| 434 | + * @param boolean $isExpanded |
|
| 435 | + * @return $this |
|
| 436 | + */ |
|
| 437 | + public function expanded($isExpanded = true) |
|
| 438 | + { |
|
| 439 | + $this->options['isExpanded'] = $isExpanded; |
|
| 440 | + $this->setChoiceType(); |
|
| 441 | + |
|
| 442 | + return $this; |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Set the choices as inline (for expanded items) |
|
| 447 | + */ |
|
| 448 | + public function inline($isInline = true) |
|
| 449 | + { |
|
| 450 | + $this->inline = $isInline; |
|
| 451 | + |
|
| 452 | + return $this; |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Set the choices as stacked (for expanded items) |
|
| 457 | + */ |
|
| 458 | + public function stacked($isStacked = true) |
|
| 459 | + { |
|
| 460 | + $this->inline = !$isStacked; |
|
| 461 | + |
|
| 462 | + return $this; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + /** |
|
| 466 | + * Check if field is a checkbox or a radio |
|
| 467 | + * |
|
| 468 | + * @return boolean |
|
| 469 | + */ |
|
| 470 | + public function isCheckable() |
|
| 471 | + { |
|
| 472 | + return $this->options['isExpanded']; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + //////////////////////////////////////////////////////////////////// |
|
| 476 | + ////////////////////////////// HELPERS ///////////////////////////// |
|
| 477 | + //////////////////////////////////////////////////////////////////// |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Returns the current choices in memory for manipulations |
|
| 481 | + * |
|
| 482 | + * @return array The current choices array |
|
| 483 | + */ |
|
| 484 | + public function getChoices() |
|
| 485 | + { |
|
| 486 | + return $this->choices; |
|
| 487 | + } |
|
| 488 | 488 | |
| 489 | 489 | } |