StringFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 28
ccs 8
cts 8
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B create() 0 13 5
1
<?php namespace BuildR\Utils\Factories;
2
3
use BuildR\Foundation\Exception\InvalidArgumentException;
4
use BuildR\Foundation\Object\StringConvertibleInterface;
5
use BuildR\Utils\StringObject;
6
7
/**
8
 * Basic factory class for flexible stringObject creation
9
 *
10
 * BuildR PHP Framework
11
 *
12
 * @author Zoltán Borsos <[email protected]>
13
 * @package Utils
14
 * @subpackage Factories
15
 *
16
 * @copyright    Copyright 2016, Zoltán Borsos.
17
 * @license      https://github.com/BuildrPHP/Utils/blob/master/LICENSE.md
18
 * @link         https://github.com/BuildrPHP/Utils
19
 */
20
class StringFactory {
21
22
    /**
23
     * Creates a new stringObject class from the given value.
24
     * If the given value is an object and is string convertible the object
25
     * will be converted to string, before consumed.
26
     *
27
     * @param string $string
28
     *
29
     * @return \BuildR\Utils\StringObject
30
     *
31
     * @throws \BuildR\Foundation\Exception\InvalidArgumentException
32
     */
33 91
    public static function create($string) {
34 91
        if(is_array($string)) {
35 1
            throw new InvalidArgumentException('Arrays cannot be casted to string!');
36
        }
37
38 90
        if(is_object($string) &&
39 90
            !(($string instanceof StringConvertibleInterface) || (method_exists($string, '__toString')))) {
40 1
            throw new InvalidArgumentException('The given value is an object but cant converted to string!');
41
        }
42
43 89
        $value = (string) $string;
44 89
        return new StringObject($value);
45
    }
46
47
}
48