Arrays   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 17
dl 0
loc 41
rs 10
c 1
b 0
f 1
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 31 6
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\Triggers;
12
13
use Axiom\Collections\Collection;
14
use Axiom\Rivescript\Cortex\Input;
15
16
/**
17
 * Arrays class
18
 *
19
 * The Atomic class determines if a provided trigger
20
 * is an array.
21
 *
22
 * PHP version 7.4 and higher.
23
 *
24
 * @category Core
25
 * @package  Cortext\Triggers
26
 * @author   Shea Lewis <[email protected]>
27
 * @license  https://opensource.org/licenses/MIT MIT
28
 * @link     https://github.com/axiom-labs/rivescript-php
29
 * @since    0.3.0
30
 */
31
class Arrays extends Trigger
32
{
33
    /**
34
     * Parse the trigger.
35
     *
36
     * @param string $trigger The trigger to parse.
37
     * @param Input  $input   Input information.
38
     *
39
     * @return bool
40
     */
41
    public function parse(string $trigger, Input $input): bool
42
    {
43
        $trigger = $this->parseTags($trigger, $input);
44
45
        if (preg_match_all('/@(\w+)/', $trigger, $array_names)) {
46
            $array_names = $array_names[1];
47
48
            foreach ($array_names as $array_name) {
49
                if ($array = synapse()->memory->arrays()->get($array_name)) {
50
                    array_walk($array, 'preg_quote');
51
                    $array_str = implode('|', $array);
52
53
                    $trigger = str_replace("(@$array_name)", "($array_str)", $trigger);
54
                    $trigger = str_replace("@$array_name", "(?:$array_str)", $trigger);
55
                }
56
            }
57
58
            if (@preg_match_all('/' . $trigger . '$/ui', $input->source(), $wildcards)) {
59
                array_shift($wildcards);
60
61
                if ($wildcards) {
62
                    $wildcards = Collection::make($wildcards)->flatten()->all();
63
64
                    synapse()->memory->shortTerm()->put('wildcards', $wildcards);
65
                }
66
67
                return true;
68
            }
69
        }
70
71
        return false;
72
    }
73
}
74