Passed
Push — master ( 439d83...dfc367 )
by Jonathan
09:32
created

Base64Data   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 40
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A isValid() 0 14 2
1
<?php
2
3
/**
4
 * ReportingCloud PHP Wrapper
5
 *
6
 * PHP wrapper for ReportingCloud Web API. Authored and supported by Text Control GmbH.
7
 *
8
 * @link      http://www.reporting.cloud to learn more about ReportingCloud
9
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
10
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
11
 * @copyright © 2018 Text Control GmbH
12
 */
13
14
namespace TxTextControl\ReportingCloud\Validator;
15
16
/**
17
 * Base64Data validator
18
 *
19
 * @package TxTextControl\ReportingCloud
20
 * @author  Jonathan Maron (@JonathanMaron)
21
 */
22
class Base64Data extends AbstractValidator
23
{
24
    /**
25
     * Invalid type
26
     *
27
     * @const INVALID_ENCODING
28
     */
29
    const INVALID_ENCODING = 'invalidEncoding';
30
31
    /**
32
     * Message templates
33
     *
34
     * @var array
35
     */
36
    protected $messageTemplates
37
        = [
38
            self::INVALID_ENCODING => "'%value%' must be base64 encoded",
39
        ];
40
41
    /**
42
     * Returns true, if value is valid. False otherwise.
43
     *
44
     * @param mixed $value
45
     *
46
     * @return bool
47
     */
48 26
    public function isValid($value)
49
    {
50 26
        $this->setValue($value);
51
52
        // If the strict parameter is set to TRUE, base64_decode() returns false,
53
        // if the input contains characters from outside the base64 alphabet.
54
55 26
        if (false === base64_decode($this->getValue(), true)) {
56 2
            $this->error(self::INVALID_ENCODING);
57
58 2
            return false;
59
        }
60
61 24
        return true;
62
    }
63
}
64