Passed
Push — master ( d44e36...c2d6d5 )
by Johnny
02:21
created

NotEquals::matches()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
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\Condition;
12
13
use Axiom\Rivescript\Contracts\Condition as ConditionContract;
14
15
/**
16
 * NotEquals class
17
 *
18
 * This class handles the != or not equals condition.
19
 *
20
 * Aliases:
21
 *  - ne
22
 *  - <>
23
 *
24
 * PHP version 7.4 and higher.
25
 *
26
 * @category Core
27
 * @package  Cortext\Condition
28
 * @author   Johnny Mast <[email protected]>
29
 * @license  https://opensource.org/licenses/MIT MIT
30
 * @link     https://github.com/axiom-labs/rivescript-php
31
 * @since    0.4.0
32
 */
33
class NotEquals extends Condition implements ConditionContract
34
{
35
36
    /**
37
     * Handle conditions '!=' or its aliases 'ne' and '<>'.
38
     *
39
     * @param string $source The source to parse.
40
     *
41
     * @return bool
42
     */
43
    public function matches(string $source): bool
44
    {
45
        $pattern = "/^([\S]+) (!=|ne|<>) ([\S]+) =>/";
46
47
        if ($this->matchesPattern($pattern, $source) === true) {
48
            $matches = $this->getMatchesFromPattern($pattern, $source);
49
50
            if (isset($matches[0]) === true && count($matches[0]) >= 2) {
51
                return ($matches[0][1] !== $matches[0][3]);
52
            }
53
        }
54
55
        return false;
56
    }
57
}
58