add methods to decrypt return data from router
[GitHub/Stricted/speedport-hybrid-php-api.git] / CryptLib / Core / Enum.php
CommitLineData
14d4f286
S
1<?php
2/**
3 * The Enum base class for Enum functionality
4 *
5 * PHP version 5.3
6 *
7 * @category PHPCryptLib
8 * @package Core
9 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
10 * @copyright 2011 The Authors
11 * @license http://www.opensource.org/licenses/mit-license.html MIT License
12 * @version Build @@version@@
13 */
14namespace CryptLib\Core;
15
16use \ReflectionObject;
17
18/**
19 * The Enum base class for Enum functionality
20 *
21 * This is based off of the SplEnum class implementation (which is only available
22 * as a PECL extension in 5.3)
23 *
24 * @see http://www.php.net/manual/en/class.splenum.php
25 * @category PHPCryptLib
26 * @package Core
27 * @author Anthony Ferrara <ircmaxell@ircmaxell.com>
28 */
29abstract class Enum {
30
31 /**
32 * A default value of null is provided. Override this to set your own default
33 */
34 const __DEFAULT = null;
35
36 /**
37 * @var string The name of the constant this instance is using
38 */
39 protected $name = '';
40
41 /**
42 * @var scalar The value of the constant this instance is using.
43 */
44 protected $value = '';
45
46 /**
47 * Creates a new value of the Enum type
48 *
49 * @param mixed $value The value this instance represents
50 * @param boolean $strict Not Implemented at this time
51 *
52 * @return void
53 * @throws UnexpectedValueException If the value is not a constant
54 */
55 public function __construct($value = null, $strict = false) {
56 if (is_null($value)) {
57 $value = static::__DEFAULT;
58 }
59 $validValues = $this->getConstList();
60 $this->name = array_search($value, $validValues);
61 if (!$this->name) {
62 throw new \UnexpectedValueException(
63 'Value not a const in enum ' . get_class($this)
64 );
65 }
66 $this->value = $value;
67 }
68
69 /**
70 * Cast the current object to a string and return its value
71 *
72 * @return mixed the current value of the instance
73 */
74 public function __toString() {
75 return (string) $this->value;
76 }
77
78 /**
79 * Compare two enums using numeric comparison
80 *
81 * @param Enum $arg The enum to compare this instance to
82 *
83 * @return int 0 if same, 1 if the argument is greater, -1 else
84 */
85 public function compare(Enum $arg) {
86 if ($this->value == $arg->value) {
87 return 0;
88 } elseif ($this->value > $arg->value) {
89 return -1;
90 } else {
91 return 1;
92 }
93 }
94
95 /**
96 * Returns all constants (including values) as an associative array
97 *
98 * @param boolean $include_default Include the __default magic value?
99 *
100 * @return array All of the constants found against this instance
101 */
102 public function getConstList($include_default = false) {
103 static $constCache = array();
104 $class = get_class($this);
105 if (!isset($constCache[$class])) {
106 $reflector = new ReflectionObject($this);
107 $constCache[$class] = $reflector->getConstants();
108 }
109 if (!$include_default) {
110 $constants = $constCache[$class];
111 unset($constants['__DEFAULT']);
112 return $constants;
113 }
114 return $constCache[$class];
115 }
116
117}