GlSpellCheckerSentence   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 89
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getText() 0 4 1
A addError() 0 4 1
A getErrors() 0 4 1
B mergeErrors() 0 25 6
A sortedErrorsByOffset() 0 11 3
1
<?php
2
/**
3
 * Spell check a sentence
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  GLICER
8
 * @package   GlSpellChecker
9
 * @author    Emmanuel ROECKER
10
 * @author    Rym BOUCHAGOUR
11
 * @copyright 2015 GLICER
12
 * @license   MIT
13
 * @link      http://dev.glicer.com/
14
 *
15
 * Created : 04/05/15
16
 * File : GlSpellCheckerSentence.php
17
 *
18
 */
19
20
namespace GlSpellChecker;
21
22
/**
23
 * Class GlSpellCheckerError
24
 * @package GlSpellChecker
25
 */
26
class GlSpellCheckerSentence
27
{
28
29
    /**
30
     * @var string
31
     */
32
    private $text;
33
34
    /**
35
     * @var GlSpellCheckerError[] $errors
36
     */
37
    private $errors = [];
38
39
    /**
40
     * @param $text
41
     */
42
    public function __construct($text)
43
    {
44
        $this->text = $text;
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getText()
51
    {
52
        return $this->text;
53
    }
54
55
    /**
56
     * @param GlSpellCheckerError $newerror
57
     */
58
    public function addError(GlSpellCheckerError $newerror)
59
    {
60
        $this->errors[] = $newerror;
61
    }
62
63
    /**
64
     * @return GlSpellCheckerError[]
65
     */
66
    public function getErrors()
67
    {
68
        return $this->errors;
69
    }
70
71
    /**
72
     * @return GlSpellCheckerError[]
73
     */
74
    public function mergeErrors() {
75
        $this->sortedErrorsByOffset();
76
        $errors = $this->errors;
77
        /**
78
         * @var GlSpellCheckerError[] $newerrors
79
         */
80
        $newerrors = [];
81
        foreach ($errors as $error) {
82
83
            $finded = false;
84
            foreach ($newerrors as $newerror) {
85
                $start = $newerror->getOffset();
86
                $end = $start + $newerror->getLength();
87
                if (($error->getOffset() >= $start) && ($error->getOffset() <= $end)) {
88
                    $newerror->merge($error);
89
                    $finded = true;
90
                }
91
            }
92
            if (!$finded) {
93
                $newerrors[] = clone $error;
94
            }
95
        }
96
97
        return $newerrors;
98
    }
99
100
    /**
101
     *
102
     */
103
    private function sortedErrorsByOffset() {
104
        usort(
105
            $this->errors,
106
            function (GlSpellCheckerError $a, GlSpellCheckerError $b) {
107
                if ($a->getOffset() == $b->getOffset()) {
108
                    return 0;
109
                }
110
                return ($a->getOffset() < $b->getOffset()) ? -1 : 1;
111
            }
112
        );
113
    }
114
} 
115