@@ -40,7 +40,6 @@ class File extends Constraint
40
40
self ::INVALID_MIME_TYPE_ERROR => 'INVALID_MIME_TYPE_ERROR ' ,
41
41
);
42
42
43
- public $ maxSize ;
44
43
public $ binaryFormat ;
45
44
public $ mimeTypes = array ();
46
45
public $ notFoundMessage = 'The file could not be found. ' ;
@@ -58,29 +57,56 @@ class File extends Constraint
58
57
public $ uploadExtensionErrorMessage = 'A PHP extension caused the upload to fail. ' ;
59
58
public $ uploadErrorMessage = 'The file could not be uploaded. ' ;
60
59
60
+ protected $ maxSize ;
61
+
61
62
public function __construct ($ options = null )
62
63
{
63
64
parent ::__construct ($ options );
64
65
65
- if ($ this ->maxSize ) {
66
- if (ctype_digit ((string ) $ this ->maxSize )) {
67
- $ this ->maxSize = (int ) $ this ->maxSize ;
68
- $ this ->binaryFormat = null === $ this ->binaryFormat ? false : $ this ->binaryFormat ;
69
- } elseif (preg_match ('/^\d++k$/i ' , $ this ->maxSize )) {
70
- $ this ->maxSize = $ this ->maxSize * 1000 ;
71
- $ this ->binaryFormat = null === $ this ->binaryFormat ? false : $ this ->binaryFormat ;
72
- } elseif (preg_match ('/^\d++M$/i ' , $ this ->maxSize )) {
73
- $ this ->maxSize = $ this ->maxSize * 1000000 ;
74
- $ this ->binaryFormat = null === $ this ->binaryFormat ? false : $ this ->binaryFormat ;
75
- } elseif (preg_match ('/^\d++Ki$/i ' , $ this ->maxSize )) {
76
- $ this ->maxSize = $ this ->maxSize << 10 ;
77
- $ this ->binaryFormat = null === $ this ->binaryFormat ? true : $ this ->binaryFormat ;
78
- } elseif (preg_match ('/^\d++Mi$/i ' , $ this ->maxSize )) {
79
- $ this ->maxSize = $ this ->maxSize << 20 ;
80
- $ this ->binaryFormat = null === $ this ->binaryFormat ? true : $ this ->binaryFormat ;
81
- } else {
82
- throw new ConstraintDefinitionException (sprintf ('"%s" is not a valid maximum size ' , $ this ->maxSize ));
83
- }
66
+ if (null !== $ this ->maxSize ) {
67
+ $ this ->normalizeBinaryFormat ($ this ->maxSize );
68
+ }
69
+ }
70
+
71
+ public function __set ($ option , $ value )
72
+ {
73
+ if ('maxSize ' === $ option ) {
74
+ $ this ->normalizeBinaryFormat ($ value );
75
+
76
+ return ;
77
+ }
78
+
79
+ parent ::__set ($ option , $ value );
80
+ }
81
+
82
+ public function __get ($ option )
83
+ {
84
+ if ('maxSize ' === $ option ) {
85
+ return $ this ->maxSize ;
86
+ }
87
+
88
+ return parent ::__get ($ option );
89
+ }
90
+
91
+ private function normalizeBinaryFormat ($ maxSize )
92
+ {
93
+ if (ctype_digit ((string ) $ maxSize )) {
94
+ $ this ->maxSize = (int ) $ maxSize ;
95
+ $ this ->binaryFormat = null === $ this ->binaryFormat ? false : $ this ->binaryFormat ;
96
+ } elseif (preg_match ('/^\d++k$/i ' , $ maxSize )) {
97
+ $ this ->maxSize = $ maxSize * 1000 ;
98
+ $ this ->binaryFormat = null === $ this ->binaryFormat ? false : $ this ->binaryFormat ;
99
+ } elseif (preg_match ('/^\d++M$/i ' , $ maxSize )) {
100
+ $ this ->maxSize = $ maxSize * 1000000 ;
101
+ $ this ->binaryFormat = null === $ this ->binaryFormat ? false : $ this ->binaryFormat ;
102
+ } elseif (preg_match ('/^\d++Ki$/i ' , $ maxSize )) {
103
+ $ this ->maxSize = $ maxSize << 10 ;
104
+ $ this ->binaryFormat = null === $ this ->binaryFormat ? true : $ this ->binaryFormat ;
105
+ } elseif (preg_match ('/^\d++Mi$/i ' , $ maxSize )) {
106
+ $ this ->maxSize = $ maxSize << 20 ;
107
+ $ this ->binaryFormat = null === $ this ->binaryFormat ? true : $ this ->binaryFormat ;
108
+ } else {
109
+ throw new ConstraintDefinitionException (sprintf ('"%s" is not a valid maximum size ' , $ this ->maxSize ));
84
110
}
85
111
}
86
112
}
0 commit comments