Completed
Push — master ( a60f01...d79ec6 )
by Peter
25s
created

Text::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * AnalyzerText package.
4
 *
5
 * @author  Peter Gribanov <[email protected]>
6
 */
7
8
namespace AnalyzerText;
9
10
use AnalyzerText\Text\Word;
11
12
/**
13
 * Анализируемый текст
14
 *
15
 * Класс используется для абстрагирования мотодов доступа к словами в тексте, предоставляя простой интерфейс
16
 *
17
 * @author  Peter Gribanov <[email protected]>
18
 */
19
class Text extends \ArrayIterator
20
{
21
    /**
22
     * Спиок всех слов в тексте в простой форме.
23
     *
24
     * @var array
25
     */
26
    protected $plains = array();
27
28
    /**
29
     * @param string $text
30
     */
31 11
    public function __construct($text)
32
    {
33 11
        $words = array();
34
        // слово не может начинаться с тире и не может содержать только его
35 11
        if (preg_match_all('/[[:alnum:]]+(?:[-\'][[:alnum:]]+)*/u', trim(strip_tags($text)), $match)) {
36 11
            $words = $match[0];
37
            // получение списка слов в нижнем регистре
38 11
            $this->plains = explode(' ', mb_strtolower(implode(' ', $words), 'utf8'));
39
        }
40 11
        parent::__construct($words);
41 11
    }
42
43
    /**
44
     * Возвращает список слов.
45
     *
46
     * @return array
47
     */
48 11
    public function getWords()
49
    {
50 11
        return $this->getArrayCopy();
51
    }
52
53
    /**
54
     * Возвращает текущий элемент
55
     *
56
     * @return Word
57
     */
58 10
    public function current()
59
    {
60 10
        return new Word(parent::current(), $this->plains[$this->key()]);
61
    }
62
63
    /**
64
     * Удаляет слово из текста.
65
     */
66 1
    public function remove()
67
    {
68 1
        $this->offsetUnset($this->key());
69 1
        unset($this->plains[$this->key()]);
70 1
    }
71
72
    /**
73
     * Заменяет слово в тексте.
74
     *
75
     * @param Word $word
76
     */
77 1
    public function replace(Word $word)
78
    {
79 1
        $this->offsetSet($this->key(), $word->getWord());
80 1
        $this->plains[$this->key()] = $word->getPlain();
81 1
    }
82
83
    /**
84
     * Возвращает текст
85
     *
86
     * @return string
87
     */
88 11
    public function __toString()
89
    {
90 11
        return implode(' ', $this->getWords());
91
    }
92
}
93