Completed
Push — master ( 17312a...d17d72 )
by Mario
03:12
created

ObjectProperty::extract()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7.5034

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 36
ccs 18
cts 23
cp 0.7826
rs 6.7272
cc 7
eloc 19
nc 7
nop 1
crap 7.5034
1
<?php
2
3
namespace Marek\Toggable\Hydrator;
4
5
use Zend\Stdlib\Exception;
6
7
/**
8
 * Class ObjectProperty
9
 * @package Marek\Toggable\Hydrator
10
 */
11
class ObjectProperty extends \Zend\Hydrator\ObjectProperty
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 10
    public function extract($object)
17
    {
18 10
        if (!is_object($object)) {
19 1
            throw new Exception\BadMethodCallException(
20 1
                sprintf('%s expects the provided $object to be a PHP object)', __METHOD__)
21 1
            );
22
        }
23
24 9
        $data   = get_object_vars($object);
25 9
        $filter = $this->getFilter();
26
27 9
        $vars = array();
28 9
        foreach ($data as $name => $value) {
29
            // Filter keys, removing any we don't want
30 9
            if (! $filter->filter($name)) {
31
                unset($data[$name]);
32
                continue;
33
            }
34
35
            // Replace name if extracted differ
36 9
            $extracted = $this->extractName($name, $object);
37
38 9
            if ($extracted !== $name) {
39
                unset($data[$name]);
40
                $name = $extracted;
41
            }
42
43 9
            $data[$name] = $this->extractValue($name, $value, $object);
44
45 9
            if (!empty($data[$name]) || $data[$name] === false) {
46 9
                $vars[$name] = $data[$name];
47 9
            }
48 9
        }
49
50 9
        return $vars;
51
    }
52
}
53