Weighted   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 18 4
A getType() 0 3 1
1
<?php
2
/*
3
 * This file is part of Rivescript-php
4
 *
5
 * (c) Johnny Mast <[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\Responses;
12
13
use Axiom\Rivescript\Contracts\Response as ResponseContract;
14
15
/**
16
 * Weighted class
17
 *
18
 * The Weighted class adds a weight to a weighted response.
19
 *
20
 * PHP version 7.4 and higher.
21
 *
22
 * @category Core
23
 * @package  Cortext\Responses
24
 * @author   Johnny Mast <[email protected]>
25
 * @license  https://opensource.org/licenses/MIT MIT
26
 * @link     https://github.com/axiom-labs/rivescript-php
27
 * @since    0.4.0
28
 */
29
class Weighted extends Response implements ResponseContract
30
{
31
32
    /**
33
     * Parse the Weighted response.
34
     *
35
     * @return bool|string
36
     */
37
    public function parse()
38
    {
39
        if ($this->responseQueueItem()->getCommand() === '-') {
40
            $source = $this->source();
41
            $pattern = '/{weight=([0-9]+)}/';
42
43
            if ($this->matchesPattern($pattern, $source)) {
44
                $matches = $this->getMatchesFromPattern($pattern, $source)[0];
45
46
                if (isset($matches[0])) {
47
                    $this->responseQueueItem->order += (int)$matches[1];
48
                    $source = str_replace($matches[0], '', $source);
49
                }
50
            }
51
            return $source;
52
        }
53
54
        return false;
55
    }
56
57
    /**
58
     * Indicate the type of response this
59
     * class handles.
60
     *
61
     * @return string
62
     */
63
    public function getType(): string
64
    {
65
        return 'weighted';
66
    }
67
}
68