|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ZfcUser\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\Validator\AbstractValidator; |
|
6
|
|
|
use ZfcUser\Mapper\UserInterface; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractRecord extends AbstractValidator |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Error constants |
|
12
|
|
|
*/ |
|
13
|
|
|
const ERROR_NO_RECORD_FOUND = 'noRecordFound'; |
|
14
|
|
|
const ERROR_RECORD_FOUND = 'recordFound'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array Message templates |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $messageTemplates = array( |
|
20
|
|
|
self::ERROR_NO_RECORD_FOUND => "No record matching the input was found", |
|
21
|
|
|
self::ERROR_RECORD_FOUND => "A record matching the input was found", |
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var UserInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $mapper; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $key; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Required options are: |
|
36
|
|
|
* - key Field to use, 'email' or 'username' |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(array $options) |
|
39
|
|
|
{ |
|
40
|
|
|
if (!array_key_exists('key', $options)) { |
|
41
|
|
|
throw new Exception\InvalidArgumentException('No key provided'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->setKey($options['key']); |
|
45
|
|
|
|
|
46
|
|
|
parent::__construct($options); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* getMapper |
|
51
|
|
|
* |
|
52
|
|
|
* @return UserInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getMapper() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->mapper; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* setMapper |
|
61
|
|
|
* |
|
62
|
|
|
* @param UserInterface $mapper |
|
63
|
|
|
* @return AbstractRecord |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setMapper(UserInterface $mapper) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->mapper = $mapper; |
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get key. |
|
73
|
|
|
* |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getKey() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->key; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Set key. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $key |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setKey($key) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->key = $key; |
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Grab the user from the mapper |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $value |
|
96
|
|
|
* @return mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function query($value) |
|
99
|
|
|
{ |
|
100
|
|
|
$result = false; |
|
101
|
|
|
|
|
102
|
|
|
switch ($this->getKey()) { |
|
103
|
|
|
case 'email': |
|
104
|
|
|
$result = $this->getMapper()->findByEmail($value); |
|
105
|
|
|
break; |
|
106
|
|
|
|
|
107
|
|
|
case 'username': |
|
108
|
|
|
$result = $this->getMapper()->findByUsername($value); |
|
109
|
|
|
break; |
|
110
|
|
|
|
|
111
|
|
|
default: |
|
112
|
|
|
throw new \Exception('Invalid key used in ZfcUser validator'); |
|
113
|
|
|
break; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $result; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|