1<?php
2
3declare(strict_types=1);
4
5/*
6 * This file is part of the league/commonmark package.
7 *
8 * (c) Colin O'Dell <colinodell@gmail.com>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14namespace League\CommonMark\Node;
15
16final class StringContainerHelper
17{
18    /**
19     * Extract text literals from all descendant nodes
20     *
21     * @param Node          $node         Parent node
22     * @param array<string> $excludeTypes Optional list of node class types to exclude
23     *
24     * @return string Concatenated literals
25     */
26    public static function getChildText(Node $node, array $excludeTypes = []): string
27    {
28        $text = '';
29
30        foreach ($node->iterator() as $child) {
31            if ($child instanceof StringContainerInterface && ! self::isOneOf($child, $excludeTypes)) {
32                $text .= $child->getLiteral();
33            }
34        }
35
36        return $text;
37    }
38
39    /**
40     * @param string[] $classesOrInterfacesToCheck
41     *
42     * @psalm-pure
43     */
44    private static function isOneOf(object $object, array $classesOrInterfacesToCheck): bool
45    {
46        foreach ($classesOrInterfacesToCheck as $type) {
47            if ($object instanceof $type) {
48                return true;
49            }
50        }
51
52        return false;
53    }
54}
55