ArgumentHelper::convert()   C
last analyzed

Complexity

Conditions 12
Paths 12

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 6.9666
c 0
b 0
f 0
cc 12
nc 12
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 InvalidArgumentException;
16
use WBW\Library\Core\Argument\Exception\DateArgumentException;
17
use WBW\Library\Core\Argument\Exception\TimestampArgumentException;
18
use WBW\Library\Core\Argument\Helper\ArrayHelper;
19
use WBW\Library\Core\Argument\Helper\BooleanHelper;
20
use WBW\Library\Core\Argument\Helper\DateTimeHelper;
21
use WBW\Library\Core\Argument\Helper\DoubleHelper;
22
use WBW\Library\Core\Argument\Helper\FloatHelper;
23
use WBW\Library\Core\Argument\Helper\IntegerHelper;
24
use WBW\Library\Core\Argument\Helper\NumberHelper;
25
use WBW\Library\Core\Argument\Helper\ObjectHelper;
26
use WBW\Library\Core\Argument\Helper\ResourceHelper;
27
use WBW\Library\Core\Argument\Helper\StringHelper;
28
use WBW\Library\Core\Argument\Helper\TimestampHelper;
29
30
/**
31
 * Argument helper.
32
 *
33
 * @author webeweb <https://github.com/webeweb/>
34
 * @package WBW\Library\Core\Argument
35
 */
36
class ArgumentHelper implements ArgumentInterface {
37
38
    /**
39
     * Convert a string value into type $type.
40
     *
41
     * @param string|null $value The string value.
42
     * @param int $type The type.
43
     * @param string|null $dateFormat The date format.
44
     * @return mixed Returns the value.
45
     * @throws InvalidArgumentException Throws an invalid argument exception.
46
     */
47
    public static function convert(?string $value, int $type, string $dateFormat = null) {
48
49
        switch ($type) {
50
51
            case self::ARGUMENT_BOOLEAN:
52
                return BooleanHelper::parseString($value);
53
54
            case self::ARGUMENT_DATE:
55
                if (null === $dateFormat) {
56
                    throw new InvalidArgumentException("The date format is null");
57
                }
58
                $datetime = DateTime::createFromFormat($dateFormat, $value);
59
                if (false === $datetime) {
60
                    throw new DateArgumentException($value);
61
                }
62
                return $datetime;
63
64
            case self::ARGUMENT_DOUBLE:
65
                return DoubleHelper::parseString($value);
66
67
            case self::ARGUMENT_FLOAT:
68
                return FloatHelper::parseString($value);
69
70
            case self::ARGUMENT_INTEGER:
71
                return IntegerHelper::parseString($value);
72
73
            case self::ARGUMENT_STRING:
74
                return $value;
75
76
            case self::ARGUMENT_TIMESTAMP:
77
                if (null === $dateFormat) {
78
                    throw new InvalidArgumentException("The datetime format is null");
79
                }
80
                $datetime = DateTime::createFromFormat($dateFormat, $value);
81
                if (false === $datetime) {
82
                    throw new TimestampArgumentException($value);
83
                }
84
                return $datetime;
85
        }
86
87
        throw new InvalidArgumentException("The type \"{$type}\" is not implemented");
88
    }
89
90
    /**
91
     * Determines if the $value is of type $type.
92
     *
93
     * @param mixed $value The value.
94
     * @param int $type The type.
95
     * @return bool Returns true.
96
     * @throws InvalidArgumentException Throws an invalid argument exception.
97
     */
98
    public static function isTypeOf($value, int $type): bool {
99
100
        switch ($type) {
101
102
            case self::ARGUMENT_ARRAY:
103
                ArrayHelper::isArray($value);
104
                return true;
105
106
            case self::ARGUMENT_BOOLEAN:
107
                BooleanHelper::isBoolean($value);
108
                return true;
109
110
            case self::ARGUMENT_DATE:
111
                DateTimeHelper::isDate($value);
112
                return true;
113
114
            case self::ARGUMENT_DOUBLE:
115
                DoubleHelper::isDouble($value);
116
                return true;
117
118
            case self::ARGUMENT_FLOAT:
119
                FloatHelper::isFloat($value);
120
                return true;
121
122
            case self::ARGUMENT_INTEGER:
123
                IntegerHelper::isInteger($value);
124
                return true;
125
126
            case self::ARGUMENT_NUMBER:
127
                NumberHelper::isNumber($value);
128
                return true;
129
130
            case self::ARGUMENT_OBJECT:
131
                ObjectHelper::isObject($value);
132
                return true;
133
134
            case self::ARGUMENT_RESOURCE:
135
                ResourceHelper::isResource($value);
136
                return true;
137
138
            case self::ARGUMENT_STRING:
139
                StringHelper::isString($value);
140
                return true;
141
142
            case self::ARGUMENT_TIMESTAMP:
143
                TimestampHelper::isTimestamp($value);
144
                return true;
145
        }
146
147
        throw new InvalidArgumentException("The type \"{$type}\" is not implemented");
148
    }
149
}
150