Completed
Push — master ( 8f5bc0...179bc9 )
by WEBEWEB
03:06
created

ArgumentHelper   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 15

Importance

Changes 0
Metric Value
wmc 24
lcom 0
cbo 15
dl 0
loc 96
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C convert() 0 34 12
C isTypeOf() 0 40 12
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Argument;
13
14
use DateTime;
15
use WBW\Library\Core\Exception\Argument\DateArgumentException;
16
use WBW\Library\Core\Exception\Argument\IllegalArgumentException;
17
use WBW\Library\Core\Exception\Argument\TimestampArgumentException;
18
use WBW\Library\Core\Exception\Pointer\NullPointerException;
19
20
/**
21
 * Argument helper.
22
 *
23
 * @author webeweb <https://github.com/webeweb/>
24
 * @package WBW\Library\Core\Argument
25
 */
26
class ArgumentHelper implements ArgumentInterface {
27
28
    /**
29
     * Convert a string value into type $type.
30
     *
31
     * @param string $value The string value.
32
     * @param integer $type The type.
33
     * @param string $dateFormat The date format.
34
     * @return mixed Returns the value.
35
     * @throws IllegalArgumentException Throws an illegal argument exception if the value is not of expected type.
36
     * @throws NullPointerException Throws a null pointer exception if the type is date and the date format is null.
37
     */
38
    public static function convert($value, $type, $dateFormat = null) {
39
        switch ($type) {
40
            case self::ARGUMENT_BOOLEAN:
41
                return BooleanHelper::parseString($value);
42
            case self::ARGUMENT_DATE:
43
                if (null === $dateFormat) {
44
                    throw new NullPointerException("The date format is null");
45
                }
46
                $datetime = DateTime::createFromFormat($dateFormat, $value);
47
                if (false === $datetime) {
48
                    throw new DateArgumentException($value);
49
                }
50
                return $datetime;
51
            case self::ARGUMENT_DOUBLE:
52
                return DoubleHelper::parseString($value);
53
            case self::ARGUMENT_FLOAT:
54
                return FloatHelper::parseString($value);
55
            case self::ARGUMENT_INTEGER:
56
                return IntegerHelper::parseString($value);
57
            case self::ARGUMENT_STRING:
58
                return $value;
59
            case self::ARGUMENT_TIMESTAMP:
60
                if (null === $dateFormat) {
61
                    throw new NullPointerException("The datetime format is null");
62
                }
63
                $datetime = DateTime::createFromFormat($dateFormat, $value);
64
                if (false === $datetime) {
65
                    throw new TimestampArgumentException($value);
66
                }
67
                return $datetime;
68
            default:
69
                throw new IllegalArgumentException("The type \"" . $type . "\" is not implemented");
70
        }
71
    }
72
73
    /**
74
     * Determines if the $value is of type $type.
75
     *
76
     * @param mixed $value The value.
77
     * @param integer $type The type.
78
     * @throws IllegalArgumentException Throws an illegal argument exception if the value is not of expected type.
79
     */
80
    public static function isTypeOf($value, $type) {
81
        switch ($type) {
82
            case self::ARGUMENT_ARRAY:
83
                ArrayHelper::isArray($value);
84
                break;
85
            case self::ARGUMENT_BOOLEAN:
86
                BooleanHelper::isBoolean($value);
87
                break;
88
            case self::ARGUMENT_DATE:
89
                DateTimeHelper::isDate($value);
90
                break;
91
            case self::ARGUMENT_DOUBLE:
92
                DoubleHelper::isDouble($value);
93
                break;
94
            case self::ARGUMENT_FLOAT:
95
                FloatHelper::isFloat($value);
96
                break;
97
            case self::ARGUMENT_INTEGER:
98
                IntegerHelper::isInteger($value);
99
                break;
100
            case self::ARGUMENT_NUMBER:
101
                NumberHelper::isNumber($value);
102
                break;
103
            case self::ARGUMENT_OBJECT:
104
                ObjectHelper::isObject($value);
105
                break;
106
            case self::ARGUMENT_RESOURCE:
107
                ResourceHelper::isResource($value);
108
                break;
109
            case self::ARGUMENT_STRING:
110
                StringHelper::isString($value);
111
                break;
112
            case self::ARGUMENT_TIMESTAMP:
113
                TimestampHelper::isTimestamp($value);
114
                break;
115
            default:
116
                throw new IllegalArgumentException("The type \"" . $type . "\" is not implemented");
117
        }
118
        return true;
119
    }
120
121
}
122