Trie::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Autocomplete\Container\Trie;
3
4
use Autocomplete\Contract\Container\ContainerInterface;
5
use Autocomplete\Container\Trie\Node;
6
7
/**
8
 * @implements ContainerInterface
9
 */
10
class Trie implements ContainerInterface
11
{
12
    /**
13
     * @var Node
14
     */
15
    private $root;
16
    /**
17
     * @var boolean
18
     */
19
    private $caseSensitive = true;
20
21
    public function __construct($caseSensitive = true)
22
    {
23
        $this->root = new Node('');
24
        $this->caseSensitive = $caseSensitive;
25
    }
26
27
    private function inputParse(&$input)
28
    {
29
        if ( ! $this->caseSensitive) {
30
            $input = strtolower($input);
31
        }
32
    }
33
34
    public function addWord($word)
35
    {
36
        $this->inputParse($word);
37
        $this->root->addSuffix(str_split($word));
38
    }
39
40
    public function hasPrefix($prefix)
41
    {
42
        $this->inputParse($prefix);
43
        $prefix = str_split($prefix);
44
        return $this->root->hasPrefix($prefix);
45
    }
46
47
    public function getByPrefix($prefix)
48
    {
49
        $this->inputParse($prefix);
50
        $child = $this->root->getClosest($prefix);
51
        if ( ! $child) {
52
            return [];
53
        }
54
        $postFixes = $child->getPostfix($prefix);
55
        return $postFixes;
56
    }
57
58
    public function hasWord($word)
59
    {
60
        $this->inputParse($word);
61
        $word = str_split($word);
62
        return $this->root->hasWord($word);
63
    }
64
65
    public function graph()
66
    {
67
        $graphArr = [];
68
        $this->root->graph($graphArr);
69
        return $graphArr;
70
    }
71
}
72