@@ -102,31 +102,41 @@ public function setValues($data, bool $erase = false)
102102
103103 /**
104104 * Returns the values submitted by the form.
105- * @param string|null $returnType 'array' for array
105+ * @param string|object|null $returnType 'array' for array
106+
106107 * @return object|array
107108 */
108109 public function getValues ($ returnType = null )
109110 {
110- $ returnType = $ returnType
111- ? ( $ returnType === true ? self ::ARRAY : $ returnType ) // back compatibility
112- : ( $ this -> mappedType ?? ArrayHash::class) ;
111+ if ( $ returnType === self :: ARRAY || $ returnType === true || $ this -> mappedType === self :: ARRAY ) {
112+ $ returnType = self ::ARRAY ;
113+ $ obj = new \ stdClass ;
113114
114- $ isArray = $ returnType === self ::ARRAY ;
115- $ obj = $ isArray ? new \stdClass : new $ returnType ;
116- $ rc = new \ReflectionClass ($ obj );
115+ } elseif (is_object ($ returnType )) {
116+ $ obj = $ returnType ;
117+
118+ } else {
119+ $ returnType = ($ returnType ?? $ this ->mappedType ?? ArrayHash::class);
120+ $ obj = new $ returnType ;
121+ }
117122
123+ $ rc = new \ReflectionClass ($ obj );
118124 foreach ($ this ->getComponents () as $ name => $ control ) {
119125 $ name = (string ) $ name ;
120126 if ($ control instanceof Control && !$ control ->isOmitted ()) {
121127 $ obj ->$ name = $ control ->getValue ();
128+
122129 } elseif ($ control instanceof self) {
123- $ type = $ isArray && !$ control ->mappedType
130+ $ type = $ returnType === self :: ARRAY && !$ control ->mappedType
124131 ? self ::ARRAY
125132 : ($ rc ->hasProperty ($ name ) ? Nette \Utils \Reflection::getPropertyType ($ rc ->getProperty ($ name )) : null );
126133 $ obj ->$ name = $ control ->getValues ($ type );
127134 }
128135 }
129- return $ isArray ? (array ) $ obj : $ obj ;
136+
137+ return $ returnType === self ::ARRAY
138+ ? (array ) $ obj
139+ : $ obj ;
130140 }
131141
132142
0 commit comments