Util::getStrongScalarType()   B
last analyzed

Complexity

Conditions 9
Paths 7

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 20
rs 7.756
cc 9
eloc 16
nc 7
nop 1
1
<?php
2
/**
3
 * Copyright (c) 2014-2016 Ryan Parman.
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a copy
6
 * of this software and associated documentation files (the "Software"), to deal
7
 * in the Software without restriction, including without limitation the rights
8
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the Software is
10
 * furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included in
13
 * all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * http://opensource.org/licenses/MIT
24
 */
25
26
namespace Skyzyx\StrongTypes;
27
28
class Util
29
{
30
    /**
31
     * Gets the most useful description of the value's type.
32
     *
33
     * @param  mixed  $param The value to check.
34
     * @return string The description of the type of the value.
35
     */
36
    public static function getClassOrType($param)
37
    {
38
        $type = gettype($param);
39
40
        if ($type === 'object') {
41
            return get_class($param);
42
        }
43
44
        return $type;
45
    }
46
47
    /**
48
     * Gets the strongly-typed version of a scalar type.
49
     *
50
     * @param  mixed                $value The scalar value to convert to a strong type.
51
     * @return SingleValueInterface A Boolean, Integer, Float or String type.
52
     */
53
    public static function getStrongScalarType($value)
54
    {
55
        if ($value === 'true' || $value === 'false') {
56
            return new BooleanType($value === 'true');
57
        } elseif (is_bool($value)) {
58
            return new BooleanType($value);
59
        } elseif (is_string($value) && is_numeric($value)) {
60
            if (strpos($value, '.') !== false) {
61
                return new FloatType((float) $value);
62
            } else {
63
                return new IntegerType((integer) $value);
64
            }
65
        } elseif (is_int($value)) {
66
            return new IntegerType($value);
67
        } elseif (is_float($value)) {
68
            return new FloatType($value);
69
        } else {
70
            return new StringType((string) $value);
71
        }
72
    }
73
74
    /**
75
     * Convenience method for validating and returning a native value in one step.
76
     *
77
     * @param  ShapeInterface $shape A ShapeInterface object.
78
     * @return mixed                 The native value, validated as its expected type.
79
     */
80
    public static function getValidatedValue(ShapeInterface $shape)
81
    {
82
        $shape->validate();
83
84
        return $shape->getValue();
85
    }
86
}
87