Completed
Push — feature/other-validation ( c4ebdf...a3189f )
by Narcotic
152:45 queued 146:40
created

DocumentMap::getSerializerFields()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
ccs 12
cts 12
cp 1
cc 2
eloc 11
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * DocumentMap class file
4
 */
5
6
namespace Graviton\DocumentBundle\DependencyInjection\Compiler\Utils;
7
8
use Symfony\Component\Finder\Finder;
9
10
/**
11
 * Document map
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class DocumentMap
18
{
19
    /**
20
     * @var array
21
     */
22
    private $mappings = [];
23
    /**
24
     * @var Document[]
25
     */
26
    private $documents = [];
27
28
    /**
29
     * Constructor
30
     *
31
     * @param Finder $doctrineFinder   Doctrine mapping finder
32
     * @param Finder $serializerFinder Serializer mapping finder
33
     * @param Finder $validationFinder Validation mapping finder
34
     * @param Finder $schemaFinder     Schema finder
35
     */
36 14
    public function __construct(
37
        Finder $doctrineFinder,
38
        Finder $serializerFinder,
39
        Finder $validationFinder,
40
        Finder $schemaFinder
41
    ) {
42 14
        $doctrineMap = $this->loadDoctrineClassMap($doctrineFinder);
43 14
        $serializerMap = $this->loadSerializerClassMap($serializerFinder);
44 14
        $validationMap = $this->loadValidationClassMap($validationFinder);
45 14
        $schemaMap = $this->loadSchemaClassMap($schemaFinder);
46
47 14
        foreach ($doctrineMap as $className => $doctrineMapping) {
48 14
            $this->mappings[$className] = [
49 14
                'doctrine'   => $doctrineMap[$className],
50 14
                'serializer' => isset($serializerMap[$className]) ? $serializerMap[$className] : null,
51 14
                'validation' => isset($validationMap[$className]) ? $validationMap[$className] : null,
52 14
                'schema' => isset($schemaMap[$className]) ? $schemaMap[$className] : null,
53
            ];
54 7
        }
55 14
    }
56
57
    /**
58
     * Get document
59
     *
60
     * @param string $className Document class
61
     * @return Document
62
     */
63 14
    public function getDocument($className)
64
    {
65 14
        if (isset($this->documents[$className])) {
66 14
            return $this->documents[$className];
67
        }
68 14
        if (!isset($this->mappings[$className])) {
69
            throw new \InvalidArgumentException(sprintf('No XML mapping found for document "%s"', $className));
70
        }
71
72 14
        return $this->documents[$className] = $this->processDocument(
73 7
            $className,
74 14
            $this->mappings[$className]['doctrine'],
75 14
            $this->mappings[$className]['serializer'],
76 14
            $this->mappings[$className]['validation'],
77 14
            $this->mappings[$className]['schema']
78 7
        );
79
    }
80
81
    /**
82
     * Get all documents
83
     *
84
     * @return Document[]
85
     */
86 10
    public function getDocuments()
87
    {
88 10
        return array_map([$this, 'getDocument'], array_keys($this->mappings));
89
    }
90
91
    /**
92
     * Process document
93
     *
94
     * @param string      $className         Class name
95
     * @param \DOMElement $doctrineMapping   Doctrine XML mapping
96
     * @param \DOMElement $serializerMapping Serializer XML mapping
0 ignored issues
show
Documentation introduced by
Should the type for parameter $serializerMapping not be null|\DOMElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
97
     * @param \DOMElement $validationMapping Validation XML mapping
0 ignored issues
show
Documentation introduced by
Should the type for parameter $validationMapping not be null|\DOMElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
98
     * @param array       $schemaMapping     Schema mapping
0 ignored issues
show
Documentation introduced by
Should the type for parameter $schemaMapping not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
99
     *
100
     * @return Document
101
     */
