Passed
Push — master ( a3e4b0...6ea7df )
by Sebastian
05:06
created

ConvertHelper_EOL::isCRLF()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * File containing the {@see AppUtils\ConvertHelper_EOL} class.
4
 * 
5
 * @package Application Utils
6
 * @subpackage ConvertHelper
7
 * @see AppUtils\ConvertHelper_EOL
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppUtils;
13
14
/**
15
 * Container class for an end of line (EOL) character.
16
 * Used as result when detecting EOL characters in a
17
 * string or file.
18
 *
19
 * @package Application Utils
20
 * @subpackage ConvertHelper
21
 * @author Sebastian Mordziol <[email protected]>
22
 * 
23
 * @see ConvertHelper::detectEOLCharacter()
24
 */
25
class ConvertHelper_EOL
26
{
27
    const TYPE_CRLF = 'CR+LF';
28
    const TYPE_LFCR = 'LF+CR';
29
    const TYPE_LF = 'LF';
30
    const TYPE_CR = 'CR';
31
    
32
   /**
33
    * @var string
34
    */
35
    protected $char;
36
    
37
   /**
38
    * @var string
39
    */
40
    protected $type;
41
    
42
   /**
43
    * @var string
44
    */
45
    protected $description;
46
47
    protected static $eolChars = null;
48
49
    public function __construct(string $char, string $type, string $description)
50
    {
51
        $this->char = $char;
52
        $this->type = $type;
53
        $this->description = $description;
54
    }
55
    
56
   /**
57
    * The actual EOL character.
58
    * @return string
59
    */
60
    public function getCharacter() : string
61
    {
62
        return $this->char;
63
    }
64
    
65
   /**
66
    * A more detailed, human readable description of the character.
67
    * @return string
68
    */
69
    public function getDescription() : string
70
    {
71
        return $this->description;
72
    }
73
    
74
   /**
75
    * The EOL character type, e.g. "CR+LF", "CR"...
76
    * @return string
77
    * 
78
    * @see ConvertHelper_EOL::TYPE_CR
79
    * @see ConvertHelper_EOL::TYPE_CRLF
80
    * @see ConvertHelper_EOL::TYPE_LF
81
    * @see ConvertHelper_EOL::TYPE_LFCR
82
    */
83
    public function getType() : string
84
    {
85
        return $this->type;
86
    }
87
88
    public function isCRLF() : bool
89
    {
90
        return $this->isType(self::TYPE_CRLF);
91
    }
92
    
93
    public function isCR() : bool
94
    {
95
        return $this->isType(self::TYPE_CR);
96
    }
97
    
98
    public function isLF() : bool
99
    {
100
        return $this->isType(self::TYPE_LF);
101
    }
102
    
103
    public function isLFCR() : bool
104
    {
105
        return $this->isType(self::TYPE_LFCR);
106
    }
107
    
108
    public function isType(string $type) : bool
109
    {
110
        return $this->type === $type;
111
    }
112
113
    /**
114
     * Detects the most used end-of-line character in the subject string.
115
     *
116
     * @param string $subjectString The string to check.
117
     * @return NULL|ConvertHelper_EOL The detected EOL instance, or NULL if none has been detected.
118
     */
119
    public static function detect(string $subjectString) : ?ConvertHelper_EOL
120
    {
121
        if(empty($subjectString)) {
122
            return null;
123
        }
124
125
        if(!isset(self::$eolChars))
126
        {
127
            $cr = chr((int)hexdec('0d'));
128
            $lf = chr((int)hexdec('0a'));
129
130
            self::$eolChars = array(
131
                array(
132
                    'char' => $cr.$lf,
133
                    'type' => ConvertHelper_EOL::TYPE_CRLF,
134
                    'description' => t('Carriage return followed by a line feed'),
135
                ),
136
                array(
137
                    'char' => $lf.$cr,
138
                    'type' => ConvertHelper_EOL::TYPE_LFCR,
139
                    'description' => t('Line feed followed by a carriage return'),
140
                ),
141
                array(
142
                    'char' => $lf,
143
                    'type' => ConvertHelper_EOL::TYPE_LF,
144
                    'description' => t('Line feed'),
145
                ),
146
                array(
147
                    'char' => $cr,
148
                    'type' => ConvertHelper_EOL::TYPE_CR,
149
                    'description' => t('Carriage Return'),
150
                ),
151
            );
152
        }
153
154
        $max = 0;
155
        $results = array();
156
        foreach(self::$eolChars as $def)
157
        {
158
            $amount = substr_count($subjectString, $def['char']);
159
160
            if($amount > $max)
161
            {
162
                $max = $amount;
163
                $results[] = $def;
164
            }
165
        }
166
167
        if(empty($results)) {
168
            return null;
169
        }
170
171
        return new ConvertHelper_EOL(
172
            $results[0]['char'],
173
            $results[0]['type'],
174
            $results[0]['description']
175
        );
176
    }
177
}
178