AbstractDefinitionComponent   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A forceIdentifierForProperty() 0 16 5
1
<?php
2
declare(strict_types=1);
3
4
/*
5
 * Copyright (C)
6
 * Nathan Boiron <[email protected]>
7
 * Romain Canon <[email protected]>
8
 *
9
 * This file is part of the TYPO3 NotiZ project.
10
 * It is free software; you can redistribute it and/or modify it
11
 * under the terms of the GNU General Public License, either
12
 * version 3 of the License, or any later version.
13
 *
14
 * For the full copyright and license information, see:
15
 * http://www.gnu.org/licenses/gpl-3.0.html
16
 */
17
18
namespace CuyZ\Notiz\Core\Definition\Tree;
19
20
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
21
use Romm\ConfigurationObject\Traits\ConfigurationObject\MagicMethodsTrait;
22
23
abstract class AbstractDefinitionComponent
24
{
25
    use MagicMethodsTrait;
26
27
    /**
28
     * Utility function to automatically fill the `identifier` property of the
29
     * entries in the given data, with the key in the array of each entry.
30
     *
31
     * Before:
32
     * -------
33
     *
34
     * $data = [
35
     *     'myProperty' => [
36
     *          'prop1' => ['type' => 'foo'],
37
     *          'prop2' => ['type' => 'bar']
38
     *     ]
39
     * ];
40
     *
41
     * After:
42
     * ------
43
     *
44
     * ```
45
     * $data = [
46
     *     'myProperty' => [
47
     *          'prop1' => [
48
     *              'identifier' => 'prop1',
49
     *              'type'       => 'foo'
50
     *          ],
51
     *          'prop2' => [
52
     *              'identifier' => 'prop2',
53
     *              'type'       => 'bar'
54
     *          ],
55
     *     ]
56
     * ];
57
     * ```
58
     *
59
     * @param DataPreProcessor $processor
60
     * @param string $property
61
     * @param string $name
62
     */
63
    protected static function forceIdentifierForProperty(DataPreProcessor $processor, string $property, string $name = 'identifier')
64
    {
65
        $data = $processor->getData();
66
        $data = is_array($data)
67
            ? $data
68
            : [];
69
70
        if (isset($data[$property])) {
71
            foreach ($data[$property] as $key => $entry) {
72
                if (is_array($entry)) {
73
                    $data[$property][$key][$name] = $key;
74
                }
75
            }
76
        }
77
78
        $processor->setData($data);
79
    }
80
}
81