102 14
    private function processDocument(
103
        $className,
104
        \DOMElement $doctrineMapping,
105
        \DOMElement $serializerMapping = null,
106
        \DOMElement $validationMapping = null,
107
        array $schemaMapping = null
108
    ) {
109 14
        if ($serializerMapping === null) {
110 2
            $serializerFields = [];
111 1
        } else {
112 14
            $serializerFields = array_reduce(
113 14
                $this->getSerializerFields($serializerMapping),
114
                function (array $fields, array $field) {
115 14
                    $fields[$field['fieldName']] = $field;
116 14
                    return $fields;
117 14
                },
118 14
                []
119 7
            );
120
        }
121
122 14
        if ($validationMapping === null) {
123 2
            $validationFields = [];
124 1
        } else {
125 14
            $validationFields = array_reduce(
126 14
                $this->getValidationFields($validationMapping),
127
                function (array $fields, array $field) {
128 8
                    $fields[$field['fieldName']] = $field;
129 8
                    return $fields;
130 14
                },
131 14
                []
132 7
            );
133
        }
134
135 14
        if ($schemaMapping === null) {
136 14
            $schemaFields = [];
137 7
        } else {
138 2
            $schemaFields = $schemaMapping;
139
        }
140
141 14
        $fields = [];
142 14
        foreach ($this->getDoctrineFields($doctrineMapping) as $doctrineField) {
143 14
            $serializerField = isset($serializerFields[$doctrineField['name']]) ?
144 14
                $serializerFields[$doctrineField['name']] :
145 14
                null;
146 14
            $validationField = isset($validationFields[$doctrineField['name']]) ?
147 11
                $validationFields[$doctrineField['name']] :
148 14
                null;
149 14
            $schemaField = isset($schemaFields[$doctrineField['name']]) ?
150 8
                $schemaFields[$doctrineField['name']] :
151 14
                null;
152
153 14
            if ($doctrineField['type'] === 'collection') {
154 2
                $fields[] = new ArrayField(
155 2
                    $serializerField === null ? 'array<string>' : $serializerField['fieldType'],
156 2
                    $doctrineField['name'],
157 2
                    $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
158 2
                    !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
159 2
                    $validationField === null ? false : $validationField['required'],
160 2
                    $serializerField === null ? false : $serializerField['searchable']
0 ignored issues
show
Unused Code introduced by
The call to ArrayField::__construct() has too many arguments starting with $serializerField === nul...izerField['searchable'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
161 1
                );
162 1
            } else {
163 14
                $fields[] = new Field(
164 14
                    $doctrineField['type'],
165 14
                    $doctrineField['name'],
166 14
                    $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
167 14
                    !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
168 14
                    $validationField === null ? false : $validationField['required'],
169 14
                    $serializerField === null ? false : $serializerField['searchable']
170 7
                );
171
            }
172 7
        }
173 14
        foreach ($this->getDoctrineEmbedOneFields($doctrineMapping) as $doctrineField) {
174 14
            $serializerField = isset($serializerFields[$doctrineField['name']]) ?
175 14
                $serializerFields[$doctrineField['name']] :
176 14
                null;
177 14
            $validationField = isset($validationFields[$doctrineField['name']]) ?
178 11
                $validationFields[$doctrineField['name']] :
179 14
                null;
180 14
            $schemaField = isset($schemaFields[$doctrineField['name']]) ?
181 8
                $schemaFields[$doctrineField['name']] :
182 14
                null;
183
184 14
            $fields[] = new EmbedOne(
185 14
                $this->getDocument($doctrineField['type']),
186 14
                $doctrineField['name'],
187 14
                $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
188 14
                !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
189 14
                $validationField === null ? false : $validationField['required'],
190 14
                $serializerField === null ? false : $serializerField['searchable']
0 ignored issues
show
Unused Code introduced by
The call to EmbedOne::__construct() has too many arguments starting with $serializerField === nul...izerField['searchable'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
191 7
            );
192 7
        }
193 14
        foreach ($this->getDoctrineEmbedManyFields($doctrineMapping) as $doctrineField) {
194 14
            $serializerField = isset($serializerFields[$doctrineField['name']]) ?
195 14
                $serializerFields[$doctrineField['name']] :
196 14
                null;
197 14
            $validationField = isset($validationFields[$doctrineField['name']]) ?
198 8
                $validationFields[$doctrineField['name']] :
199 14
                null;
200
201 14
            $fields[] = new EmbedMany(
202 14
                $this->getDocument($doctrineField['type']),
203 14
                $doctrineField['name'],
204 14
                $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
205 14
                !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
206 14
                $validationField === null ? false : $validationField['required']
207 7
            );
208 7
        }
209
210 14
        return new Document($className, $fields);
211
    }
212
213
    /**
214
     * Load doctrine class map
215
     *
216
     * @param Finder $finder Mapping finder
217
     * @return array
218
     */
219 14 View Code Duplication
    private function loadDoctrineClassMap(Finder $finder)
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...
220
    {
221 14
        $classMap = [];
222 14
        foreach ($finder as $file) {
223 14
            $document = new \DOMDocument();
224 14
            $document->load($file);
225
226 14
            $xpath = new \DOMXPath($document);
227 14
            $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
228
229 14
            $classMap = array_reduce(
230 14
                iterator_to_array($xpath->query('//*[self::doctrine:document or self::doctrine:embedded-document]')),
231
                function (array $classMap, \DOMElement $element) {
232 14
                    $classMap[$element->getAttribute('name')] = $element;
233 14
                    return $classMap;
234 14
                },
235
                $classMap
236 7
            );
237 7
        }
238
239 14
        return $classMap;
240
    }
241
242
    /**
243
     * Load serializer class map
244
     *
245
     * @param Finder $finder Mapping finder
246
     * @return array
247
     */
248 14 View Code Duplication
    private function loadSerializerClassMap(Finder $finder)
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...
249
    {
250 14
        $classMap = [];
251 14
        foreach ($finder as $file) {
252 14
            $document = new \DOMDocument();
253 14
            $document->load($file);
254
255 14
            $xpath = new \DOMXPath($document);
256
257 14
            $classMap = array_reduce(
258 14
                iterator_to_array($xpath->query('//class')),
259
                function (array $classMap, \DOMElement $element) {
260 14
                    $classMap[$element->getAttribute('name')] = $element;
261 14
                    return $classMap;
262 14
                },
263
                $classMap
264 7
            );
265 7
        }
266
267 14
        return $classMap;
268
    }
269
270
    /**
271
     * Load schema class map
272
     *
273
     * @param Finder $finder Mapping finder
274
     * @return array
275
     */
276 14
    private function loadSchemaClassMap(Finder $finder)
277
    {
278 14
        $classMap = [];
279 14
        foreach ($finder as $file) {
280 14
            $schema = json_decode(file_get_contents($file), true);
281
282 14
            if (!isset($schema['x-documentClass'])) {
283 14
                continue;
284
            }
285
286 2 View Code Duplication
            foreach ($schema['required'] as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
287 2
                $classMap[$schema['x-documentClass']][$field]['required'] = true;
288 1
            }
289 2 View Code Duplication
            foreach ($schema['searchable'] as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
290 2
                $classMap[$schema['x-documentClass']][$field]['searchable'] = true;
291 1
            }
292 2 View Code Duplication
            foreach ($schema['readOnlyFields'] as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
293 2
                $classMap[$schema['x-documentClass']][$field]['readOnly'] = true;
294 1
            }
295 7
        }
296
297 14
        return $classMap;
298
    }
299
300
    /**
301
     * Load validation class map
302
     *
303
     * @param Finder $finder Mapping finder
304
     * @return array
305
     */
306 14 View Code Duplication
    private function loadValidationClassMap(Finder $finder)
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...
307
    {
308 14
        $classMap = [];
309 14
        foreach ($finder as $file) {
310 14
            $document = new \DOMDocument();
311 14
            $document->load($file);
312
313 14
            $xpath = new \DOMXPath($document);
314 14
            $xpath->registerNamespace('constraint', 'http://symfony.com/schema/dic/constraint-mapping');
315
316 14
            $classMap = array_reduce(
317 14
                iterator_to_array($xpath->query('//constraint:class')),
318
                function (array $classMap, \DOMElement $element) {
319 14
                    $classMap[$element->getAttribute('name')] = $element;
320 14
                    return $classMap;
321 14
                },
322
                $classMap
323 7
            );
324 7
        }
325
326 14
        return $classMap;
327
    }
328
329
    /**
330
     * Get serializer fields
331
     *
332
     * @param \DOMElement $mapping Serializer XML mapping
333
     * @return array
334
     */
335 14
    private function getSerializerFields(\DOMElement $mapping)
336
    {
337 14
        $xpath = new \DOMXPath($mapping->ownerDocument);
338
339 14
        return array_map(
340
            function (\DOMElement $element) {
341
                return [
342 14
                    'fieldName'   => $element->getAttribute('name'),
343 14
                    'fieldType'   => $this->getSerializerFieldType($element),
344 14
                    'exposedName' => $element->getAttribute('serialized-name') ?: $element->getAttribute('name'),
345 14
                    'readOnly'    => $element->getAttribute('read-only') === 'true',
346 14
                    'searchable'  => $element->getAttribute('searchable') === 'true',
347 7
                ];
348 14
            },
349 14
            iterator_to_array($xpath->query('property', $mapping))
350 7
        );
351
    }
352
353
    /**
354
     * Get serializer field type
355
     *
356
     * @param \DOMElement $field Field node
357
     * @return string|null
358
     */
359 14
    private function getSerializerFieldType(\DOMElement $field)
360
    {
361 14
        if ($field->getAttribute('type')) {
362 14
            return $field->getAttribute('type');
363
        }
364
365 2
        $xpath = new \DOMXPath($field->ownerDocument);
366
367 2
        $type = $xpath->query('type', $field)->item(0);
368 2
        return $type === null ? null : $type->nodeValue;
369
    }
370
371
    /**
372
     * Get validation fields
373
     *
374
     * @param \DOMElement $mapping Validation XML mapping
375
     * @return array
376
     */
377 14
    private function getValidationFields(\DOMElement $mapping)
378
    {
379 14
        $xpath = new \DOMXPath($mapping->ownerDocument);
380 14
        $xpath->registerNamespace('constraint', 'http://symfony.com/schema/dic/constraint-mapping');
381
382 14
        return array_map(
383
            function (\DOMElement $element) use ($xpath) {
384 8
                $constraints = $xpath->query('constraint:constraint[@name="NotBlank" or @name="NotNull"]', $element);
385
                return [
386 8
                    'fieldName' => $element->getAttribute('name'),
387 8
                    'required'  => $constraints->length > 0,
388 4
                ];
389 14
            },
390 14
            iterator_to_array($xpath->query('constraint:property', $mapping))
391 7
        );
392
    }
393
394
    /**
395
     * Get doctrine document fields
396
     *
397
     * @param \DOMElement $mapping Doctrine XML mapping
398
     * @return array
399
     */
400 14 View Code Duplication
    private function getDoctrineFields(\DOMElement $mapping)
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...
401
    {
402 14
        $xpath = new \DOMXPath($mapping->ownerDocument);
403 14
        $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
404
405 14
        return array_map(
406
            function (\DOMElement $element) {
407
                return [
408 14
                    'name' => $element->getAttribute('fieldName'),
409 14
                    'type' => $element->getAttribute('type'),
410 7
                ];
411 14
            },
412 14
            iterator_to_array($xpath->query('doctrine:field', $mapping))
413 7
        );
414
    }
415
416
    /**
417
     * Get doctrine document embed-one fields
418
     *
419
     * @param \DOMElement $mapping Doctrine XML mapping
420
     * @return array
421
     */
422 14 View Code Duplication
    private function getDoctrineEmbedOneFields(\DOMElement $mapping)
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...
423
    {
424 14
        $xpath = new \DOMXPath($mapping->ownerDocument);
425 14
        $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
426
427 14
        return array_map(
428
            function (\DOMElement $element) {
429
                return [
430 14
                    'name' => $element->getAttribute('field'),
431 14
                    'type' => $element->getAttribute('target-document'),
432 7
                ];
433 14
            },
434 14
            iterator_to_array($xpath->query('*[self::doctrine:embed-one or self::doctrine:reference-one]', $mapping))
435 7
        );
436
    }
437
438
    /**
439
     * Get doctrine document embed-many fields
440
     *
441
     * @param \DOMElement $mapping Doctrine XML mapping
442
     * @return array
443
     */
444 14 View Code Duplication
    private function getDoctrineEmbedManyFields(\DOMElement $mapping)
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...
445
    {
446 14
        $xpath = new \DOMXPath($mapping->ownerDocument);
447 14
        $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
448
449 14
        return array_map(
450 14
            function (\DOMElement $element) {
451
                return [
452 14
                    'name' => $element->getAttribute('field'),
453 14
                    'type' => $element->getAttribute('target-document'),
454 7
                ];
455 14
            },
456 14
            iterator_to_array($xpath->query('*[self::doctrine:embed-many or self::doctrine:reference-many]', $mapping))
457 7
        );
458
    }
459
460
    /**
461
     * Gets an array of all fields, flat with full internal name in dot notation as key and
462
     * the exposed field name as value. You can pass a callable to limit the fields return a subset of fields.
463
     * If the callback returns true, the field will be included in the output. You will get the field definition
464
     * passed to your callback.
465
     *
466
     * @param Document $document       The document
467
     * @param string   $documentPrefix Document field prefix
468
     * @param string   $exposedPrefix  Exposed field prefix
469
     * @param callable $callback       An optional callback where you can influence the number of fields returned
0 ignored issues
show
Documentation introduced by
Should the type for parameter $callback not be null|callable? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
470
     *
471
     * @return array
472
     */
473 4
    public function getFieldNamesFlat(
474
        Document $document,
475
        $documentPrefix = '',
476
        $exposedPrefix = '',
477
        callable $callback = null
478
    ) {
479 4
        $result = [];
480 4
        foreach ($document->getFields() as $field) {
481 4 View Code Duplication
            if ($this->getFlatFieldCheckCallback($field, $callback)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
482 4
                $result[$documentPrefix . $field->getFieldName()] = $exposedPrefix . $field->getExposedName();
483 2
            }
484
485 4
            if ($field instanceof ArrayField) {
486 2 View Code Duplication
                if ($this->getFlatFieldCheckCallback($field, $callback)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
487 2
                    $result[$documentPrefix . $field->getFieldName() . '.0'] =
488 2
                        $exposedPrefix . $field->getExposedName() . '.0';
489 1
                }
490 2
            } elseif ($field instanceof EmbedOne) {
491 4
                $result = array_merge(
492 2
                    $result,
493 4
                    $this->getFieldNamesFlat(
494 4
                        $field->getDocument(),
495 4
                        $documentPrefix.$field->getFieldName().'.',
496 4
                        $exposedPrefix.$field->getExposedName().'.',
497
                        $callback
498 2
                    )
499 2
                );
500 2
            } elseif ($field instanceof EmbedMany) {
501 4 View Code Duplication
                if ($this->getFlatFieldCheckCallback($field, $callback)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
502 4
                    $result[$documentPrefix . $field->getFieldName() . '.0'] =
503 4
                        $exposedPrefix . $field->getExposedName() . '.0';
504 2
                }
505 4
                $result = array_merge(
506 2
                    $result,
507 4
                    $this->getFieldNamesFlat(
508 4
                        $field->getDocument(),
509 4
                        $documentPrefix.$field->getFieldName().'.0.',
510 4
                        $exposedPrefix.$field->getExposedName().'.0.',
511
                        $callback
512 2
                    )
513 2
                );
514 2
            }
515 2
        }
516
517 4
        return $result;
518
    }
519
520
    /**
521
     * Simple function to check whether a given shall be returned in the output of getFieldNamesFlat
522
     * and the optional given callback there.
523
     *
524
     * @param AbstractField $field    field
525
     * @param callable|null $callback optional callback
526
     *
527
     * @return bool|mixed true if field should be returned, false otherwise
528
     */
529 4
    private function getFlatFieldCheckCallback($field, callable $callback = null)
530
    {
531 4
        if (!is_callable($callback)) {
532 4
            return true;
533
        }
534
535 2
        return call_user_func($callback, $field);
536
    }
537
}
538