Array2ObjectBuilder::useIdenticalMatcher()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the rafrsr/lib-array2object package.
5
 *
6
 * (c) Rafael SR <https://github.com/rafrsr>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Rafrsr\LibArray2Object;
12
13
use Rafrsr\LibArray2Object\Matcher\CallableMatcher;
14
use Rafrsr\LibArray2Object\Matcher\CamelizeMatcher;
15
use Rafrsr\LibArray2Object\Matcher\IdenticalMatcher;
16
use Rafrsr\LibArray2Object\Matcher\LevenshteinMatcher;
17
use Rafrsr\LibArray2Object\Matcher\MapMatcher;
18
use Rafrsr\LibArray2Object\Traits\MatcherAwareTrait;
19
use Rafrsr\LibArray2Object\Writer\AccessorWriter;
20
use Rafrsr\LibArray2Object\Writer\PropertyWriterInterface;
21
use Rafrsr\LibArray2Object\Writer\ReflectionWriter;
22
23
/**
24
 * Class Array2ObjectBuilder.
25
 */
26
class Array2ObjectBuilder extends AbstractBuilder
27
{
28
    use MatcherAwareTrait;
29
30
    /**
31
     * @var PropertyWriterInterface
32
     */
33
    private $writer;
34
35
    /**
36
     * @return PropertyWriterInterface
37
     */
38
    public function getWriter()
39
    {
40
        return $this->writer;
41
    }
42
43
    /**
44
     * @param PropertyWriterInterface $writer
45
     *
46
     * @return $this
47
     */
48
    public function setWriter($writer)
49
    {
50
        $this->writer = $writer;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Array keys and property names are identical.
57
     *
58
     * @return $this
59
     */
60
    public function useIdenticalMatcher()
61
    {
62
        $this->setMatcher(new IdenticalMatcher());
63
64
        return $this;
65
    }
66
67
    /**
68
     * Array keys and property names are compared using some property possible versions
69
     * e.g, propertyName => property_name.
70
     *
71
     * @return $this
72
     */
73
    public function useCamelizeMatcher()
74
    {
75
        $this->setMatcher(new CamelizeMatcher());
76
77
        return $this;
78
    }
79
80
    /**
81
     * Array keys and property names are compared using levenshtein algorithm to find similar strings.
82
     *
83
     * @return $this
84
     */
85
    public function useLevenshteinMatcher()
86
    {
87
        $this->setMatcher(new LevenshteinMatcher());
88
89
        return $this;
90
    }
91
92
    /**
93
     * Array keys and property names are compared using the given property and array key map.
94
     *
95
     * @param array $map
96
     *
97
     * @return $this
98
     */
99
    public function useMapMatcher(array $map)
100
    {
101
        $this->setMatcher(new MapMatcher($map));
102
103
        return $this;
104
    }
105
106
    /**
107
     * Array keys and property names are compared using custom function
108
     * the given function receive two parameters \ReflectionProperty $property, $givenName.
109
     *
110
     * @param callable $callback
111
     *
112
     * @return $this
113
     */
114
    public function useCallableMatcher(callable $callback)
115
    {
116
        $this->setMatcher(new CallableMatcher($callback));
117
118
        return $this;
119
    }
120
121
    /**
122
     * Write the property directly without use setters.
123
     *
124
     * @param bool $onlyPublicProperties only public properties should be exported
125
     *
126
     * @return $this
127
     */
128
    public function disableSetters($onlyPublicProperties = false)
129
    {
130
        $this->setWriter(new ReflectionWriter($onlyPublicProperties));
131
132
        return $this;
133
    }
134
135
    /**
136
     * Build custom Array2Object instance.
137
     */
138
    public function build()
139
    {
140
        if ($this->getContext()) {
141
            $context = $this->getContext();
142
        } else {
143
            $context = new Array2ObjectContext();
144
        }
145
146
        $this->prepareContext($context);
147
148
        return new Array2Object($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\Array2ObjectContext>. 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...
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    protected function prepareContext(AbstractContext $context)
155
    {
156
        parent::prepareContext($context);
157
158
        if ($context instanceof Array2ObjectContext) {
159
            $context->setWriter($this->getWriter() ?: new AccessorWriter());
160
            $context->setMatcher($this->getMatcher() ?: new CamelizeMatcher());
161
        }
162
    }
163
}
164