44
55namespace Flow \Types \Type \Logical ;
66
7- use function Flow \Types \DSL \type_string ;
7+ use function Flow \Types \DSL \{ type_string , type_xml } ;
88use Flow \Types \Exception \{CastingException , InvalidTypeException };
99use Flow \Types \Type ;
1010
@@ -28,24 +28,26 @@ public function cast(mixed $value) : \DOMDocument
2828 return $ value ;
2929 }
3030
31- $ document = new \DOMDocument ();
32-
3331 if (\is_string ($ value )) {
34- if (!$ this ->isXml ($ document , $ value )) {
35- throw new CastingException ($ value , $ this );
32+ $ doc = new \DOMDocument ();
33+
34+ if (!@$ doc ->loadXML ($ value )) {
35+ throw new CastingException ($ value , type_xml ());
3636 }
3737
38- return $ document ;
38+ return $ doc ;
3939 }
4040
4141 try {
4242 $ stringValue = type_string ()->cast ($ value );
4343
44- if (!$ this ->isXml ($ document , $ stringValue )) {
44+ $ doc = new \DOMDocument ();
45+
46+ if (!@$ doc ->loadXML ((string ) $ stringValue )) {
4547 throw new CastingException ($ stringValue , $ this );
4648 }
4749
48- return $ document ;
50+ return $ doc ;
4951 } catch (CastingException $ e ) {
5052 throw new CastingException ($ value , $ this , $ e );
5153 }
@@ -54,7 +56,7 @@ public function cast(mixed $value) : \DOMDocument
5456 public function isValid (mixed $ value ) : bool
5557 {
5658 if ($ value instanceof \DOMDocument) {
57- return $ this -> isXml ( $ value , ( string ) $ value -> saveXML ()) ;
59+ return true ;
5860 }
5961
6062 return false ;
@@ -71,9 +73,4 @@ public function toString() : string
7173 {
7274 return 'xml ' ;
7375 }
74-
75- private function isXml (\DOMDocument $ document , string $ value ) : bool
76- {
77- return (bool ) @$ document ->loadXML ($ value );
78- }
7976}
0 commit comments