Validator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Validator.php
5
 *
6
 * Validate requests data before they are passed into the library.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\Request;
16
17
/*
18
 * See the following links to get explanations about the regexp.
19
 * http://php.net/manual/en/language.oop5.basic.php
20
 * http://stackoverflow.com/questions/3195614/validate-class-method-names-with-regex
21
 * http://www.w3schools.com/charsets/ref_html_utf8.asp
22
 * http://www.w3schools.com/charsets/ref_utf_latin1_supplement.asp
23
 */
24
25
use Jaxon\App\Config\ConfigManager;
26
use Jaxon\App\I18n\Translator;
27
28
use function preg_match;
29
30
class Validator
31
{
32
    /**
33
     * The config manager
34
     *
35
     * @var ConfigManager
36
     */
37
    protected $xConfigManager;
38
39
    /**
40
     * The translator
41
     *
42
     * @var Translator
43
     */
44
    protected $xTranslator;
45
46
    public function __construct(ConfigManager $xConfigManager, Translator $xTranslator)
47
    {
48
        // Set the config manager
49
        $this->xConfigManager = $xConfigManager;
50
        // Set the translator
51
        $this->xTranslator = $xTranslator;
52
    }
53
54
    /**
55
     * Validate a function name
56
     *
57
     * @param string $sName    The function name
58
     *
59
     * @return bool
60
     */
61
    public function validateFunction(string $sName): bool
62
    {
63
        return (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $sName) > 0);
64
    }
65
66
    /**
67
     * Validate a class name
68
     *
69
     * @param string $sName    The class name
70
     *
71
     * @return bool
72
     */
73
    public function validateClass(string $sName): bool
74
    {
75
        return (preg_match('/^([a-zA-Z][a-zA-Z0-9_]*)(\.[a-zA-Z][a-zA-Z0-9_]*)*$/', $sName) > 0);
76
    }
77
78
    /**
79
     * Validate a method name
80
     *
81
     * @param string $sName    The function name
82
     *
83
     * @return bool
84
     */
85
    public function validateMethod(string $sName): bool
86
    {
87
        return (preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $sName) > 0);
88
    }
89
}
90