33namespace webignition \InternetMediaType \Parser ;
44
55use webignition \InternetMediaType \InternetMediaType ;
6- use webignition \InternetMediaType \Parameter \Parameter ;
76use webignition \InternetMediaType \Parameter \Parser \AttributeFixer ;
87use webignition \InternetMediaType \Parameter \Parser \AttributeParser ;
98use webignition \InternetMediaType \Parameter \Parser \AttributeParserException ;
@@ -24,7 +23,8 @@ class Parser
2423 public const TYPE_SUBTYPE_SEPARATOR = '/ ' ;
2524 public const TYPE_PARAMETER_SEPARATOR = '; ' ;
2625
27- private Configuration $ configuration ;
26+ private bool $ ignoreInvalidAttributes = false ;
27+ private bool $ attemptToRecoverFromInvalidInternalCharacter = false ;
2828 private bool $ hasAttemptedToFixSubtypeInvalidInternalCharacter = false ;
2929 private bool $ hasAttemptedToFixAttributeInvalidInternalCharacter = false ;
3030
@@ -35,7 +35,6 @@ public function __construct(
3535 private TypeFixer $ typeFixer ,
3636 private AttributeFixer $ attributeFixer ,
3737 ) {
38- $ this ->configuration = new Configuration ();
3938 }
4039
4140 public static function create (): Parser
@@ -110,33 +109,15 @@ public function parse(string $internetMediaTypeString): ?InternetMediaTypeInterf
110109 }
111110 }
112111
113- public function setConfiguration (Configuration $ configuration ): void
114- {
115- $ this ->configuration = $ configuration ;
116- }
117-
118- public function getConfiguration (): Configuration
119- {
120- return $ this ->configuration ;
121- }
122-
123112 public function setIgnoreInvalidAttributes (bool $ ignoreInvalidAttributes ): void
124113 {
125- if (filter_var ($ ignoreInvalidAttributes , FILTER_VALIDATE_BOOLEAN )) {
126- $ this ->getConfiguration ()->enableIgnoreInvalidAttributes ();
127- } else {
128- $ this ->getConfiguration ()->disableIgnoreInvalidAttributes ();
129- }
114+ $ this ->ignoreInvalidAttributes = $ ignoreInvalidAttributes ;
130115 }
131116
132117 public function setAttemptToRecoverFromInvalidInternalCharacter (
133118 bool $ attemptToRecoverFromInvalidInternalCharacter
134119 ): void {
135- if (filter_var ($ attemptToRecoverFromInvalidInternalCharacter , FILTER_VALIDATE_BOOLEAN )) {
136- $ this ->getConfiguration ()->enableAttemptToRecoverFromInvalidInternalCharacter ();
137- } else {
138- $ this ->getConfiguration ()->disableAttemptToRecoverFromInvalidInternalCharacter ();
139- }
120+ $ this ->attemptToRecoverFromInvalidInternalCharacter = $ attemptToRecoverFromInvalidInternalCharacter ;
140121 }
141122
142123 private function createParameterString (string $ inputString , string $ type , string $ subtype ): string
@@ -205,7 +186,7 @@ private function parseSubtype(string $inputString): string
205186 return $ this ->subtypeParser ->parse ($ inputString );
206187 } catch (SubtypeParserException $ subtypeParserException ) {
207188 $ shouldAttemptToFixInvalidInternalCharacter =
208- $ this ->getConfiguration ()-> attemptToRecoverFromInvalidInternalCharacter ()
189+ $ this ->attemptToRecoverFromInvalidInternalCharacter
209190 && !$ this ->hasAttemptedToFixSubtypeInvalidInternalCharacter ;
210191
211192 if ($ shouldAttemptToFixInvalidInternalCharacter ) {
@@ -218,6 +199,8 @@ private function parseSubtype(string $inputString): string
218199 }
219200 }
220201
202+ $ this ->hasAttemptedToFixSubtypeInvalidInternalCharacter = false ;
203+
221204 throw $ subtypeParserException ;
222205 }
223206 }
@@ -232,13 +215,12 @@ private function parseParameterString(string $parameterString): ?ParameterInterf
232215 try {
233216 return $ this ->parameterParser ->parse ($ parameterString );
234217 } catch (AttributeParserException $ attributeParserException ) {
235- $ shouldIgnoreInvalidAttributes = $ this ->getConfiguration ()->ignoreInvalidAttributes ();
236- if ($ shouldIgnoreInvalidAttributes ) {
218+ if ($ this ->ignoreInvalidAttributes ) {
237219 return null ;
238220 }
239221
240222 $ shouldAttemptToFixInvalidInternalCharacter =
241- $ this ->getConfiguration ()-> attemptToRecoverFromInvalidInternalCharacter ()
223+ $ this ->attemptToRecoverFromInvalidInternalCharacter
242224 && !$ this ->hasAttemptedToFixAttributeInvalidInternalCharacter ;
243225
244226 if ($ shouldAttemptToFixInvalidInternalCharacter ) {
@@ -249,6 +231,8 @@ private function parseParameterString(string $parameterString): ?ParameterInterf
249231 return $ this ->parameterParser ->parse ($ fixedInputString );
250232 }
251233
234+ $ this ->hasAttemptedToFixAttributeInvalidInternalCharacter = false ;
235+
252236 throw $ attributeParserException ;
253237 }
254238 }
0 commit comments