Passed
Push — master ( 1515ff...7c0b30 )
by Sebastian
04:21
created

Mailcode_Traits_Commands_Validation_SearchTerm   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 35
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSearchTerm() 0 11 2
A validateSyntax_search_term() 0 13 2
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Traits_Commands_Validation_SearchTerm} trait.
4
 *
5
 * @package Mailcode
6
 * @subpackage Validation
7
 * @see Mailcode_Traits_Commands_Validation_SearchTerm
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Command validation drop-in: checks for the presence
16
 * of a string literal supposed to contain a search term.
17
 * Will accept the first string literal it finds.
18
 *
19
 * @package Mailcode
20
 * @subpackage Validation
21
 * @author Sebastian Mordziol <[email protected]>
22
 *
23
 * @property Mailcode_Parser_Statement_Validator $validator
24
 */
25
trait Mailcode_Traits_Commands_Validation_SearchTerm
26
{
27
    /**
28
     * @var Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL
29
     */
30
    protected $searchTerm;
31
    
32
    protected function validateSyntax_search_term() : void
33
    {
34
        $val = $this->validator->createStringLiteral();
35
        
36
        if($val->isValid())
37
        {
38
            $this->searchTerm = $val->getToken();
39
        }
40
        else
41
        {
42
            $this->validationResult->makeError(
43
                t('No search term specified.'),
44
                Mailcode_Commands_CommonConstants::VALIDATION_SEARCH_TERM_MISSING
45
            );
46
        }
47
    }
48
    
49
    public function getSearchTerm() : string
50
    {
51
        if($this->searchTerm instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral)
52
        {
53
            return $this->searchTerm->getNormalized();
54
        }
55
        
56
        throw new Mailcode_Exception(
57
            'No string literal available',
58
            null,
59
            Mailcode_Commands_CommonConstants::ERROR_NO_STRING_LITERAL_AVAILABLE
60
        );
61
    }
62
}
63