Passed
Push — master ( 4812ae...635254 )
by Johnny
06:32
created

Topic::isOk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of Rivescript-php
4
 *
5
 * (c) Shea Lewis <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Axiom\Rivescript\Cortex;
12
13
use Axiom\Collections\Collection;
14
15
/**
16
 * Topic class
17
 *
18
 * Stores information about a topic.
19
 *
20
 * PHP version 7.4 and higher.
21
 *
22
 * @category Core
23
 * @package  Cortext
24
 * @author   Shea Lewis <[email protected]>
25
 * @license  https://opensource.org/licenses/MIT MIT
26
 * @link     https://github.com/axiom-labs/rivescript-php
27
 * @since    0.3.0
28
 */
29
class Topic
30
{
31
    /**
32
     * The name of this Topic.
33
     *
34
     * @var string
35
     */
36
    protected string $name;
37
38
    /**
39
     * The triggers for this Topic.
40
     *
41
     * @var Collection<string, mixed>
42
     */
43
    public Collection $triggers;
44
45
    /**
46
     * The responses for this Topic.
47
     *
48
     * @var Collection<string, mixed>
49
     */
50
    public Collection $responses;
51
52
    /**
53
     * This indicates if the > begin topic
54
     * is ok to finish.
55
     *
56
     * @var bool
57
     */
58
    public bool $ok = false;
59
60
    /**
61
     * Create a new Topic instance.
62
     *
63
     * @param string $name
64
     */
65
    public function __construct(string $name)
66
    {
67
        $this->name = $name;
68
        $this->triggers = new Collection([]);
69
        $this->responses = new Collection([]);
70
    }
71
72
    /**
73
     * Return triggers associated with this branch.
74
     *
75
     * @return Collection<string, mixed>
76
     */
77
    public function triggers(): Collection
78
    {
79
        return $this->triggers;
80
    }
81
82
    /**
83
     * Return the responses associated with this branch.
84
     *
85
     * @return Collection<string, mixed>
86
     */
87
    public function responses(): Collection
88
    {
89
        return $this->responses;
90
    }
91
92
    /**
93
     * Sort triggers based on type and word count from
94
     * largest to smallest.
95
     *
96
     * @param \Axiom\Collections\Collection $triggers
97
     *
98
     * @return \Axiom\Collections\Collection
99
     */
100
    public function sortTriggers(Collection $triggers): Collection
101
    {
102
        $triggers = $this->determineWordCount($triggers);
103
        $triggers = $this->determineTypeCount($triggers);
104
105
        return $triggers->sort(function ($current, $previous) {
106
            return ($current['order'] < $previous['order']) ? -1 : 1;
107
        })->reverse();
108
    }
109
110
    /**
111
     * Determine the order in the triggers.
112
     *
113
     * @param Collection<array> $triggers A collection of triggers.
114
     *
115
     * @return Collection<array>
116
     */
117
    protected function determineTypeCount(Collection $triggers): Collection
118
    {
119
        return $triggers->each(function ($data, $trigger) use ($triggers) {
120
            if (isset($data['type'])) {
121
                switch ($data['type']) {
122
                    case 'atomic':
123
                        $data['order'] += 4000000;
124
                        break;
125
                    case 'alphabetic':
126
                        $data['order'] += 3000000;
127
                        break;
128
                    case 'numeric':
129
                        $data['order'] += 2000000;
130
                        break;
131
                    case 'global':
132
                        $data['order'] += 1000000;
133
                        break;
134
                }
135
136
                $triggers->put($trigger, $data);
137
            }
138
        });
139
    }
140
141
    /**
142
     * Sort triggers based on word count from
143
     * largest to smallest.
144
     *
145
     * @param Collection<array> $triggers A collection of triggers.
146
     *
147
     * @return Collection<array>
148
     */
149
    protected function determineWordCount(Collection $triggers): Collection
150
    {
151
        return $triggers->each(function ($data, $trigger) use ($triggers) {
152
            $data['order'] = count(explode(' ', $trigger));
153
154
            $triggers->put($trigger, $data);
155
        });
156
    }
157
158
    public function isBegin(): bool
159
    {
160
        return ($this->name === "__begin__");
161
    }
162
163
    /**
164
     * Set the ok value.
165
     *
166
     * @param bool $value Ok value true or false.
167
     *
168
     * @return void
169
     */
170
    public function setOk(bool $value = false): void
171
    {
172
        $this->ok = $value;
173
    }
174
175
    /**
176
     * Indicate if this topic is ok to end
177
     * or not.
178
     *
179
     * Note: This is only used for the __begin__
180
     * topic.
181
     *
182
     * @return bool
183
     */
184
    public function isOk(): bool
185
    {
186
        return $this->ok;
187
    }
188
}
189