Completed
Push — master ( e386b5...c8b222 )
by Andre
01:50
created

ObjectSynopsis::generateValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace TheIconic\Synopsis;
4
5
use ReflectionClass;
6
use ReflectionProperty;
7
8
/**
9
 * represents an object
10
 *
11
 * @package TheIconic\Synopsis
12
 */
13
class ObjectSynopsis extends AbstractSynopsis
14
{
15
    /**
16
     * @var string the namespace
17
     */
18
    protected $namespace;
19
20
    /**
21
     * @see parent::process()
22
     * @param $value
23
     * @param $depth
24
     */
25
    public function process($value, $depth)
26
    {
27
        parent::process($value, $depth);
28
29
        $reflector = new ReflectionClass($value);
30
31
        $properties = $reflector->getProperties(ReflectionProperty::IS_PUBLIC);
32
33
        foreach ($properties as $property) {
34
            $this->addChild($this->getFactory()->synopsize($property->getValue($value), $depth), $property->getName());
35
        }
36
37
        if (empty($this->length)) {
38
            $this->length = count($this->children);
39
        }
40
41
        $this->type = $reflector->name;
42
43
        $this->value = $this->generateValue($value);
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    protected function generateValue($value)
50
    {
51
        foreach (['__toSynopsisValue', '__toString', 'getId', 'getName'] as $method) {
52
            if (method_exists($value, $method) && is_callable([$value, $method])) {
53
                return (string) call_user_func([$value, $method]);
54
            }
55
        }
56
57
        return '';
58
    }
59
60
    /**
61
     * get the namespace
62
     *
63
     * @return string
64
     */
65
    public function getNamespace()
66
    {
67
        return $this->namespace;
68
    }
69
}
70