PurifierValidator::_purifyEmpty()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
namespace App\Model\Validation;
3
4
use Cake\Validation\Validator;
5
use HTMLPurifier;
6
use HTMLPurifier_Config;
7
8
class PurifierValidator extends Validator
9
{
10
11
    /**
12
     * The default configuration for the HTMLPurifier_Config.
13
     *
14
     * @var array
15
     */
16
    protected static $_purifierEmptyConfig = [
17
        'Core.Encoding' => 'UTF-8',
18
        'HTML.Allowed' => ''
19
    ];
20
21
    /**
22
     * Check if the length of a HTML string is smaller or equal to a maximal length.
23
     *
24
     * @param string $check The value to check.
25
     * @param int    $max   The maximal string length.
26
     *
27
     * @return bool
28
     */
29 View Code Duplication
    public static function purifierMaxLength($check, $max)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $check = static::_purifyEmpty($check);
32
33
        if ($check === false) {
34
            return false;
35
        }
36
37
        return mb_strlen($check) <= $max;
38
    }
39
40
    /**
41
     * Check if the length of a HTML string is greater or equal to a minimal length.
42
     *
43
     * @param string $check The value to check.
44
     * @param int    $min   The minimal string length.
45
     *
46
     * @return bool
47
     */
48 View Code Duplication
    public static function purifierMinLength($check, $min)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        $check = static::_purifyEmpty($check);
51
52
        if ($check === false) {
53
            return false;
54
        }
55
56
        return mb_strlen($check) >= $min;
57
    }
58
59
    /**
60
     * Parse a HTML string with HTMLPurifier to remove all/special HTML tags.
61
     *
62
     * @param bool|string $text The string to be parsed.
63
     *
64
     * @return bool|string
65
     */
66
    protected static function _purifyEmpty($text = false)
67
    {
68
        if ($text === false) {
69
            return false;
70
        }
71
72
        $config = HTMLPurifier_Config::createDefault();
73
        $config->loadArray(static::$_purifierEmptyConfig);
74
75
        return (new HTMLPurifier($config))->purify(trim($text));
76
    }
77
}
78