Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.74% covered (success)
94.74%
18 / 19
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Tag
94.74% covered (success)
94.74%
18 / 19
80.00% covered (warning)
80.00%
4 / 5
11.02
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
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
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 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    /**
16     * @param string $name Tag name, e.g. "maplink"
17     * @param array<string,string> $args
18     * @param StatusValue $status
19     */
20    public function __construct(
21        public readonly string $name,
22        private readonly array $args,
23        private readonly StatusValue $status,
24    ) {
25    }
26
27    /**
28     * @param string $name
29     * @return bool True if an attribute exists, even if valueless
30     */
31    public function has( string $name ): bool {
32        return isset( $this->args[$name] );
33    }
34
35    /**
36     * @param string $name
37     * @return int|null Null when missing or invalid
38     */
39    public function getInt( string $name ): ?int {
40        $value = $this->getString( $name, '/^-?[0-9]+$/' );
41        if ( $value !== null ) {
42            $value = intval( $value );
43        }
44
45        return $value;
46    }
47
48    /**
49     * @param string $name
50     * @param int $max Defaults to +/-360°
51     * @return float|null Null when missing or invalid
52     */
53    public function getFloat( string $name, int $max = 360 ): ?float {
54        $value = $this->getString( $name, '/^-?[0-9]*\.?[0-9]+$/' );
55        if ( $value !== null ) {
56            $value = floatval( $value );
57            if ( abs( $value ) > $max ) {
58                return null;
59            }
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}