Completed
Push — master ( 366fbf...5f1478 )
by Rafael
02:45
created

Object2Array::createArray()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 15
nc 3
nop 1
1
<?php
2
3
/**
4
 * LICENSE: This file is subject to the terms and conditions defined in
5
 * file 'LICENSE', which is part of this source code package.
6
 *
7
 * @copyright 2016 Copyright(c) - All rights reserved.
8
 */
9
10
namespace Rafrsr\LibArray2Object;
11
12
use Rafrsr\LibArray2Object\Parser\ValueParserInterface;
13
14
class Object2Array
15
{
16
17
    /**
18
     * @var Object2ArrayContext
19
     */
20
    private $context;
21
22
    /**
23
     * @param Object2ArrayContext $context
24
     */
25
    public function __construct(Object2ArrayContext $context)
26
    {
27
        $this->context = $context;
28
    }
29
30
    /**
31
     * @return Object2ArrayContext
32
     */
33
    public function getContext()
34
    {
35
        return $this->context;
36
    }
37
38
    /**
39
     * @param Object2ArrayContext $context
40
     *
41
     * @return $this
42
     */
43
    public function setContext(Object2ArrayContext $context)
44
    {
45
        $this->context = $context;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @param object $object object instance to traverse
52
     *
53
     * @throws \InvalidArgumentException
54
     *
55
     * @return array
56
     */
57
    public function createArray($object)
58
    {
59
        if (!is_object($object)) {
60
            throw new \InvalidArgumentException('The first param should be a object.');
61
        }
62
63
        $array = [];
64
        if ($object instanceof Object2ArrayInterface) {
65
            $array = $object->__toArray($this);
66
        } else {
67
            $reflClass = new \ReflectionClass($object);
68
69
            foreach (Utils::getClassProperties($reflClass) as $property) {
70
                if ($this->context->getReader()->isReadable($object, $property->getName())) {
71
                    $value = $this->context->getReader()->getValue($object, $property->getName());
72
                    $types = $types = Utils::getPropertyTypes($property);
73
                    $value = $this->parseValue($value, $types, $property, $object);
74
                    $array[$property->getName()] = $value;
75
                }
76
            }
77
        }
78
79
        return $array;
80
    }
81
82
    /**
83
     * Parse a value using given types
84
     *
85
     * @param mixed               $value
86
     * @param array               $types
87
     * @param \ReflectionProperty $property
88
     * @param object              $object
89
     *
90
     * @return array|bool|float|int|string
91
     */
92 View Code Duplication
    private function parseValue($value, $types, \ReflectionProperty $property, $object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        foreach ($types as $type) {
95
            foreach ($this->context->getParsers() as $parser) {
96
                if ($parser instanceof ValueParserInterface) {
97
                    if (is_array($value) && strpos($type, '[]') !== false) {
98
                        foreach ($value as $key => &$arrayValue) {
99
                            $arrayValue = $parser->toArrayValue($arrayValue, str_replace('[]', null, $type), $property, $arrayValue);
100
                        }
101
                    } else {
102
                        //print_r($value);
103
                        $value = $parser->toArrayValue($value, $type, $property, $object);
104
                    }
105
                } else {
106
                    throw new \InvalidArgumentException(sprintf("%s is not a valid parser.", get_class($parser)));
107
                }
108
            }
109
        }
110
111
        return $value;
112
    }
113
}