Completed
Pull Request — master (#607)
by Tortue
01:28
created
src/Former/Form/Fields/Textarea.php 1 patch
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -8,17 +8,17 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/Former/Facades/Former.php 1 patch
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -9,17 +9,17 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/Former/Interfaces/FrameworkInterface.php 1 patch
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -8,141 +8,141 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/Former/Interfaces/FieldInterface.php 1 patch
Indentation   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -6,10 +6,10 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/Laravel/File.php 1 patch
Indentation   +121 added lines, -121 removed lines patch added patch discarded remove patch
@@ -7,128 +7,128 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/Former/Form/Fields/File.php 1 patch
Indentation   +123 added lines, -124 removed lines patch added patch discarded remove patch
@@ -12,128 +12,127 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/Former/Form/Elements.php 1 patch
Indentation   +102 added lines, -102 removed lines patch added patch discarded remove patch
@@ -12,106 +12,106 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/Former/Traits/Field.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -196,7 +196,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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.
Please login to merge, or discard this patch.
Indentation   +406 added lines, -406 removed lines patch added patch discarded remove patch
@@ -16,264 +16,264 @@  discard block
 block discarded – undo
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
-	 * Get the current framework instance
70
-	 *
71
-	 * @return Framework
72
-	 */
73
-	protected function currentFramework()
74
-	{
75
-		if ($this->app->bound('former.form.framework')) {
76
-			return $this->app['former.form.framework'];
77
-		}
78
-
79
-		return $this->app['former.framework'];
80
-	}
81
-
82
-	////////////////////////////////////////////////////////////////////
83
-	///////////////////////////// INTERFACE ////////////////////////////
84
-	////////////////////////////////////////////////////////////////////
85
-
86
-	/**
87
-	 * Set up a Field instance
88
-	 *
89
-	 * @param string $type A field type
90
-	 */
91
-	public function __construct(Container $app, $type, $name, $label, $value, $attributes)
92
-	{
93
-		// Set base parameters
94
-		$this->app   = $app;
95
-		$this->type  = $type;
96
-		$this->value = $value;
97
-		$this->setAttributes($attributes);
98
-		$this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null;
99
-
100
-		// Compute and translate label
101
-		$this->automaticLabels($name, $label);
102
-
103
-		// Repopulate field
104
-		if ($type != 'password' && $name !== '_token') {
105
-			$this->value = $this->repopulate();
106
-		}
107
-
108
-		// Apply Live validation rules
109
-		if ($this->app['former']->getOption('live_validation')) {
110
-			$rules = new LiveValidation($this);
111
-			$rules->apply($this->getRules());
112
-		}
113
-
114
-		// Bind the Group class
115
-		$groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group';
116
-		$groupClass = Former::FORMSPACE.$groupClass;
117
-
118
-		$this->group = new $groupClass($this->app, $this->label);
119
-	}
120
-
121
-	/**
122
-	 * Redirect calls to the group if necessary
123
-	 *
124
-	 * @param string $method
125
-	 */
126
-	public function __call($method, $parameters)
127
-	{
128
-		// Translate attributes
129
-		$translatable = $this->app['former']->getOption('translatable', array());
130
-		if (in_array($method, $translatable) and isset($parameters[0])) {
131
-			$parameters[0] = Helpers::translate($parameters[0]);
132
-		}
133
-
134
-		// Redirect calls to the Control Group
135
-		if ((!empty($this->group) && method_exists($this->group, $method)) or Str::startsWith($method, 'onGroup')) {
136
-			$method = str_replace('onGroup', '', $method);
137
-			$method = lcfirst($method);
138
-
139
-			call_user_func_array(array($this->group, $method), $parameters);
140
-
141
-			return $this;
142
-		}
143
-
144
-		return parent::__call($method, $parameters);
145
-	}
146
-
147
-	/**
148
-	 * Prints out the field, wrapped in its group
149
-	 *
150
-	 * @return string
151
-	 */
152
-	public function wrapAndRender()
153
-	{
154
-		// Dry syntax (hidden fields, plain fields)
155
-		if ($this->isUnwrappable()) {
156
-			$html = $this->render();
157
-			// Control group syntax
158
-		} elseif (Form::hasInstanceOpened()) {
159
-			$html = $this->group->wrapField($this);
160
-			// Classic syntax
161
-		} else {
162
-			$html = $this->currentFramework()->createLabelOf($this);
163
-			$html .= $this->render();
164
-		}
165
-
166
-		return $html;
167
-	}
168
-
169
-	/**
170
-	 * Prints out the field
171
-	 *
172
-	 * @return string
173
-	 */
174
-	public function __toString()
175
-	{
176
-		return $this->wrapAndRender();
177
-	}
178
-
179
-	////////////////////////////////////////////////////////////////////
180
-	////////////////////////// PUBLIC INTERFACE ////////////////////////
181
-	////////////////////////////////////////////////////////////////////
182
-
183
-	/**
184
-	 * Whether the current field is required or not
185
-	 *
186
-	 * @return boolean
187
-	 */
188
-	public function isRequired()
189
-	{
190
-		return isset($this->attributes['required']);
191
-	}
192
-
193
-	/**
194
-	 * Set required live validation attribute
195
-	 *
196
-	 * @param boolean $isRequired
197
-	 * @return $this
198
-	 */
199
-	public function required($isRequired=true)
200
-	{
201
-		if ($isRequired) {
202
-			$this->attributes['required'] = true;
203
-		} else {
204
-			unset($this->attributes['required']);
205
-		}
206
-
207
-		return $this;
208
-	}
209
-
210
-	/**
211
-	 * Check if a field is unwrappable (no label)
212
-	 *
213
-	 * @return boolean
214
-	 */
215
-	public function isUnwrappable()
216
-	{
217
-		return
218
-			($this->form and $this->currentFramework()->is('Nude')) or
219
-			($this->form and $this->isOfType('inline')) or
220
-			$this->isButton() or
221
-			$this->isOfType('hidden') or
222
-			\Former\Form\Group::$opened or
223
-			$this->group and $this->group->isRaw();
224
-	}
225
-
226
-	/**
227
-	 * Check if field is a checkbox or a radio
228
-	 *
229
-	 * @return boolean
230
-	 */
231
-	public function isCheckable()
232
-	{
233
-		return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios');
234
-	}
235
-
236
-	/**
237
-	 * Check if the field is a button
238
-	 *
239
-	 * @return boolean
240
-	 */
241
-	public function isButton()
242
-	{
243
-		return false;
244
-	}
245
-
246
-	/**
247
-	 * Get the rules applied to the current field
248
-	 *
249
-	 * @return array An array of rules
250
-	 */
251
-	public function getRules()
252
-	{
253
-		return $this->app['former']->getRules($this->name);
254
-	}
255
-
256
-	////////////////////////////////////////////////////////////////////
257
-	//////////////////////// SETTERS AND GETTERS ///////////////////////
258
-	////////////////////////////////////////////////////////////////////
259
-
260
-	/**
261
-	 * Apply a Live Validation rule by chaining
262
-	 *
263
-	 * @param string $rule The rule
264
-	 */
265
-	public function rule($rule)
266
-	{
267
-		$parameters = func_get_args();
268
-		array_shift($parameters);
269
-
270
-		$live = new LiveValidation($this);
271
-		$live->apply(array(
272
-			$rule => $parameters,
273
-		));
274
-
275
-		return $this;
276
-	}
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
+     * Get the current framework instance
70
+     *
71
+     * @return Framework
72
+     */
73
+    protected function currentFramework()
74
+    {
75
+        if ($this->app->bound('former.form.framework')) {
76
+            return $this->app['former.form.framework'];
77
+        }
78
+
79
+        return $this->app['former.framework'];
80
+    }
81
+
82
+    ////////////////////////////////////////////////////////////////////
83
+    ///////////////////////////// INTERFACE ////////////////////////////
84
+    ////////////////////////////////////////////////////////////////////
85
+
86
+    /**
87
+     * Set up a Field instance
88
+     *
89
+     * @param string $type A field type
90
+     */
91
+    public function __construct(Container $app, $type, $name, $label, $value, $attributes)
92
+    {
93
+        // Set base parameters
94
+        $this->app   = $app;
95
+        $this->type  = $type;
96
+        $this->value = $value;
97
+        $this->setAttributes($attributes);
98
+        $this->form = $this->app->bound('former.form') ? $this->app['former.form'] : null;
99
+
100
+        // Compute and translate label
101
+        $this->automaticLabels($name, $label);
102
+
103
+        // Repopulate field
104
+        if ($type != 'password' && $name !== '_token') {
105
+            $this->value = $this->repopulate();
106
+        }
107
+
108
+        // Apply Live validation rules
109
+        if ($this->app['former']->getOption('live_validation')) {
110
+            $rules = new LiveValidation($this);
111
+            $rules->apply($this->getRules());
112
+        }
113
+
114
+        // Bind the Group class
115
+        $groupClass = $this->isCheckable() ? 'CheckableGroup' : 'Group';
116
+        $groupClass = Former::FORMSPACE.$groupClass;
117
+
118
+        $this->group = new $groupClass($this->app, $this->label);
119
+    }
120
+
121
+    /**
122
+     * Redirect calls to the group if necessary
123
+     *
124
+     * @param string $method
125
+     */
126
+    public function __call($method, $parameters)
127
+    {
128
+        // Translate attributes
129
+        $translatable = $this->app['former']->getOption('translatable', array());
130
+        if (in_array($method, $translatable) and isset($parameters[0])) {
131
+            $parameters[0] = Helpers::translate($parameters[0]);
132
+        }
133
+
134
+        // Redirect calls to the Control Group
135
+        if ((!empty($this->group) && method_exists($this->group, $method)) or Str::startsWith($method, 'onGroup')) {
136
+            $method = str_replace('onGroup', '', $method);
137
+            $method = lcfirst($method);
138
+
139
+            call_user_func_array(array($this->group, $method), $parameters);
140
+
141
+            return $this;
142
+        }
143
+
144
+        return parent::__call($method, $parameters);
145
+    }
146
+
147
+    /**
148
+     * Prints out the field, wrapped in its group
149
+     *
150
+     * @return string
151
+     */
152
+    public function wrapAndRender()
153
+    {
154
+        // Dry syntax (hidden fields, plain fields)
155
+        if ($this->isUnwrappable()) {
156
+            $html = $this->render();
157
+            // Control group syntax
158
+        } elseif (Form::hasInstanceOpened()) {
159
+            $html = $this->group->wrapField($this);
160
+            // Classic syntax
161
+        } else {
162
+            $html = $this->currentFramework()->createLabelOf($this);
163
+            $html .= $this->render();
164
+        }
165
+
166
+        return $html;
167
+    }
168
+
169
+    /**
170
+     * Prints out the field
171
+     *
172
+     * @return string
173
+     */
174
+    public function __toString()
175
+    {
176
+        return $this->wrapAndRender();
177
+    }
178
+
179
+    ////////////////////////////////////////////////////////////////////
180
+    ////////////////////////// PUBLIC INTERFACE ////////////////////////
181
+    ////////////////////////////////////////////////////////////////////
182
+
183
+    /**
184
+     * Whether the current field is required or not
185
+     *
186
+     * @return boolean
187
+     */
188
+    public function isRequired()
189
+    {
190
+        return isset($this->attributes['required']);
191
+    }
192
+
193
+    /**
194
+     * Set required live validation attribute
195
+     *
196
+     * @param boolean $isRequired
197
+     * @return $this
198
+     */
199
+    public function required($isRequired=true)
200
+    {
201
+        if ($isRequired) {
202
+            $this->attributes['required'] = true;
203
+        } else {
204
+            unset($this->attributes['required']);
205
+        }
206
+
207
+        return $this;
208
+    }
209
+
210
+    /**
211
+     * Check if a field is unwrappable (no label)
212
+     *
213
+     * @return boolean
214
+     */
215
+    public function isUnwrappable()
216
+    {
217
+        return
218
+            ($this->form and $this->currentFramework()->is('Nude')) or
219
+            ($this->form and $this->isOfType('inline')) or
220
+            $this->isButton() or
221
+            $this->isOfType('hidden') or
222
+            \Former\Form\Group::$opened or
223
+            $this->group and $this->group->isRaw();
224
+    }
225
+
226
+    /**
227
+     * Check if field is a checkbox or a radio
228
+     *
229
+     * @return boolean
230
+     */
231
+    public function isCheckable()
232
+    {
233
+        return $this->isOfType('checkbox', 'checkboxes', 'radio', 'radios');
234
+    }
235
+
236
+    /**
237
+     * Check if the field is a button
238
+     *
239
+     * @return boolean
240
+     */
241
+    public function isButton()
242
+    {
243
+        return false;
244
+    }
245
+
246
+    /**
247
+     * Get the rules applied to the current field
248
+     *
249
+     * @return array An array of rules
250
+     */
251
+    public function getRules()
252
+    {
253
+        return $this->app['former']->getRules($this->name);
254
+    }
255
+
256
+    ////////////////////////////////////////////////////////////////////
257
+    //////////////////////// SETTERS AND GETTERS ///////////////////////
258
+    ////////////////////////////////////////////////////////////////////
259
+
260
+    /**
261
+     * Apply a Live Validation rule by chaining
262
+     *
263
+     * @param string $rule The rule
264
+     */
265
+    public function rule($rule)
266
+    {
267
+        $parameters = func_get_args();
268
+        array_shift($parameters);
269
+
270
+        $live = new LiveValidation($this);
271
+        $live->apply(array(
272
+            $rule => $parameters,
273
+        ));
274
+
275
+        return $this;
276
+    }
277 277
 
278 278
     /**
279 279
      * Apply multiple rules passed as a string.
@@ -295,9 +295,9 @@  discard block
 block discarded – undo
295 295
                 $rulename = substr($rule, 0, $colon) ;
296 296
 
297 297
                 /**
298
-                * Regular expressions may contain commas and should not be divided by str_getcsv.
299
-                * For regular expressions we are just using the complete expression as a parameter.
300
-                */
298
+                 * Regular expressions may contain commas and should not be divided by str_getcsv.
299
+                 * For regular expressions we are just using the complete expression as a parameter.
300
+                 */
301 301
                 if ($rulename !== 'regex') {
302 302
                     $parameters = str_getcsv(substr($rule, $colon + 1));
303 303
                 } else {
@@ -319,149 +319,149 @@  discard block
 block discarded – undo
319 319
         return $this;
320 320
     }
321 321
 
322
-	/**
323
-	 * Adds a label to the group/field
324
-	 *
325
-	 * @param  string $text       A label
326
-	 * @param  array  $attributes The label's attributes
327
-	 *
328
-	 * @return Field              A field
329
-	 */
330
-	public function label($text, $attributes = array())
331
-	{
332
-		// Create the Label element
333
-		$for   = $this->id ?: $this->name;
334
-		$label = $this->app['former']->label($text, $for, $attributes);
335
-
336
-		// Set label
337
-		$this->label = $label;
338
-		if ($this->group) {
339
-			$this->group->setLabel($label);
340
-		}
341
-
342
-		return $this;
343
-	}
344
-
345
-	/**
346
-	 * Set the Field value no matter what
347
-	 *
348
-	 * @param string $value A new value
349
-	 */
350
-	public function forceValue($value)
351
-	{
352
-		$this->value = $value;
353
-
354
-		return $this;
355
-	}
356
-
357
-	/**
358
-	 * Classic setting of attribute, won't overwrite any populate() attempt
359
-	 *
360
-	 * @param  string $value A new value
361
-	 */
362
-	public function value($value)
363
-	{
364
-		// Check if we already have a value stored for this field or in POST data
365
-		$already = $this->repopulate();
366
-
367
-		if (!$already) {
368
-			$this->value = $value;
369
-		}
370
-
371
-		return $this;
372
-	}
373
-
374
-	/**
375
-	 * Change the field's name
376
-	 *
377
-	 * @param  string $name The new name
378
-	 */
379
-	public function name($name)
380
-	{
381
-		$this->name = $name;
382
-
383
-		// Also relink the label to the new name
384
-		$this->label($name);
385
-
386
-		return $this;
387
-	}
388
-
389
-	/**
390
-	 * Get the field's labels
391
-	 *
392
-	 * @return string
393
-	 */
394
-	public function getLabel()
395
-	{
396
-		return $this->label;
397
-	}
398
-
399
-	/**
400
-	 * Change the field's bind destination
401
-	 *
402
-	 * @param $destination
403
-	 */
404
-	public function bind($destination) {
405
-		$this->bind = $destination;
406
-		if ($this->type != 'password') {
407
-			$this->value = $this->repopulate();
408
-		}
409
-
410
-		return $this;
411
-	}
412
-
413
-	////////////////////////////////////////////////////////////////////
414
-	//////////////////////////////// HELPERS ///////////////////////////
415
-	////////////////////////////////////////////////////////////////////
416
-
417
-	/**
418
-	 * Use values stored in Former to populate the current field
419
-	 */
420
-	private function repopulate($fallback = null)
421
-	{
422
-		// Get values from POST, populated, and manually set value
423
-		$post      = $this->app['former']->getPost($this->name);
424
-		$populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator'];
425
-		$populate  = $populator->get($this->bind ?: $this->name);
426
-
427
-		// Assign a priority to each
428
-		if (!is_null($post)) {
429
-			return $post;
430
-		}
431
-		if (!is_null($populate)) {
432
-			return $populate;
433
-		}
434
-
435
-		return $fallback ?: $this->value;
436
-	}
437
-
438
-	/**
439
-	 * Ponders a label and a field name, and tries to get the best out of it
440
-	 *
441
-	 * @param  string $label A label
442
-	 * @param  string $name  A field name
443
-	 *
444
-	 * @return false|null         A label and a field name
445
-	 */
446
-	private function automaticLabels($name, $label)
447
-	{
448
-		// Disabled automatic labels
449
-		if (!$this->app['former']->getOption('automatic_label')) {
450
-			$this->name = $name;
451
-			$this->label($label);
452
-
453
-			return false;
454
-		}
455
-
456
-		// Check for the two possibilities
457
-		if ($label and is_null($name)) {
458
-			$name = Str::slug($label);
459
-		} elseif (is_null($label) and $name) {
460
-			$label = preg_replace('/\[\]$/', '', $name);
461
-		}
462
-
463
-		// Save values
464
-		$this->name = $name;
465
-		$this->label($label);
466
-	}
322
+    /**
323
+     * Adds a label to the group/field
324
+     *
325
+     * @param  string $text       A label
326
+     * @param  array  $attributes The label's attributes
327
+     *
328
+     * @return Field              A field
329
+     */
330
+    public function label($text, $attributes = array())
331
+    {
332
+        // Create the Label element
333
+        $for   = $this->id ?: $this->name;
334
+        $label = $this->app['former']->label($text, $for, $attributes);
335
+
336
+        // Set label
337
+        $this->label = $label;
338
+        if ($this->group) {
339
+            $this->group->setLabel($label);
340
+        }
341
+
342
+        return $this;
343
+    }
344
+
345
+    /**
346
+     * Set the Field value no matter what
347
+     *
348
+     * @param string $value A new value
349
+     */
350
+    public function forceValue($value)
351
+    {
352
+        $this->value = $value;
353
+
354
+        return $this;
355
+    }
356
+
357
+    /**
358
+     * Classic setting of attribute, won't overwrite any populate() attempt
359
+     *
360
+     * @param  string $value A new value
361
+     */
362
+    public function value($value)
363
+    {
364
+        // Check if we already have a value stored for this field or in POST data
365
+        $already = $this->repopulate();
366
+
367
+        if (!$already) {
368
+            $this->value = $value;
369
+        }
370
+
371
+        return $this;
372
+    }
373
+
374
+    /**
375
+     * Change the field's name
376
+     *
377
+     * @param  string $name The new name
378
+     */
379
+    public function name($name)
380
+    {
381
+        $this->name = $name;
382
+
383
+        // Also relink the label to the new name
384
+        $this->label($name);
385
+
386
+        return $this;
387
+    }
388
+
389
+    /**
390
+     * Get the field's labels
391
+     *
392
+     * @return string
393
+     */
394
+    public function getLabel()
395
+    {
396
+        return $this->label;
397
+    }
398
+
399
+    /**
400
+     * Change the field's bind destination
401
+     *
402
+     * @param $destination
403
+     */
404
+    public function bind($destination) {
405
+        $this->bind = $destination;
406
+        if ($this->type != 'password') {
407
+            $this->value = $this->repopulate();
408
+        }
409
+
410
+        return $this;
411
+    }
412
+
413
+    ////////////////////////////////////////////////////////////////////
414
+    //////////////////////////////// HELPERS ///////////////////////////
415
+    ////////////////////////////////////////////////////////////////////
416
+
417
+    /**
418
+     * Use values stored in Former to populate the current field
419
+     */
420
+    private function repopulate($fallback = null)
421
+    {
422
+        // Get values from POST, populated, and manually set value
423
+        $post      = $this->app['former']->getPost($this->name);
424
+        $populator = $this->form ? $this->form->getPopulator() : $this->app['former.populator'];
425
+        $populate  = $populator->get($this->bind ?: $this->name);
426
+
427
+        // Assign a priority to each
428
+        if (!is_null($post)) {
429
+            return $post;
430
+        }
431
+        if (!is_null($populate)) {
432
+            return $populate;
433
+        }
434
+
435
+        return $fallback ?: $this->value;
436
+    }
437
+
438
+    /**
439
+     * Ponders a label and a field name, and tries to get the best out of it
440
+     *
441
+     * @param  string $label A label
442
+     * @param  string $name  A field name
443
+     *
444
+     * @return false|null         A label and a field name
445
+     */
446
+    private function automaticLabels($name, $label)
447
+    {
448
+        // Disabled automatic labels
449
+        if (!$this->app['former']->getOption('automatic_label')) {
450
+            $this->name = $name;
451
+            $this->label($label);
452
+
453
+            return false;
454
+        }
455
+
456
+        // Check for the two possibilities
457
+        if ($label and is_null($name)) {
458
+            $name = Str::slug($label);
459
+        } elseif (is_null($label) and $name) {
460
+            $label = preg_replace('/\[\]$/', '', $name);
461
+        }
462
+
463
+        // Save values
464
+        $this->name = $name;
465
+        $this->label($label);
466
+    }
467 467
 }
Please login to merge, or discard this patch.
src/Former/Form/Fields/Choice.php 3 patches
Doc Comments   +4 added lines, -7 removed lines patch added patch discarded remove patch
@@ -188,6 +188,9 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 	 */
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -217,7 +217,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 		}
Please login to merge, or discard this patch.
Indentation   +468 added lines, -468 removed lines patch added patch discarded remove patch
@@ -13,477 +13,477 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.