Completed
Push — master ( f10304...059713 )
by Thierry
02:01
created

Validator::validateClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 9.4285
1
<?php
2
3
/**
4
 * Validator.php - Jaxon input data validator
5
 *
6
 * Validate requests data before the are passed into the library.
7
 *
8
 * @package jaxon-core
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2016 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\Utils\Validation;
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
class Validator
25
{
26
    /**
27
     * The translator
28
     *
29
     * @var \Jaxon\Utils\Translation\Translator
30
     */
31
    protected $xTranslator;
32
33
    /**
34
     * The config manager
35
     *
36
     * @var \Jaxon\Utils\Config\Config
37
     */
38
    protected $xConfig;
39
40
    /**
41
     * The last error message
42
     *
43
     * @var string
44
     */
45
    protected $sErrorMessage;
46
47
    public function __construct($xTranslator, $xConfig)
48
    {
49
        // Set the translator
50
        $this->xTranslator = $xTranslator;
51
        // Set the config manager
52
        $this->xConfig = $xConfig;
53
    }
54
55
    /**
56
     * Get the last error message
57
     *
58
     * @return string          The last error message
59
     */
60
    public function getErrorMessage()
61
    {
62
        return $this->sErrorMessage;
63
    }
64
65
    /**
66
     * Validate a function name
67
     *
68
     * @param string        $sName            The function name
69
     *
70
     * @return bool            True if the function name is valid, and false if not
71
     */
72
    public function validateFunction($sName)
73
    {
74
        $this->sErrorMessage = '';
75
        return preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $sName);
76
    }
77
78
    /**
79
     * Validate an event name
80
     *
81
     * @param string        $sName            The event name
82
     *
83
     * @return bool            True if the event name is valid, and false if not
84
     */
85
    public function validateEvent($sName)
86
    {
87
        $this->sErrorMessage = '';
88
        return preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $sName);
89
    }
90
91
    /**
92
     * Validate a class name
93
     *
94
     * @param string        $sName            The class name
95
     *
96
     * @return bool            True if the class name is valid, and false if not
97
     */
98
    public function validateClass($sName)
99
    {
100
        $this->sErrorMessage = '';
101
        return preg_match('/^([a-zA-Z][a-zA-Z0-9_]*)(\.[a-zA-Z][a-zA-Z0-9_]*)*$/', $sName);
102
    }
103
104
    /**
105
     * Validate a method name
106
     *
107
     * @param string        $sName            The function name
108
     *
109
     * @return bool            True if the method name is valid, and false if not
110
     */
111
    public function validateMethod($sName)
112
    {
113
        $this->sErrorMessage = '';
114
        // return preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $sName);
115
        return preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $sName);
116
    }
117
118
    /**
119
     * Validate an uploaded file
120
     *
121
     * @param string        $sName            The uploaded file variable name
122
     * @param array         $aUploadedFile    The file data received in the $_FILES array
123
     *
124
     * @return bool            True if the file data are valid, and false if not
125
     */
126
    public function validateUploadedFile($sName, array $aUploadedFile)
127
    {
128
        $this->sErrorMessage = '';
129
        // Verify the file extension
130
        $xDefault = $this->xConfig->getOption('upload.default.types');
131
        $aAllowed = $this->xConfig->getOption('upload.files.' . $sName . '.types', $xDefault);
132 View Code Duplication
        if(is_array($aAllowed) && !in_array($aUploadedFile['type'], $aAllowed))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
133
        {
134
            $this->sErrorMessage = $this->xTranslator->trans('errors.upload.type', $aUploadedFile);
135
            return false;
136
        }
137
        // Verify the file extension
138
        $xDefault = $this->xConfig->getOption('upload.default.extensions');
139
        $aAllowed = $this->xConfig->getOption('upload.files.' . $sName . '.extensions', $xDefault);
140 View Code Duplication
        if(is_array($aAllowed) && !in_array($aUploadedFile['extension'], $aAllowed))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
141
        {
142
            $this->sErrorMessage = $this->xTranslator->trans('errors.upload.extension', $aUploadedFile);
143
            return false;
144
        }
145
        // Verify the max size
146
        $xDefault = $this->xConfig->getOption('upload.default.max-size', 0);
147
        $iSize = $this->xConfig->getOption('upload.files.' . $sName . '.max-size', $xDefault);
148 View Code Duplication
        if($iSize > 0 && $aUploadedFile['size'] > $iSize)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
149
        {
150
            $this->sErrorMessage = $this->xTranslator->trans('errors.upload.max-size', $aUploadedFile);
151
            return false;
152
        }
153
        // Verify the min size
154
        $xDefault = $this->xConfig->getOption('upload.default.min-size', 0);
155
        $iSize = $this->xConfig->getOption('upload.files.' . $sName . '.min-size', $xDefault);
156 View Code Duplication
        if($iSize > 0 && $aUploadedFile['size'] < $iSize)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
157
        {
158
            $this->sErrorMessage = $this->xTranslator->trans('errors.upload.min-size', $aUploadedFile);
159
            return false;
160
        }
161
        return true;
162
    }
163
}
164