1: <?php
2:
3: /**
4: * PhpOptions
5: * @link https://github.com/masicek/PhpOptions
6: * @author Viktor Mašíček <viktor@masicek.net>
7: * @license "New" BSD License
8: */
9:
10: namespace PhpOptions\Types;
11:
12: require_once __DIR__ . '/AType.php';
13:
14: /**
15: * Enum type
16: *
17: * List of possible values can be set as array
18: * or sring, that will by delimited by "," or " " or ";" or "|".
19: * Filtered value is key of coresponded value in settings.
20: * If values are set by string, values are not filtered by default.
21: *
22: * @author Viktor Mašíček <viktor@masicek.net>
23: */
24: class EnumType extends AType
25: {
26:
27: /**
28: * List of possible values
29: *
30: * @var array
31: */
32: private $values = array();
33:
34:
35: /**
36: * Set object
37: * First setting is list of possible values.
38: * If it is not array, then list is make from substring delimited by "," or " " or ";" or "|"
39: *
40: * @param array $setting Array of setting of object
41: */
42: public function __construct($settings = array())
43: {
44: parent::__construct($settings);
45:
46: $values = isset($settings[0]) ? $settings[0] : array();
47: if (!is_array($values))
48: {
49: $values = preg_replace('/[,; |]+/', ',', $values);
50: $values = explode(',', $values);
51: $this->useFilter = FALSE;
52: }
53: $this->values = $values;
54: }
55:
56:
57: /**
58: * Check type of value.
59: *
60: * @param mixed $value Checked value
61: *
62: * @return bool
63: */
64: public function check($value)
65: {
66: return in_array($value, $this->values);
67: }
68:
69:
70: /**
71: * Return list of possible values
72: *
73: * @return string
74: */
75: public function getName()
76: {
77: return '(' . implode('|', $this->values) . ')';
78: }
79:
80:
81: /**
82: * Return modified value
83: *
84: * @param mixed $value Filtered value
85: *
86: * @return mixed
87: */
88: protected function useFilter($value)
89: {
90: return array_search($value, $this->values);
91: }
92:
93:
94: }
95: