Completed
Push — master ( 9b2e6a...8bb7e3 )
by Tim
8s
created

ServerUtil::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
/**
4
 * \AppserverIo\Server\Utils\ServerUtil
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/server
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Server\Utils;
22
23
/**
24
 * Utility implementation that provides some useful functionality.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/appserver-io/server
30
 * @link      http://www.appserver.io
31
 */
32
class ServerUtil
33
{
34
35
    /**
36
     * Contains possible string to boolean mappings.
37
     *
38
     * @var array
39
     */
40
    protected $booleanValues = array();
41
42
    /**
43
     * The singleton instance.
44
     *
45
     * @var \AppserverIo\Server\Utils\ServerUtil
46
     */
47
    protected static $singleton;
48
49
    /**
50
     * Initializes the utility instance.
51
     */
52
    protected function __construct()
53
    {
54
55
        // initialize the string to boolean mappings
56
        $this->booleanValues = array(
57
            true => true,
58
            false => false,
59
            1 => true,
60
            0 => false,
61
            "1" => true,
62
            "0" => false,
63
            "true" => true,
64
            "false" => false,
65
            "on" => true,
66
            "off" => false,
67
            "yes" => true,
68
            "no" => false,
69
            "y" => true,
70
            "n" => false
71
        );
72
    }
73
74
    /**
75
     * Create's and return's the singleton instance.
76
     *
77
     * @return \AppserverIo\Server\Utils\ServerUtil The singleton instance
78
     */
79
    public static function singleton()
80
    {
81
82
        // query whether or not the instance already exists
83
        if (ServerUtil::$singleton == null) {
84
            ServerUtil::$singleton = new ServerUtil();
85
        }
86
87
        // return the singleton instance
88
        return ServerUtil::$singleton;
89
    }
90
91
    /**
92
     * Cast's the passed scalar value to the passed type.
93
     *
94
     * @param string $type  The type the value has been casted to
95
     * @param string $value The value to cast
96
     *
97
     * @return mixed The casted value
98
     * @throws \Exception Is thrown if the passed value can't be casted
99
     */
100
    public function castToType($type, $value)
101
    {
102
103
        // query whether or not, the passed value is a scalar type
104
        if (is_scalar($value) === false) {
105
            throw new \Exception('Passed value is not of scalar type');
106
        }
107
108
        // query the parameters type
109
        switch ($type) {
110
            case 'bool':
111
            case 'boolean':
112
                if (isset($this->booleanValues[$value])) {
113
                    $value = $this->booleanValues[$value];
114
                } else {
115
                    throw new \Exception(sprintf('Can\'t cast %s to boolean', $value));
116
                }
117
                break;
118
119
            default:
120
                // all other can go the same way
121
                if (settype($value, $type) === false) {
122
                    throw new \Exception(sprintf('Can\'t cast %s to %s', $value, type));
123
                }
124
        }
125
126
        // return the casted value
127
        return $value;
128
    }
129
}
130