Completed
Push — master ( 4b4c57...035a27 )
by Rafael
03:03
created

Object2ArrayBuilder::useCallbackNamingStrategy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
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\Naming\CallableNamingStrategy;
13
use Rafrsr\LibArray2Object\Naming\CamelCaseNamingStrategy;
14
use Rafrsr\LibArray2Object\Naming\IdenticalNamingStrategy;
15
use Rafrsr\LibArray2Object\Naming\UnderscoreNamingStrategy;
16
use Rafrsr\LibArray2Object\Reader\AccessorReader;
17
use Rafrsr\LibArray2Object\Reader\PropertyReaderInterface;
18
use Rafrsr\LibArray2Object\Reader\ReflectionReader;
19
use Rafrsr\LibArray2Object\Traits\NamingStrategyAwareTrait;
20
21
class Object2ArrayBuilder extends AbstractBuilder
22
{
23
    use NamingStrategyAwareTrait;
24
25
    /**
26
     * @var PropertyReaderInterface
27
     */
28
    private $reader;
29
30
    /**
31
     * @return PropertyReaderInterface
32
     */
33
    public function getReader()
34
    {
35
        return $this->reader;
36
    }
37
38
    /**
39
     * @param PropertyReaderInterface $reader
40
     *
41
     * @return $this
42
     */
43
    public function setReader($reader)
44
    {
45
        $this->reader = $reader;
46
47
        return $this;
48
    }
49
50
    /**
51
     * Name array keys as "PropertyName"
52
     *
53
     * @return $this
54
     */
55
    public function useUcFirstCamelCaseNamingStrategy()
56
    {
57
        $this->setNamingStrategy(new CamelCaseNamingStrategy(true));
58
59
        return $this;
60
    }
61
62
    /**
63
     * Name array keys as "propertyName"
64
     *
65
     * @return $this
66
     */
67
    public function useCamelCaseNamingStrategy()
68
    {
69
        $this->setNamingStrategy(new CamelCaseNamingStrategy());
70
71
        return $this;
72
    }
73
74
    /**
75
     * Name array keys as "property_name"
76
     *
77
     * @return $this
78
     */
79
    public function useUnderScoreNamingStrategy()
80
    {
81
        $this->setNamingStrategy(new UnderscoreNamingStrategy());
82
83
        return $this;
84
    }
85
86
    /**
87
     * The array keys is identical to property name
88
     *
89
     * @return $this
90
     */
91
    public function useIdenticalNamingStrategy()
92
    {
93
        $this->setNamingStrategy(new IdenticalNamingStrategy());
94
95
        return $this;
96
    }
97
98
    /**
99
     * Use given callback to transform the array key
100
     *
101
     * @param callable $callback
102
     *
103
     * @return $this
104
     */
105
    public function useCallbackNamingStrategy(callable  $callback)
106
    {
107
        $this->setNamingStrategy(new CallableNamingStrategy($callback));
108
109
        return $this;
110
    }
111
112
    /**
113
     * Read the property directly without use getters
114
     *
115
     * @param boolean $onlyPublicProperties only public properties should be exported
116
     *
117
     * @return $this
118
     */
119
    public function disableGetters($onlyPublicProperties = false)
120
    {
121
        $this->setReader(new ReflectionReader($onlyPublicProperties));
122
123
        return $this;
124
    }
125
126
    /**
127
     * Build custom Array2Object instance
128
     */
129
    public function build()
130
    {
131
        if ($this->getContext()) {
132
            $context = $this->getContext();
133
        } else {
134
            $context = new Object2ArrayContext();
135
        }
136
137
        $this->prepareContext($context);
138
139
        return new Object2Array($context);
0 ignored issues
show
Compatibility introduced by
$context of type object<Rafrsr\LibArray2Object\AbstractContext> is not a sub-type of object<Rafrsr\LibArray2O...ct\Object2ArrayContext>. It seems like you assume a child class of the class Rafrsr\LibArray2Object\AbstractContext to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145
    protected function prepareContext(AbstractContext $context)
146
    {
147
        parent::prepareContext($context);
148
149
        if ($context instanceof Object2ArrayContext) {
150
            $context->setReader($this->getReader() ?: new AccessorReader());
151
            $context->setNamingStrategy($this->getNamingStrategy() ?: new CamelCaseNamingStrategy());
152
        }
153
    }
154
}