|
1
|
|
|
<?php |
|
2
|
|
|
namespace Solvire\API\Serializers\DataFields; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use Solvire\Utilities\GenderNormalizer as G; |
|
6
|
|
|
/** |
|
7
|
|
|
* |
|
8
|
|
|
* @author solvire <[email protected]> |
|
9
|
|
|
* @package DataFields |
|
10
|
|
|
* @namespace Solvire\API\Serializers\DataFields |
|
11
|
|
|
*/ |
|
12
|
|
|
class GenderField extends DataField |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $cast = 'string'; |
|
20
|
|
|
|
|
21
|
|
|
protected $maleValue = 'male'; |
|
22
|
|
|
|
|
23
|
|
|
protected $femaleValue = 'female'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $options |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function __construct($options=[]) |
|
30
|
|
|
{ |
|
31
|
|
|
// load up the format at creation time |
|
32
|
1 |
|
if(isset($options['maleValue'])) |
|
33
|
1 |
|
$this->maleValue = $options['maleValue']; |
|
34
|
|
|
|
|
35
|
1 |
|
if(isset($options['femaleValue'])) |
|
36
|
1 |
|
$this->femaleValue = $options['femaleValue']; |
|
37
|
|
|
|
|
38
|
1 |
|
parent::__construct($options); |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* (non-PHPdoc) |
|
43
|
|
|
* |
|
44
|
|
|
* @see \Solvire\API\Serializers\DataFields\DataField::setData() |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function setData($data) |
|
47
|
|
|
{ |
|
48
|
1 |
|
if (! is_string($data)) |
|
49
|
1 |
|
throw new \RuntimeException('GenderField data must be a valid representation of a normalized gender : ' . $data . ' in ' . $this->name ); |
|
50
|
|
|
|
|
51
|
1 |
|
$gender = G::spot($data); |
|
52
|
|
|
|
|
53
|
1 |
|
if($gender === 'male') |
|
54
|
1 |
|
$this->data = $this->maleValue; |
|
55
|
1 |
|
elseif ($gender === 'female') |
|
56
|
1 |
|
$this->data = $this->femaleValue; |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
1 |
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* This is a char so it will always be just a string |
|
64
|
|
|
* |
|
65
|
|
|
* (non-PHPdoc) |
|
66
|
|
|
* |
|
67
|
|
|
* @see \Solvire\API\Serializers\DataFields\DataField::getData() |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getData() |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->data; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function setMaleValue($male) |
|
76
|
|
|
{ |
|
77
|
1 |
|
$this->maleValue = $male; |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public function setFemaleValue($female) |
|
81
|
|
|
{ |
|
82
|
1 |
|
$this->femaleValue = $female; |
|
83
|
1 |
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|