ClassToString::canonical()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Domain\Tools;
4
5
/**
6
 * @author Sebastiaan Hilbers <[email protected]>
7
 */
8
final class ClassToString
9
{
10
    /**
11
     * Fully qualified class name of an object, without a leading backslash
12
     * @param $object
13
     * @return string
14
     * @throws \InvalidArgumentException
15
     */
16
    public static function fqcn($object)
17
    {
18
        if (!is_object($object)) {
19
            throw new \InvalidArgumentException("Expected an object");
20
        }
21
        return trim(get_class($object), '\\');
22
    }
23
24
    /**
25
     * Canonical class name of an object, of the form "My.Namespace.MyClass"
26
     * @param $object
27
     * @return string
28
     */
29
    public static function canonical($object)
30
    {
31
        return str_replace('\\', '.', self::fqcn($object));
32
    }
33
34
    /**
35
     * Underscored and lowercased class name of an object, of the form "my.mamespace.my_class"
36
     * @param $object
37
     * @return string
38
     */
39
    public static function underscore($object)
40
    {
41
        return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', self::canonical($object)));
42
    }
43
44
    /**
45
     * The class name of an object, without the namespace
46
     * @param $object
47
     * @return string
48
     */
49
    public static function short($object)
50
    {
51
        $parts = explode('\\', self::fqcn($object));
52
        return end($parts);
53
    }
54
}
55