RangeValidityCheck   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 28
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 9 2
1
<?php
2
/**
3
 * Billing Boss
4
 *
5
 * @copyright 2018 Ransford Ako Okpoti
6
 * @license   Refer to the LICENSE distributed with this library
7
 * @link      https://github.com/ranskills/billing-boss-php
8
 */
9
10
namespace BillingBoss\Traits;
11
12
use BillingBoss\BillContext;
13
use BillingBoss\Exception\RangeException;
14
use BillingBoss\Expr;
15
use BillingBoss\RangeHelper;
16
17
/**
18
 * A trait to that implements isValid function for range-related interpreters
19
 *
20
 * @package BillingBoss
21
 * @author  Ransford Ako Okpoti <[email protected]>
22
 * @since   1.0.0
23
 */
24
trait RangeValidityCheck
25
{
26
    protected $ranges = [];
27
28
    /**
29
     * Validates the BillingContext#structure
30
     *
31
     * An interpreter that returns false if it can not match the structure of the
32
     * context.
33
     * True is returned if the structure is syntactically correct and contextually
34
     * it can be interpreted
35
     *
36
     * An exception is throw when structurally the notation/structure provided is
37
     * semantically valid but contextually cannot be interpreted
38
     *
39
     * @param BillContext $context The context to be validated
40
     * @return bool true if the context is validated, false otherwise
41
     * @throws RangeException
42
     */
43
    public function isValid(BillContext $context): bool
44
    {
45
        if (preg_match(sprintf('/%s/', Expr::RANGE), $context->getStructure()) === 0) {
46
            return false;
47
        }
48
49
        $this->ranges = RangeHelper::validate($context->getStructure());
50
51
        return count($this->ranges) > 0;
52
    }
53
}
54