Completed
Push — master ( ee5489...aa45e5 )
by Luka
03:39
created

EntityCaster   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
B __invoke() 0 24 3
1
<?php
2
3
namespace Baguette\Mastodon;
4
5
use Baguette\Mastodon\Entity\Entity;
6
use Symfony\Component\VarDumper\Caster\Caster;
7
use Symfony\Component\VarDumper\Caster\ClassStub;
8
use Symfony\Component\VarDumper\Cloner\Stub;
9
10
/**
11
 * Object caster for Symfony\VarDumper
12
 *
13
 * This is a class for debugging, and depends on Symfony\VarDumper.
14
 *
15
 * @author    USAMI Kenta <[email protected]>
16
 * @copyright 2017 Baguette HQ
17
 * @license   https://www.gnu.org/licenses/gpl-3.0.html GPL-3.0
18
 * @see https://github.com/BaguettePHP/mastodon-api/wiki/ja-Debugging
19
 */
20
final class EntityCaster
21
{
22
    /** @var false */
23
    private $display_types = false;
24
25
    public function __construct(array $options = [])
26
    {
27
        if (isset($options['display_types'])) {
28
            $this->display_types = $options['display_types'];
29
        }
30
    }
31
32
    /**
33
     * @param Entity $c
34
     * @param array $_ ignore input array
35
     * @param Stub  $stub
36
     * @param bool  $isNested
37
     */
38
    public function __invoke(Entity $c, array $_, Stub $stub, $isNested)
39
    {
40
        $a = [];
41
        $ref = new \ReflectionClass($c);
42
43
        if ($this->display_types) {
44
            $ref_types = $ref->getProperty('property_types');
45
            $ref_types->setAccessible(true);
46
            $types = $ref_types->getValue();
47
            $ref_types->setAccessible(false);
48
            $a[Caster::PREFIX_PROTECTED.'property_types'] = $types;
49
        }
50
51
        $ref_properties = $ref->getProperty('properties');
52
        $ref_properties->setAccessible(true);
53
        $properties = $ref_properties->getValue($c);
54
        $ref_properties->setAccessible(false);
55
56
        foreach ($properties as $key => $prop) {
57
            $a[Caster::PREFIX_DYNAMIC.$key] = $prop;
58
        }
59
60
        return $a;
61
    }
62
}
63