Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
XCodePrefixes
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
3 / 3
5
100.00% covered (success)
100.00%
1 / 1
 getXCodePrefix
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getXCodePrefixes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addXCodePrefix
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6
7declare(strict_types=1);
8
9namespace pvc\err;
10
11use pvc\err\err\InvalidXCodePrefixNumberException;
12use pvc\interfaces\err\XCodePrefixesInterface;
13
14/**
15 * Class XCodePrefixes
16 */
17class XCodePrefixes implements XCodePrefixesInterface
18{
19    /**
20     * @var array<string, int>
21     * array of namespace => exception code prefix pairs, each prefix must be unique
22     */
23    protected static array $prefixes = [
24        'pvcExamples\\err\\err' => 900,
25        'pvcTests\\err\\fixtureForXDataTests' => 901,
26        'pvc\\err\\err' => 902,
27        'pvc\\err\\pvc' => 903,
28        'pvc\\config\\err' => 904,
29        'pvc\\msg\\err' => 905,
30        'pvc\\intl\\err' => 906,
31        'pvc\\filtervar\\err' => 907,
32        'pvc\\regex\\err' => 908,
33        'pvc\\validator\\err' => 909,
34        'pvc\\http\\err' => 910,
35        'pvc\\parser\\err' => 911,
36        'pvc\\struct\\collection\\err' => 912,
37        'pvc\\struct\\dto\\err' => 913,
38        'pvc\\struct\\filetree\\err' => 914,
39        'pvc\\struct\\tree\\err' => 915,
40        'pvc\\struct\\treesearch\\err' => 916,
41        'pvc\\frmtr\\err' => 917,
42        'pvc\\storage\\filesys\\err' => 918,
43        'pvc\\storage\\resource\\err' => 919,
44        'pvc\\html\\err' => 920,
45        'pvc\\err\\pvc\\array' => 921,
46        'pvc\\err\\pvc\\php' => 922,
47        'pvc\\cloud\\google\\err' => 923,
48    ];
49
50    /**
51     * make this public so external applications can rely on it and to facilitate testing
52     */
53    public const MIN_APPLICATION_PREFIX = 1000;
54
55    /**
56     * @function getXCodePrefix
57     * @param string $namespace
58     * @return int
59     */
60    public static function getXCodePrefix(string $namespace): int
61    {
62        $prefixes = self::getXCodePrefixes();
63        return $prefixes[$namespace] ?? 0;
64    }
65
66    /**
67     * @function getXCodePrefixes
68     * @return array<string, int>
69     */
70    public static function getXCodePrefixes(): array
71    {
72        return self::$prefixes;
73    }
74
75    /**
76     * addXCodePrefix
77     * @param string $nameSpace
78     * @param int $prefix
79     * @throws InvalidXCodePrefixNumberException
80     */
81    public static function addXCodePrefix(string $nameSpace, int $prefix): void
82    {
83        /**
84         * there is no need to check if $nameSpace is a valid namespace name.  The code inside the pvc exception
85         * class supplies the namespace name it needs to getXCodePrefix.  The worst that can happen is it does not find
86         * it in the prefixes array and getXCodePrefix returns 0.
87         */
88        if (in_array($prefix, self::$prefixes) || ($prefix < self::MIN_APPLICATION_PREFIX)) {
89            throw new InvalidXCodePrefixNumberException($prefix);
90        }
91        self::$prefixes[$nameSpace] = $prefix;
92    }
93}