|
1
|
|
|
<?php |
|
2
|
|
|
//filename : module/TutorialValidator/src/TutorialValidator/Validator/Special.php |
|
3
|
|
|
namespace PlaygroundCore\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\Validator\AbstractValidator; |
|
6
|
|
|
use Zend\Validator\Exception; |
|
7
|
|
|
|
|
8
|
|
|
class MailDomain extends AbstractValidator |
|
9
|
|
|
{ |
|
10
|
|
|
const FORBIDDEN = 'FORBIDDEN'; |
|
11
|
|
|
|
|
12
|
|
|
protected $options = array( |
|
13
|
|
|
'file' => null, // File containing the authorized domains |
|
14
|
|
|
); |
|
15
|
|
|
|
|
16
|
|
|
protected $messageTemplates = array( |
|
17
|
|
|
self::FORBIDDEN => "This domain is not allowed", |
|
18
|
|
|
); |
|
19
|
|
|
|
|
20
|
|
|
public function __construct($options = null) |
|
21
|
|
|
{ |
|
22
|
|
|
if (is_string($options)) { |
|
23
|
|
|
$this->options = array('file' => str_replace('\\', '/', getcwd()) . '/' . ltrim($options, '/')); |
|
24
|
|
|
} elseif (is_array($options)) { |
|
25
|
|
|
$this->options = array('file' => str_replace('\\', '/', getcwd()) . '/' . ltrim(reset($options), '/')); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
parent::__construct($this->options); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns the file path |
|
33
|
|
|
* |
|
34
|
|
|
* @return int |
|
35
|
|
|
*/ |
|
36
|
|
|
public function getFile() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->options['file']; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Sets the path to the file |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $path to the file |
|
|
|
|
|
|
45
|
|
|
* @return MailDomain Provides a fluent interface |
|
46
|
|
|
* @throws Exception\InvalidArgumentException When file is not found |
|
47
|
|
|
*/ |
|
48
|
|
View Code Duplication |
public function setFile($file) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
if (empty($file) || false === stream_resolve_include_path($file)) { |
|
51
|
|
|
throw new Exception\InvalidArgumentException('Invalid options to validator provided'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$this->options['file'] = $file; |
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function isValid($value) |
|
59
|
|
|
{ |
|
60
|
|
|
// get only the mail domain |
|
61
|
|
|
$domain = explode('@', $value); |
|
62
|
|
|
$domain = end($domain); |
|
63
|
|
|
$this->setValue(strtolower($value)); |
|
64
|
|
|
|
|
65
|
|
|
if (strpos(file_get_contents($this->getFile()), strtolower($domain)) === false) { |
|
66
|
|
|
$this->error(self::FORBIDDEN); |
|
67
|
|
|
|
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.