Completed
Push — work-fleets ( addb18...c7cb6b )
by SuperNova.WS
06:04
created

Types::castAs()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 23
nc 8
nop 2
dl 0
loc 34
rs 5.3846
c 1
b 0
f 0
ccs 0
cts 25
cp 0
crap 72
1
<?php
2
3
namespace Common;
4
5
class Types {
6
7
  /**
8
   * @param string $type TYPE_XXX constant
9
   * @param mixed  $value Value to cast
10
   *
11
   * @return bool|float|int|null|string
12
   */
13
  public static function castAs($type, $value) {
14
    // TODO: Here should be some conversions to property type
15
    switch ($type) {
16
      case TYPE_INTEGER:
17
        $value = intval($value);
18
      break;
19
20
      case TYPE_DOUBLE:
21
        $value = floatval($value);
22
      break;
23
24
      case TYPE_BOOLEAN:
25
        $value = boolval($value);
26
      break;
27
28
      case TYPE_NULL:
29
        $value = null;
30
      break;
31
32
      case TYPE_ARRAY:
33
        $value = (array)$value;
34
      break;
35
36
      case TYPE_STRING:
37
        // Empty type is string
38
      case TYPE_EMPTY:
39
        // No-type defaults to string
40
      default:
41
        $value = (string)$value;
42
      break;
43
    }
44
45
    return $value;
46
  }
47
48
}