1 <?php
2 /* --------------------------------------------------------------
3 BoolType.inc.php 2016-01-28
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2016 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12 /**
13 * Class BoolType
14 *
15 * Shared boolean type class. Use the "asBool" method for getting the plain value.
16 *
17 * @category System
18 * @package Shared
19 * @subpackage Types
20 */
21 class BoolType
22 {
23 /**
24 * Instance Value
25 *
26 * @var bool
27 */
28 protected $value;
29
30
31 /**
32 * Class Constructor
33 *
34 * @throws InvalidArgumentException
35 *
36 * @param bool $p_value
37 */
38 public function __construct($p_value)
39 {
40 if(is_bool($p_value))
41 {
42 $this->value = $p_value;
43
44 return;
45 }
46
47 if(is_string($p_value) && strtolower($p_value) === 'false')
48 {
49 $p_value = false;
50 }
51 elseif(is_string($p_value) && strtolower($p_value) === 'true')
52 {
53 $p_value = true;
54 }
55 elseif((!is_bool($p_value) && !is_numeric($p_value))
56 || (is_numeric($p_value) && $p_value != 1 && $p_value != 0)
57 )
58 {
59 throw new InvalidArgumentException('BoolType: Invalid argument value given (expected bool got '
60 . gettype($p_value) . ')');
61 }
62
63 $this->value = (bool)$p_value;
64 }
65
66
67 /**
68 * Get the instance value as bool.
69 *
70 * @return bool
71 */
72 public function asBool()
73 {
74 return $this->value;
75 }
76 }