Completed
Push — master ( 1c7cd5...36e3eb )
by Maxim
04:56
created

cast()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.2
cc 4
eloc 9
nc 4
nop 3
1
<?php
2
3
namespace WebComplete\core\utils\typecast;
4
5
use WebComplete\core\utils\typecast\type\CastInterface;
6
use WebComplete\core\utils\typecast\type\Factory;
7
use WebComplete\core\utils\typecast\type\Scheme;
8
use WebComplete\core\utils\typecast\type\TypeArrayOfType;
9
10
if (!\function_exists('\WebComplete\core\utils\typecast\cast')) {
11
    /**
12
     * @param $value
13
     * @param CastInterface|string|array $config
14
     *
15
     * @param bool $strict
16
     *
17
     * @return mixed|null
18
     */
19
    function cast($value, $config, bool $strict = false)
20
    {
21
        $factory = new Factory();
22
        if (\is_array($config)) {
23
            /** @var TypeArrayOfType $arrayOfType */
24
            $config = ($arrayOfType = $factory->checkArrayOfType($config))
25
                ? $arrayOfType
26
                : new Scheme($config, $factory, $strict);
27
        } elseif (!$config instanceof CastInterface) {
28
            $config = $factory->createType($config);
29
        }
30
31
        $cast = new Cast($config);
32
        return $cast->process($value);
33
    }
34
}
35