SchemaToArrayConverter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 14
c 1
b 0
f 0
dl 0
loc 39
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A convert() 0 23 5
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Renderer;
6
7
use Cycle\ORM\SchemaInterface;
8
9
/**
10
 * The class converts the {@see SchemaInterface} class implementation into a Cycle ORM specific array
11
 */
12
final class SchemaToArrayConverter
13
{
14
    /** @var array<string, int> */
15
    private array $constants;
16
17
    public function __construct(?ConstantsInterface $constants = null)
18
    {
19
        $this->constants = ($constants ?? new SchemaConstants())->all();
20
    }
21
22
    /**
23
     * @param SchemaInterface $schema
24
     * @param array<int, int> $customProperties
25
     *
26
     * @return array<string, array<int, mixed>>
27
     */
28
    public function convert(SchemaInterface $schema, array $customProperties = []): array
29
    {
30
        // For CycleORM >= 2.x
31
        if (method_exists($schema, 'toArray')) {
32
            return $schema->toArray();
33
        }
34
35
        $result = [];
36
37
        $properties = array_merge($this->constants, $customProperties);
38
39
        foreach ($schema->getRoles() as $role) {
40
            foreach ($properties as $property) {
41
                /** @psalm-suppress ReservedWord */
42
                $value = $schema->define($role, $property);
43
                if ($value === null) {
44
                    continue;
45
                }
46
                $result[$role][$property] = $value;
47
            }
48
        }
49
50
        return $result;
51
    }
52
}
53