Brain::setMaster()   A
last analyzed

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 1
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\Rivescript\Rivescript;
14
15
/**
16
 * Brain class
17
 *
18
 * The Brain teaches the interpreter how to respond from
19
 * the user's input.
20
 *
21
 * PHP version 7.4 and higher.
22
 *
23
 * @category Core
24
 * @package  Cortext
25
 * @author   Shea Lewis <[email protected]>
26
 * @license  https://opensource.org/licenses/MIT MIT
27
 * @link     https://github.com/axiom-labs/rivescript-php
28
 * @since    0.3.0
29
 */
30
class Brain
31
{
32
    /**
33
     * A collection of topics.
34
     *
35
     * @var array<Topic>
36
     */
37
    protected array $topics;
38
39
    /**
40
     * @var \Axiom\Rivescript\Rivescript
41
     */
42
    protected Rivescript $master;
43
44
    /**
45
     * Create a new instance of Brain.
46
     */
47
    public function __construct()
48
    {
49
        $this->createTopic('random');
50
    }
51
52
    /**
53
     * Set the interpreter for future reference.
54
     *
55
     * @param \Axiom\Rivescript\Rivescript $master
56
     *
57
     * @return void
58
     */
59
    public function setMaster(Rivescript $master): void
60
    {
61
        $this->master = $master;
62
    }
63
64
    /**
65
     * Teach the Brain with new information.
66
     *
67
     * @param resource $stream the stream to read from.
68
     *
69
     * @return void
70
     */
71
    public function teach($stream): void
72
    {
73
        $commands = synapse()->commands;
74
75
        if (is_resource($stream)) {
76
            $lineNumber = 0;
77
78
            rewind($stream);
79
80
            while (!feof($stream)) {
81
                $line = fgets($stream);
82
                $node = new Node($line, $lineNumber++);
83
84
                if ($node->isInterrupted() === true || $node->isComment() === true || $node->isEmpty()) {
85
                    continue;
86
                }
87
88
                $commands->each(function ($command) use ($node) {
89
                    $class = "\\Axiom\\Rivescript\\Cortex\\Commands\\$command";
90
                    $commandClass = new $class();
91
                    $commandClass->parse($node);
92
                });
93
            }
94
        } else {
95
            echo "CANNOT TEACH INVALID RESOURCE STREAM\n";
96
        }
97
    }
98
99
    /**
100
     * Return a topic.
101
     *
102
     * @param string|null $name The name of the topic to return.
103
     *
104
     * @return mixed|null
105
     */
106
    public function topic(string $name = null)
107
    {
108
        if (is_null($name)) {
109
            $name = synapse()->memory->shortTerm()->get('topic') ?: 'random';
110
        }
111
112
        if (!isset($this->topics[$name])) {
113
            return null;
114
        }
115
116
        return $this->topics[$name];
117
    }
118
119
    /**
120
     * Create a new Topic.
121
     *
122
     * @param string $name The name of the topic to create.
123
     *
124
     * @return Topic
125
     */
126
    public function createTopic(string $name): Topic
127
    {
128
        $this->topics[$name] = new Topic($name);
129
130
        return $this->topics[$name];
131
    }
132
}
133