Completed
Push — master ( 90a4ad...3d84b5 )
by Nicolas
04:44
created

StringManipulation::isConvertibleToString()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 8.8571
cc 5
eloc 6
nc 4
nop 1
crap 5
1
<?php
2
3
namespace Puzzle\Pieces;
4
5
trait StringManipulation
6
{
7 8
    private function removeAccents($string)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
8
    {
9 8
        if(! is_string($string))
10 8
        {
11 4
            throw new \InvalidArgumentException('String expected');
12
        }
13
14
        $alphabet = array(
15 4
            'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
16 4
            'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
17 4
            'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
18 4
            'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
19 4
            'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
20 4
            'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
21 4
            'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f',
22 4
        );
23
24 4
        $text = strtr($string, $alphabet);
25
26 4
        return $text;
27
    }
28
    
29 32
    private function isConvertibleToString($value)
30
    {
31 32
        if(is_scalar($value) || is_null($value))
32 32
        {
33 16
            return true;
34
        }
35
        
36 16
        if(! is_object($value))
37 16
        {
38 4
            return false;
39
        }
40
        
41 12
        return $value instanceof ConvertibleToString || method_exists($value, "__toString");
42
    }
43
    
44 16
    private function convertToString($value)
1 ignored issue
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
45
    {
46 16
        if($this->isConvertibleToString($value))
47 16
        {
48 10
            return (string) $value;
49
        }
50
        
51 6
        return gettype($value);
52
    }
53
}
54