Wildcard   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 44
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A parse() 0 33 4
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
 * Wildcard class
18
 *
19
 * The Wildcard class determines if a provided trigger
20
 * is a wildcard.
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 Wildcard extends Trigger
32
{
33
34
    /**
35
     * Parse the trigger.
36
     *
37
     * @param string $trigger The trigger to parse.
38
     * @param Input  $input   Input information.
39
     *
40
     * @return bool
41
     */
42
    public function parse(string $trigger, Input $input): bool
43
    {
44
        $trigger = $this->parseTags($trigger, $input);
45
46
        $wildcards = [
47
            '/\*$/' => '.*?',
48
            '/\*/' => '\\w+?',
49
            '/#/' => '\\d+?',
50
            '/_/' => '(.*?[a-zA-Z])',
51
            '/<zerowidthstar>/' => '^\*$',
52
        ];
53
54
        foreach ($wildcards as $pattern => $replacement) {
55
            $parsedTrigger = preg_replace($pattern, '(' . $replacement . ')', $trigger);
56
57
            if ($parsedTrigger === $trigger) {
58
                continue;
59
            }
60
61
            if (@preg_match_all('/' . $parsedTrigger . '$/iu', $input->source(), $wildcards)) {
62
                array_shift($wildcards);
63
64
                $wildcards = Collection::make($wildcards)->flatten()->all();
65
66
                print_r($wildcards);
67
68
                synapse()->memory->shortTerm()->put("wildcards", $wildcards);
69
70
                return true;
71
            }
72
        }
73
74
        return false;
75
    }
76
}
77