ObjectSynopsis::generateValue()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 4
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
     * @see parent::process()
17
     * @param $value
18
     * @param $depth
19
     */
20
    public function process($value, $depth)
21
    {
22
        parent::process($value, $depth);
23
24
        $reflector = new ReflectionClass($value);
25
26
        $properties = $reflector->getProperties(ReflectionProperty::IS_PUBLIC);
27
28
        foreach ($properties as $property) {
29
            $this->addChild($this->getFactory()->synopsize($property->getValue($value), $depth), $property->getName());
30
        }
31
32
        if (empty($this->length)) {
33
            $this->length = count($this->children);
34
        }
35
36
        $this->type = $reflector->name;
37
38
        $this->value = $this->generateValue($value);
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    protected function generateValue($value)
45
    {
46
        foreach (['__toSynopsisValue', '__toString', 'getId', 'getName'] as $method) {
47
            if (method_exists($value, $method) && is_callable([$value, $method])) {
48
                return (string) call_user_func([$value, $method]);
49
            }
50
        }
51
52
        return '';
53
    }
54
}
55