• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jojo1981 / data-resolver / 170

pending completion
170

cron

travis-ci-com

jojo1981
Fix error: `Argument 1 passed to Jojo1981\DataResolver\Resolver\Context::pushPathPart() must be of the type string, int given, called in...`

1 of 1 new or added line in 1 file covered. (100.0%)

454 of 868 relevant lines covered (52.3%)

5.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Factory.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the jojo1981/data-resolver package
4
 *
5
 * Copyright (c) 2019 Joost Nijhuis <jnijhuis81@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed in the root of the source code
9
 */
10
namespace Jojo1981\DataResolver;
11

12
use Jojo1981\DataResolver\Comparator\ComparatorInterface;
13
use Jojo1981\DataResolver\Comparator\DefaultComparator;
14
use Jojo1981\DataResolver\Factory\Exception\FactoryException;
15
use Jojo1981\DataResolver\Factory\ExtractorBuilderFactory;
16
use Jojo1981\DataResolver\Factory\PredicateBuilderFactory;
17
use Jojo1981\DataResolver\Factory\ResolverBuilderFactory;
18
use Jojo1981\DataResolver\Handler\MergeHandler\DefaultMergeHandler;
19
use Jojo1981\DataResolver\Handler\MergeHandlerInterface;
20
use Jojo1981\DataResolver\Handler\PropertyHandler\AssociativeArrayPropertyHandler;
21
use Jojo1981\DataResolver\Handler\PropertyHandler\CompositePropertyHandler;
22
use Jojo1981\DataResolver\Handler\PropertyHandler\ObjectPropertyHandler;
23
use Jojo1981\DataResolver\Handler\PropertyHandlerInterface;
24
use Jojo1981\DataResolver\Handler\SequenceHandler\ArraySequenceHandler;
25
use Jojo1981\DataResolver\Handler\SequenceHandler\CompositeSequenceHandler;
26
use Jojo1981\DataResolver\Handler\SequenceHandlerInterface;
27
use Jojo1981\DataResolver\NamingStrategy\DefaultNamingStrategy;
28
use Jojo1981\DataResolver\NamingStrategy\NamingStrategyInterface;
29
use function array_push;
30
use function in_array;
31

32
/**
33
 * @api
34
 * @package Jojo1981\DataResolver
35
 */
