GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c430e2...993294 )
by Nicholas
03:11
created

StringUtility::convertString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace UCD\Unicode\TransformationFormat;
4
5
use UCD\Exception\InvalidArgumentException;
6
use UCD\Unicode\TransformationFormat;
7
8
class StringUtility
9
{
10
    /**
11
     * @var array
12
     */
13
    private static $iconvMap = [
14
        TransformationFormat::EIGHT => 'UTF-8',
15
        TransformationFormat::SIXTEEN => 'UTF-16',
16
        TransformationFormat::SIXTEEN_BIG_ENDIAN => 'UTF-16BE',
17
        TransformationFormat::SIXTEEN_LITTLE_ENDIAN => 'UTF-16LE',
18
        TransformationFormat::THIRTY_TWO => 'UTF-32',
19
        TransformationFormat::THIRTY_TWO_BIG_ENDIAN => 'UTF-32BE',
20
        TransformationFormat::THIRTY_TWO_LITTLE_ENDIAN => 'UTF-32LE'
21
    ];
22
23
    /**
24
     * @param string $string
25
     * @param TransformationFormat $from
26
     * @param TransformationFormat $to
27
     * @return string
28
     */
29
    public static function convertString($string, TransformationFormat $from, TransformationFormat $to)
30
    {
31
        $in = self::mapFormat($from);
32
        $out = self::mapFormat($to);
33
34
        return iconv($in, $out, $string);
35
    }
36
37
    /**
38
     * @param string $character
39
     * @param TransformationFormat $from
40
     * @param TransformationFormat $to
41
     * @throws InvalidArgumentException
42
     * @return string
43
     */
44
    public static function convertCharacter($character, TransformationFormat $from, TransformationFormat $to)
45
    {
46
        $in = self::mapFormat($from);
47
48
        if (iconv_strlen($character, $in) !== 1) {
49
            throw new InvalidArgumentException('Single character must be provided');
50
        }
51
52
        return self::convertString($character, $from, $to);
53
    }
54
55
    /**
56
     * @param TransformationFormat $format
57
     * @return string
58
     * @throws InvalidArgumentException
59
     */
60
    private static function mapFormat(TransformationFormat $format)
61
    {
62
        if (!array_key_exists($format->getType(), self::$iconvMap)) {
63
            throw new InvalidArgumentException();
64
        }
65
66
        return self::$iconvMap[$format->getType()];
67
    }
68
69
    /**
70
     * @param string $string
71
     * @param TransformationFormat $format
72
     * @return string[]
73
     * @throws InvalidArgumentException
74
     */
75
    public static function split($string, TransformationFormat $format)
76
    {
77
        $encoding = self::mapFormat($format);
78
        $characters = [];
79
80
        for ($i = 0; $i < iconv_strlen($string, $encoding); $i++) {
81
            $character = iconv_substr($string, $i, 1, $encoding);
82
            array_push($characters, $character);
83
        }
84
85
        return $characters;
86
    }
87
}