Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
XDataAbstract
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 getLocalXCode
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getLocalXCodes
n/a
0 / 0
n/a
0 / 0
0
 getXMessageTemplate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getXMessageTemplates
n/a
0 / 0
n/a
0 / 0
0
 getXMessageVariables
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * @package pvcErr
5 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
6 */
7
8declare(strict_types=1);
9
10namespace pvc\err;
11
12use pvc\interfaces\err\XDataInterface;
13
14/**
15 * Class ExceptionDataAbstract
16 */
17abstract class XDataAbstract implements XDataInterface
18{
19    /**
20     * @function getLocalXCode
21     * @param class-string $classString
22     * @return int
23     */
24    public function getLocalXCode(string $classString): int
25    {
26        $codes = $this->getLocalXCodes();
27        return $codes[$classString] ?? 0;
28    }
29
30    /**
31     * @function getLocalXCodes
32     * @return array<class-string, int>
33     */
34    abstract public function getLocalXCodes(): array;
35
36    /**
37     * @function getXMessageTemplate
38     * @param class-string $classString
39     * @return string
40     */
41    public function getXMessageTemplate(string $classString): string
42    {
43        $messages = $this->getXMessageTemplates();
44        return $messages[$classString] ?? '';
45    }
46
47    /**
48     * @function getXMessageTemplates
49     * @return array<class-string, string>
50     */
51    abstract public function getXMessageTemplates(): array;
52
53
54    public function getXMessageVariables(string $messageTemplate): array
55    {
56        /**
57         * starts with '${', character class includes any combo of characters except '}' (at least one), finishes
58         * with a '}'.
59         *
60         * Thought about restricting the characters in the character class to those that can be in a legitimate
61         * variable name, but PHP itself would kick those out with a
62         * compile error when you created the exception with bad dummy variable names, so we can afford to slack here
63         * a bit.....
64         *
65         * $matches[1] is an array of all the strings in the subject that match the first capturing subpattern.  In
66         * this case, that is the variable name without the delimiters.  I.e. if it looks like ${variable} in the
67         * message, then it appears as 'variable' in the array.
68         */
69        $regex = '/\$\{[^}]+}/';
70        preg_match_all($regex, $messageTemplate, $matches);
71        return $matches[0];
72    }
73}