Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(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
 
14
namespace League\CommonMark\Xml;
15
 
16
use League\CommonMark\Node\Block\AbstractBlock;
17
use League\CommonMark\Node\Inline\AbstractInline;
18
use League\CommonMark\Node\Node;
19
 
20
/**
21
 * @internal
22
 */
23
final class FallbackNodeXmlRenderer implements XmlNodeRendererInterface
24
{
25
    /**
26
     * @var array<string, string>
27
     *
28
     * @psalm-allow-private-mutation
29
     */
30
    private array $classCache = [];
31
 
32
    /**
33
     * @psalm-allow-private-mutation
34
     */
35
    public function getXmlTagName(Node $node): string
36
    {
37
        $className = \get_class($node);
38
        if (isset($this->classCache[$className])) {
39
            return $this->classCache[$className];
40
        }
41
 
42
        $type      = $node instanceof AbstractBlock ? 'block' : 'inline';
43
        $shortName = \strtolower((new \ReflectionClass($node))->getShortName());
44
 
45
        return $this->classCache[$className] = \sprintf('custom_%s_%s', $type, $shortName);
46
    }
47
 
48
    /**
49
     * {@inheritDoc}
50
     */
51
    public function getXmlAttributes(Node $node): array
52
    {
53
        $attrs = [];
54
        foreach ($node->data->export() as $k => $v) {
55
            if (self::isValueUsable($v)) {
56
                $attrs[$k] = $v;
57
            }
58
        }
59
 
60
        $reflClass = new \ReflectionClass($node);
61
        foreach ($reflClass->getProperties() as $property) {
62
            if (\in_array($property->getDeclaringClass()->getName(), [Node::class, AbstractBlock::class, AbstractInline::class], true)) {
63
                continue;
64
            }
65
 
66
            $property->setAccessible(true);
67
            $value = $property->getValue($node);
68
            if (self::isValueUsable($value)) {
69
                $attrs[$property->getName()] = $value;
70
            }
71
        }
72
 
73
        return $attrs;
74
    }
75
 
76
    /**
77
     * @param mixed $var
78
     *
79
     * @psalm-pure
80
     */
81
    private static function isValueUsable($var): bool
82
    {
83
        return \is_string($var) || \is_int($var) || \is_float($var) || \is_bool($var);
84
    }
85
}