CurlOptionNormalizer::numerify()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
ccs 10
cts 10
cp 1
crap 4
rs 10
1
<?php
2
3
namespace mpyw\Cowitter\Helpers;
4
5
class CurlOptionNormalizer
6
{
7
    protected static $strToInt = [];
8
    protected static $intToStr = [];
9
10 2
    public static function stringify($key)
11 2
    {
12 2
        static::initialize();
13 2
        if (is_int($key)) {
14 2
            if (!isset(static::$intToStr[$key])) {
15 2
                throw new \DomainException('Invalid cURL option number: ' . $key);
16
            }
17 2
            return static::$intToStr[$key];
18
        };
19 1
        if (!isset(static::$strToInt[$key])) {
20 1
            throw new \DomainException('Invalid cURL option name: ' . $key);
21
        }
22 1
        return $key;
23
    }
24
25 15
    public static function numerify($key)
26 15
    {
27 15
        static::initialize();
28 15
        if (is_string($key)) {
29 2
            if (!isset(static::$strToInt[$key])) {
30 2
                throw new \DomainException('Invalid cURL option name: ' . $key);
31
            }
32 2
            return static::$strToInt[$key];
33
        };
34 14
        if (!isset(static::$intToStr[$key])) {
35 1
            throw new \DomainException('Invalid cURL option number: ' . $key);
36
        }
37 14
        return $key;
38
    }
39
40 1
    public static function stringifyAll(array $options)
41 1
    {
42 1
        $r = [];
43 1
        foreach ($options as $key => $value) {
44 1
            $r[static::stringify($key)] = $value;
45
        }
46 1
        return $r;
47
    }
48
49 69
    public static function numerifyAll(array $options)
50 69
    {
51 69
        $r = [];
52 69
        foreach ($options as $key => $value) {
53 14
            $r[static::numerify($key)] = $value;
54
        }
55 69
        return $r;
56
    }
57
58 17
    protected static function initialize()
59 17
    {
60 17
        if (empty(static::$strToInt)) {
61 1
            static::$strToInt = get_defined_constants(true)['curl'];
62 1
            static::$intToStr = array_flip(static::$strToInt);
63
        }
64 17
    }
65
}
66