@@ -75,8 +75,11 @@ public static function create(string $class, array $data = [])
7575
7676 $ reflectionClass = new \ReflectionClass ($ class );
7777 $ object = $ reflectionClass ->newInstance ($ response );
78- $ data ['initialized ' ] = true ;
78+ if (Result::class !== $ class ) {
79+ self ::addPropertiesOnResult ($ reflectionClass , $ object , $ class );
80+ }
7981
82+ $ data ['initialized ' ] = true ;
8083 foreach ($ data as $ propertyName => $ propertyValue ) {
8184 $ property = $ reflectionClass ->getProperty ($ propertyName );
8285 $ property ->setAccessible (true );
@@ -144,4 +147,49 @@ private static function addUndefinedProperties(\ReflectionClass $reflectionClass
144147 }
145148 }
146149 }
150+
151+ /**
152+ * Set input and aws client to handle pagination.
153+ */
154+ private static function addPropertiesOnResult (\ReflectionClass $ reflectionClass , object $ object , string $ class ): void
155+ {
156+ if (false === $ pos = strrpos ($ class , '\\' )) {
157+ throw new \LogicException (sprintf ('Expected class "%s" to have a backslash. ' , $ class ));
158+ }
159+
160+ $ className = substr ($ class , $ pos + 1 );
161+ if ('Output ' === substr ($ className , -6 )) {
162+ $ classNameWithoutSuffix = substr ($ className , 0 , -6 );
163+ } elseif ('Response ' === substr ($ className , -8 )) {
164+ $ classNameWithoutSuffix = substr ($ className , 0 , -8 );
165+ } else {
166+ throw new \LogicException (sprintf ('Unknown class suffix: "%s" ' , $ className ));
167+ }
168+
169+ if (false === $ pos = strrpos ($ class , '\\' , -2 - \strlen ($ className ))) {
170+ throw new \LogicException (sprintf ('Expected class "%s" to have more than one backslash. ' , $ class ));
171+ }
172+
173+ $ baseNamespace = substr ($ class , 0 , $ pos );
174+ if (false === $ pos = strrpos ($ baseNamespace , '\\' )) {
175+ throw new \LogicException (sprintf ('Expected base namespace "%s" to have a backslash. ' , $ baseNamespace ));
176+ }
177+
178+ $ awsClientClass = $ baseNamespace . (substr ($ baseNamespace , $ pos )) . 'Client ' ;
179+ $ inputClass = $ baseNamespace . '\\Input \\' . $ classNameWithoutSuffix . 'Request ' ;
180+
181+ if (class_exists ($ awsClientClass )) {
182+ $ awsClientMock = (new \ReflectionClass ($ awsClientClass ))->newInstanceWithoutConstructor ();
183+ $ property = $ reflectionClass ->getProperty ('awsClient ' );
184+ $ property ->setAccessible (true );
185+ $ property ->setValue ($ object , $ awsClientMock );
186+ }
187+
188+ if (class_exists ($ inputClass )) {
189+ $ inputMock = (new \ReflectionClass ($ inputClass ))->newInstanceWithoutConstructor ();
190+ $ property = $ reflectionClass ->getProperty ('input ' );
191+ $ property ->setAccessible (true );
192+ $ property ->setValue ($ object , $ inputMock );
193+ }
194+ }
147195}
0 commit comments