Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Tag
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
5 / 5
10
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 has
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getInt
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getFloat
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getString
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace Kartographer\Tag;
4
5use StatusValue;
6
7/**
8 * Generic handler to validate and preprocess the arguments of an XML-style parser tag. This class
9 * doesn't know anything about the arguments and what they mean.
10 *
11 * @license MIT
12 */
13class Tag {
14
15    public string $name;
16    /** @var array<string,string> */
17    private array $args;
18    private StatusValue $status;
19
20    /**
21     * @param string $name Tag name, e.g. "maplink"
22     * @param array<string,string> $args
23     * @param StatusValue $status
24     */
25    public function __construct( string $name, array $args, StatusValue $status ) {
26        $this->name = $name;
27        $this->args = $args;
28        $this->status = $status;
29    }
30
31    /**
32     * @param string $name
33     * @return bool True if an attribute exists, even if valueless
34     */
35    public function has( string $name ): bool {
36        return isset( $this->args[$name] );
37    }
38
39    /**
40     * @param string $name
41     * @return int|null Null when missing or invalid
42     */
43    public function getInt( string $name ): ?int {
44        $value = $this->getString( $name, '/^-?[0-9]+$/' );
45        if ( $value !== null ) {
46            $value = intval( $value );
47        }
48
49        return $value;
50    }
51
52    /**
53     * @param string $name
54     * @return float|null Null when missing or invalid
55     */
56    public function getFloat( string $name ): ?float {
57        $value = $this->getString( $name, '/^-?[0-9]*\.?[0-9]+$/' );
58        if ( $value !== null ) {
59            $value = floatval( $value );
60        }
61
62        return $value;
63    }
64
65    /**
66     * Returns value of a named tag attribute with optional validation
67     *
68     * @param string $name Attribute name
69     * @param string|null $regexp Optional regular expression to validate against
70     * @return string|null Null when missing or invalid
71     */
72    public function getString( string $name, string $regexp = null ): ?string {
73        if ( !isset( $this->args[$name] ) ) {
74            return null;
75        }
76
77        $value = trim( $this->args[$name] );
78        if ( $regexp && !preg_match( $regexp, $value ) ) {
79            $this->status->fatal( 'kartographer-error-bad_attr', $name );
80            return null;
81        }
82
83        return $value;
84    }
85
86}