File tree Expand file tree Collapse file tree 6 files changed +98
-9
lines changed
Expand file tree Collapse file tree 6 files changed +98
-9
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace League \HTMLToMarkdown ;
6+
7+ /**
8+ * @internal
9+ */
10+ final class Coerce
11+ {
12+ private function __construct ()
13+ {
14+ }
15+
16+ /**
17+ * @param mixed $val
18+ */
19+ public static function toString ($ val ): string
20+ {
21+ switch (true ) {
22+ case \is_string ($ val ):
23+ return $ val ;
24+ case \is_bool ($ val ):
25+ case \is_float ($ val ):
26+ case \is_int ($ val ):
27+ case $ val === null :
28+ return \strval ($ val );
29+ case \is_object ($ val ) && \method_exists ($ val , '__toString ' ):
30+ return $ val ->__toString ();
31+ default :
32+ throw new \InvalidArgumentException ('Cannot coerce this value to string ' );
33+ }
34+ }
35+ }
Original file line number Diff line number Diff line change 44
55namespace League \HTMLToMarkdown \Converter ;
66
7+ use League \HTMLToMarkdown \Coerce ;
78use League \HTMLToMarkdown \Configuration ;
89use League \HTMLToMarkdown \ConfigurationAwareInterface ;
910use League \HTMLToMarkdown \ElementInterface ;
@@ -38,8 +39,8 @@ public function convert(ElementInterface $element): string
3839 }
3940
4041 if ($ listType === 'ul ' ) {
41- $ listItemStyle = \strval ($ this ->config ->getOption ('list_item_style ' , '- ' ));
42- $ listItemStyleAlternate = \strval ($ this ->config ->getOption ('list_item_style_alternate ' , '' ));
42+ $ listItemStyle = Coerce:: toString ($ this ->config ->getOption ('list_item_style ' , '- ' ));
43+ $ listItemStyleAlternate = Coerce:: toString ($ this ->config ->getOption ('list_item_style_alternate ' , '' ));
4344 if (! isset ($ this ->listItemStyle )) {
4445 $ this ->listItemStyle = $ listItemStyleAlternate ?: $ listItemStyle ;
4546 }
Original file line number Diff line number Diff line change 44
55namespace League \HTMLToMarkdown \Converter ;
66
7+ use League \HTMLToMarkdown \Coerce ;
78use League \HTMLToMarkdown \Configuration ;
89use League \HTMLToMarkdown \ConfigurationAwareInterface ;
910use League \HTMLToMarkdown \ElementInterface ;
@@ -89,7 +90,7 @@ public function convert(ElementInterface $element): string
8990 }
9091
9192 $ value = \str_replace ("\n" , ' ' , $ value );
92- $ value = \str_replace ('| ' , \strval ($ this ->config ->getOption ('table_pipe_escape ' ) ?? '\| ' ), $ value );
93+ $ value = \str_replace ('| ' , Coerce:: toString ($ this ->config ->getOption ('table_pipe_escape ' ) ?? '\| ' ), $ value );
9394
9495 return '| ' . \trim ($ value ) . ' ' ;
9596 case 'thead ' :
Original file line number Diff line number Diff line change @@ -144,11 +144,7 @@ public function isDescendantOf($tagNames): bool
144144 $ tagNames = [$ tagNames ];
145145 }
146146
147- for ($ p = $ this ->node ->parentNode ; $ p !== false ; $ p = $ p ->parentNode ) {
148- if ($ p === null ) {
149- return false ;
150- }
151-
147+ for ($ p = $ this ->node ->parentNode ; $ p !== null ; $ p = $ p ->parentNode ) {
152148 if (\in_array ($ p ->nodeName , $ tagNames , true )) {
153149 return true ;
154150 }
Original file line number Diff line number Diff line change @@ -212,7 +212,7 @@ protected function convertToMarkdown(ElementInterface $element): string
212212 $ tag = $ element ->getTagName ();
213213
214214 // Strip nodes named in remove_nodes
215- $ tagsToRemove = \explode (' ' , \strval ($ this ->getConfig ()->getOption ('remove_nodes ' ) ?? '' ));
215+ $ tagsToRemove = \explode (' ' , Coerce:: toString ($ this ->getConfig ()->getOption ('remove_nodes ' ) ?? '' ));
216216 if (\in_array ($ tag , $ tagsToRemove , true )) {
217217 return '' ;
218218 }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace League \HTMLToMarkdown \Test ;
6+
7+ use League \HTMLToMarkdown \Coerce ;
8+ use PHPUnit \Framework \TestCase ;
9+
10+ final class CoerceTest extends TestCase
11+ {
12+ /**
13+ * @dataProvider provideStringTestCases
14+ *
15+ * @param mixed $val
16+ */
17+ public function testToString ($ val , string $ expected ): void
18+ {
19+ $ this ->assertSame ($ expected , Coerce::toString ($ val ));
20+ }
21+
22+ public function provideStringTestCases (): \Generator
23+ {
24+ yield ['foo ' , 'foo ' ];
25+ yield [1 , '1 ' ];
26+ yield [1.1 , '1.1 ' ];
27+ yield [true , '1 ' ];
28+ yield [false , '' ];
29+ yield [null , '' ];
30+ yield [$ this , $ this ->__toString ()];
31+ }
32+
33+ /**
34+ * @dataProvider provideInvalidStringTestCases
35+ *
36+ * @param mixed $val
37+ */
38+ public function testToStringThrowsOnUncoercableValue ($ val ): void
39+ {
40+ $ this ->expectException (\InvalidArgumentException::class);
41+ $ this ->expectExceptionMessage ('Cannot coerce this value to string ' );
42+
43+ Coerce::toString ($ val );
44+ }
45+
46+ public function provideInvalidStringTestCases (): \Generator
47+ {
48+ yield [new \stdClass ()];
49+ yield [STDOUT ];
50+ }
51+
52+ public function __toString (): string
53+ {
54+ return 'some object ' ;
55+ }
56+ }
You can’t perform that action at this time.
0 commit comments