Passed
Push — master ( 1e5bb4...983543 )
by Dante
02:00
created

RelationsTools::toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
nc 2
nop 1
dl 0
loc 16
rs 9.9
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * BEdita, API-first content management framework
7
 * Copyright 2024 ChannelWeb Srl, Chialab Srl
8
 *
9
 * This file is part of BEdita: you can redistribute it and/or modify
10
 * it under the terms of the GNU Lesser General Public License as published
11
 * by the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
15
 */
16
17
namespace App\Utility;
18
19
use Cake\Utility\Hash;
20
21
/**
22
 * Relations tools
23
 */
24
class RelationsTools
25
{
26
    /**
27
     * Convert relations to string.
28
     *
29
     * @param array $relations Relations to convert.
30
     * @return string
31
     */
32
    public static function toString(array $relations): string
33
    {
34
        $rrs = [];
35
        foreach ($relations as $item) {
36
            $id = (string)Hash::get($item, 'id');
37
            $type = (string)Hash::get($item, 'type');
38
            $meta = json_encode((array)Hash::get($item, 'meta.relation', []));
39
            $rrs[] = sprintf(
40
                '%s-%s-%s',
41
                $id,
42
                $type,
43
                $meta
44
            );
45
        }
46
47
        return implode(',', $rrs);
48
    }
49
}
50