44
55namespace Flow \ETL \Function ;
66
7- use function Flow \Types \DSL \type_html ;
87use DOM \{Element , HTMLDocument };
98use Flow \ETL \Row ;
109
1110final class HTMLQuerySelectorAll extends ScalarFunctionChain
1211{
1312 public function __construct (
1413 private readonly mixed $ value ,
15- private readonly ScalarFunction |string $ path ,
14+ private readonly ScalarFunction |string $ selector ,
1615 ) {
1716 }
1817
19- public function eval (Row $ row ) : mixed
18+ /**
19+ * @return null|array<Element>
20+ */
21+ public function eval (Row $ row ) : ?array
2022 {
21- $ value = (new Parameter ($ this ->value ))->as ($ row , type_html () );
22- $ path = (new Parameter ($ this ->path ))->asString ($ row );
23+ $ value = (new Parameter ($ this ->value ))->asInstanceOf ($ row , HTMLDocument::class );
24+ $ selector = (new Parameter ($ this ->selector ))->asString ($ row );
2325
24- if (null === $ value || null === $ path ) {
26+ if (null === $ value || null === $ selector ) {
2527 return null ;
2628 }
2729
28- if (! \class_exists ( ' \Dom\HTMLDocument ' ) ) {
30+ if (\ PHP_VERSION_ID < 80400 ) {
2931 throw new \RuntimeException ('This function requires \Dom\HTMLDocument extension available in PHP 8.4+. ' );
3032 }
3133
32- /* @phpstan-ignore-next-line */
33- $ result = HTMLDocument::createFromString ($ value ->toString ())->querySelectorAll ($ path );
34+ $ result = $ value ->querySelectorAll ($ selector );
3435
3536 if (0 === $ result ->count ()) {
3637 return null ;
@@ -39,7 +40,6 @@ public function eval(Row $row) : mixed
3940 $ nodes = [];
4041
4142 foreach ($ result as $ node ) {
42- /* @phpstan-ignore-next-line */
4343 if (!$ node instanceof Element) {
4444 continue ;
4545 }
0 commit comments