@@ -101,50 +101,11 @@ public function encode(string $decoded): string
101101
102102 public function decode (string $ encoded , bool $ strict = false ): string
103103 {
104+ [$ encoded , $ alphabet , $ padding ] = $ this ->prepareDecoding ($ encoded , $ strict );
104105 if ('' === $ encoded ) {
105106 return '' ;
106107 }
107108
108- $ alphabet = $ this ->alphabet ;
109- $ padding = $ this ->padding ;
110- $ encoded = str_replace (str_split (self ::RESERVED_CHARACTERS ), ['' ], $ encoded );
111- if (!$ strict ) {
112- $ alphabet = strtoupper ($ alphabet );
113- $ padding = strtoupper ($ padding );
114- $ encoded = strtoupper ($ encoded );
115- }
116-
117- $ inside = rtrim ($ encoded , $ padding );
118- $ end = substr ($ encoded , strlen ($ inside ));
119- if ($ strict ) {
120- $ endLength = strlen ($ end );
121- if (0 !== $ endLength && 1 !== $ endLength && 3 !== $ endLength && 4 !== $ endLength && 6 !== $ endLength ) {
122- throw new RuntimeException ('The encoded data ends with an invalid padding sequence length. ' );
123- }
124- }
125-
126- if (false !== strpos ($ inside , $ padding )) {
127- if ($ strict ) {
128- throw new RuntimeException ('The padding character is used in the encoded data in an invalid place. ' );
129- }
130-
131- $ encoded = str_replace ($ padding , '' , $ inside ).$ end ;
132- }
133-
134- $ remainder = strlen ($ encoded ) % 8 ;
135- if (0 !== $ remainder ) {
136- if ($ strict ) {
137- throw new RuntimeException ('The encoded data length is invalid. ' );
138- }
139-
140- $ remainderStr = '' ;
141- for ($ index = 0 ; $ index < $ remainder ; $ index ++) {
142- $ remainderStr .= $ padding ;
143- }
144-
145- $ encoded .= $ remainderStr ;
146- }
147-
148109 $ chars = [];
149110 for ($ index = 0 ; $ index < self ::ALPHABET_SIZE ; $ index ++) {
150111 $ chars [$ alphabet [$ index ]] = $ index ;
@@ -197,4 +158,60 @@ public function decode(string $encoded, bool $strict = false): string
197158
198159 return $ decoded ;
199160 }
161+
162+ /**
163+ * @return array<string>
164+ */
165+ private function prepareDecoding (string $ encoded , bool $ strict ): array
166+ {
167+ if ('' === $ encoded ) {
168+ return ['' , $ this ->alphabet , $ this ->padding ];
169+ }
170+
171+ $ alphabet = $ this ->alphabet ;
172+ $ padding = $ this ->padding ;
173+ $ encoded = str_replace (str_split (self ::RESERVED_CHARACTERS ), ['' ], $ encoded );
174+ if (!$ strict ) {
175+ $ alphabet = strtoupper ($ alphabet );
176+ $ padding = strtoupper ($ padding );
177+ $ encoded = strtoupper ($ encoded );
178+ }
179+
180+ $ inside = rtrim ($ encoded , $ padding );
181+ $ end = substr ($ encoded , strlen ($ inside ));
182+ if ($ strict ) {
183+ $ endLength = strlen ($ end );
184+ if (0 !== $ endLength && 1 !== $ endLength && 3 !== $ endLength && 4 !== $ endLength && 6 !== $ endLength ) {
185+ throw new RuntimeException ('The encoded data ends with an invalid padding sequence length. ' );
186+ }
187+ }
188+
189+ if (false !== strpos ($ inside , $ padding )) {
190+ if ($ strict ) {
191+ throw new RuntimeException ('The padding character is used in the encoded data in an invalid place. ' );
192+ }
193+
194+ $ encoded = str_replace ($ padding , '' , $ inside ).$ end ;
195+ }
196+
197+ if ('' === $ encoded ) {
198+ return [$ encoded , $ alphabet , $ padding ];
199+ }
200+
201+ $ remainder = strlen ($ encoded ) % 8 ;
202+ if (0 !== $ remainder ) {
203+ if ($ strict ) {
204+ throw new RuntimeException ('The encoded data length is invalid. ' );
205+ }
206+
207+ $ remainderStr = '' ;
208+ for ($ index = 0 ; $ index < $ remainder ; $ index ++) {
209+ $ remainderStr .= $ padding ;
210+ }
211+
212+ $ encoded .= $ remainderStr ;
213+ }
214+
215+ return [$ encoded , $ alphabet , $ padding ];
216+ }
200217}
0 commit comments