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

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