36
final class Factory
37
{
38
    /** @var ExtractorBuilderFactory|null */
39
    private ?ExtractorBuilderFactory $extractorBuilderFactory = null;
40

41
    /** @var PredicateBuilderFactory|null */
42
    private ?PredicateBuilderFactory $predicateBuilderFactory = null;
43

44
    /** @var ResolverBuilderFactory|null */
45
    private ?ResolverBuilderFactory $resolverBuilderFactory = null;
46

47
    /** @var PropertyHandlerInterface|null */
48
    private ?PropertyHandlerInterface $propertyHandler = null;
49

50
    /** @var SequenceHandlerInterface|null */
51
    private ?SequenceHandlerInterface $sequenceHandler = null;
52

53
    /** @var MergeHandlerInterface|null */
54
    private ?MergeHandlerInterface $mergeHandler = null;
55

56
    /** @var ComparatorInterface|null */
57
    private ?ComparatorInterface $comparator = null;
58

59
    /** @var bool */
60
    private bool $isFrozen = false;
61

62
    /** @var NamingStrategyInterface|null */
63
    private ?NamingStrategyInterface $namingStrategy = null;
64

65
    /** @var PropertyHandlerInterface[] */
66
    private array $propertyHandlers = [];
67

68
    /** @var SequenceHandlerInterface[] */
69
    private array $sequenceHandlers = [];
70

71
    /**
72
     * @return $this
73
     * @throws FactoryException
74
     */
75
    public function useDefaultPropertyHandlers(): self
76
    {
77
        $this->assertNotFrozen();
×
78
        foreach ($this->getDefaultPropertyHandlers() as $propertyHandler) {
×
79
            $this->registerPropertyHandler($propertyHandler);
×
80
        }
81

82
        return $this;
×
83
    }
84

85
    /**
86
     * @return $this
87
     * @throws FactoryException
88
     */
89
    public function useDefaultSequenceHandlers(): self
90
    {
91
        $this->assertNotFrozen();
×
92
        foreach ($this->getDefaultSequenceHandlers() as $sequenceHandler) {
×
93
            $this->registerSequenceHandler($sequenceHandler);
×
94
        }
95

96
        return $this;
×
97
    }
98

99
    /**
100
     * @param PropertyHandlerInterface $propertyHandler
101
     * @return $this
102
     * @throws FactoryException
103
     */
104
    public function registerPropertyHandler(PropertyHandlerInterface $propertyHandler): self
105
    {
106
        $this->assertNotFrozen();
×
107
        if (!in_array($propertyHandler, $this->propertyHandlers, true)) {
×
108
            $this->propertyHandlers[] = $propertyHandler;
×
109
        }
110

111
        return $this;
×
112
    }
113

114
    /**
115
     * @param SequenceHandlerInterface $sequenceHandler
116
     * @return $this
117
     * @throws FactoryException
118
     */
119
    public function registerSequenceHandler(SequenceHandlerInterface $sequenceHandler): self
120
    {
121
        $this->assertNotFrozen();
×
122
        if (!in_array($sequenceHandler, $this->sequenceHandlers, true)) {
×
123
            $this->sequenceHandlers[] = $sequenceHandler;
×
124
        }
125

126
        return $this;
×
127
    }
128

129
    /**
130
     * @param NamingStrategyInterface $namingStrategy
131
     * @return $this
132
     * @throws FactoryException
133
     */
134
    public function setNamingStrategy(NamingStrategyInterface $namingStrategy): self
135
    {
136
        $this->assertNotFrozen();
×
137
        $this->namingStrategy = $namingStrategy;
×
138

139
        return $this;
×
140
    }
141

142
    /**
143
     * @param MergeHandlerInterface $mergeHandler
144
     * @return $this
145
     * @throws FactoryException
146
     */
147
    public function setMergeHandler(MergeHandlerInterface $mergeHandler): self
148
    {
149
        $this->assertNotFrozen();
×
150
        $this->mergeHandler = $mergeHandler;
×
151

152
        return $this;
×
153
    }
154

155
    /**
156
     * @param ComparatorInterface $comparator
157
     * @return $this
158
     * @throws FactoryException
159
     */
160
    public function setComparator(ComparatorInterface $comparator): self
161
    {
162
        $this->assertNotFrozen();
×
163
        $this->comparator = $comparator;
×
164

165
        return $this;
×
166
    }
167

168
    /**
169
     * @return ResolverBuilderFactory
170
     */
171
    public function getResolverBuilderFactory(): ResolverBuilderFactory
172
    {
173
        if (null === $this->resolverBuilderFactory) {
×
174
            $this->isFrozen = true;
×
175
            $this->resolverBuilderFactory = new ResolverBuilderFactory(
×
176
                $this->getExtractorBuilderFactory(),
×
177
                $this->getPredicateBuilderFactory()
×
178
            );
×
179
        }
180

181
        return $this->resolverBuilderFactory;
×
182
    }
183

184
    /**
185
     * @return ExtractorBuilderFactory
186
     */
187
    private function getExtractorBuilderFactory(): ExtractorBuilderFactory
188
    {
189
        if (null === $this->extractorBuilderFactory) {
×
190
            $this->extractorBuilderFactory = new ExtractorBuilderFactory(
×
191
                $this->getNamingStrategy(),
×
192
                $this->getPropertyHandler(),
×
193
                $this->getSequenceHandler(),
×
194
                $this->getMergeHandler()
×
195
            );
×
196
        }
197

198
        return $this->extractorBuilderFactory;
×
199
    }
200

201
    /**
202
     * @return PredicateBuilderFactory
203
     */
204
    private function getPredicateBuilderFactory(): PredicateBuilderFactory
205
    {
206
        if (null === $this->predicateBuilderFactory) {
×
207
            $this->predicateBuilderFactory = new PredicateBuilderFactory(
×
208
                $this->getExtractorBuilderFactory(),
×
209
                $this->getSequenceHandler(),
×
210
                $this->getComparator()
×
211
            );
×
212
        }
213

214
        return $this->predicateBuilderFactory;
×
215
    }
216

217
    /**
218
     * @return PropertyHandlerInterface
219
     */
220
    private function getPropertyHandler(): PropertyHandlerInterface
221
    {
222
        if (null === $this->propertyHandler) {
×
223
            if (empty($this->propertyHandlers)) {
×
224
                array_push($this->propertyHandlers, ...$this->getDefaultPropertyHandlers());
×
225
            }
226
            $this->propertyHandler = new CompositePropertyHandler($this->propertyHandlers);
×
227
        }
228

229
        return $this->propertyHandler;
×
230
    }
231

232
    /**
233
     * @return SequenceHandlerInterface
234
     */
235
    private function getSequenceHandler(): SequenceHandlerInterface
236
    {
237
        if (null === $this->sequenceHandler) {
×
238
            if (empty($this->sequenceHandlers)) {
×
239
                array_push($this->sequenceHandlers, ...$this->getDefaultSequenceHandlers());
×
240
            }
241
            $this->sequenceHandler = new CompositeSequenceHandler($this->sequenceHandlers);
×
242
        }
243

244
        return $this->sequenceHandler;
×
245
    }
246

247
    /**
248
     * @return PropertyHandlerInterface[]
249
     */
250
    private function getDefaultPropertyHandlers(): array
251
    {
252
        return [new ObjectPropertyHandler(), new AssociativeArrayPropertyHandler()];
×
253
    }
254

255
    /**
256
     * @return SequenceHandlerInterface[]
257
     */
258
    private function getDefaultSequenceHandlers(): array
259
    {
260
        return [new ArraySequenceHandler()];
×
261
    }
262

263
    /**
264
     * @return NamingStrategyInterface
265
     */
266
    private function getNamingStrategy(): NamingStrategyInterface
267
    {
268
        return $this->namingStrategy ?? new DefaultNamingStrategy();
×
269
    }
270

271
    /**
272
     * @return MergeHandlerInterface
273
     */
274
    private function getMergeHandler(): MergeHandlerInterface
275
    {
276
        return $this->mergeHandler ?? new DefaultMergeHandler();
×
277
    }
278

279
    /**
280
     * @return ComparatorInterface
281
     */
282
    private function getComparator(): ComparatorInterface
283
    {
284
        return $this->comparator ?? new DefaultComparator();
×
285
    }
286

287
    /**
288
     * @return void
289
     * @throws FactoryException
290
     */
291
    private function assertNotFrozen(): void
292
    {
293
        if ($this->isFrozen) {
×
294
            throw new FactoryException(
×
295
                'Can not modify the factory while it\'s frozen. The factory gets frozen when the factory has already ' .
×
296
                'build a resolver builder factory'
×
297
            );
×
298
        }
299
    }
300
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc