L10nJsonLdConverter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 41
ccs 23
cts 23
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B convertL10nResourceList() 0 29 5
1
<?php
2
3
namespace L10nBundle\Manager\JsonLd;
4
5
use L10nBundle\Entity\L10nResource;
6
use L10nBundle\Manager\L10nConverterInterface;
7
use ML\JsonLD\JsonLD;
8
use ML\IRI\IRI;
9
use ML\JsonLD\TypedValue;
10
use ML\JsonLD\Quad;
11
use ML\JsonLD\RdfConstants;
12
13
/**
14
 * @todo doc
15
 *
16
 * @author Cyril Otal
17
 */
18
class L10nJsonLdConverter implements L10nConverterInterface
19
{
20
    const NS = 'http://plop.org/l10n/ns/';
21
22
    /**
23
     * Convert a list of L10nResources in a JSON-LD file
24
     *
25
     * @param L10nResource[] $l10nResourceList
26
     *
27
     * @return string JSON-LD document
28
     */
29 1
    public function convertL10nResourceList(array $l10nResourceList)
30
    {
31 1
        $quadList = array();
32
33 1
        $c = 0;
34 1
        if (count($l10nResourceList)) {
35 1
            foreach ($l10nResourceList as $l10nResource) {
36 1
                $bNode = new IRI('_' . $c);
37 1
                $quadList[] = new Quad($bNode, new IRI(self::NS . 'key'), new IRI($l10nResource->getIdResource()));
38 1
                $quadList[] =
39 1
                    new Quad($bNode, new IRI(self::NS . 'localization'), new IRI($l10nResource->getIdLocalization()));
40 1
                $valueList = $l10nResource->getValueList();
41 1
                foreach ($valueList as $locale => $value) {
42 1
                    if (!is_int($locale)) {
43 1
                        $value .= '@' . $locale;
44 1
                    }
45 1
                    $quadList[] =
46 1
                        new Quad($bNode, new IRI(self::NS . 'value'), new TypedValue($value, RdfConstants::XSD_STRING));
47 1
                }
48 1
                $c++;
49 1
            }
50 1
        }
51
52 1
        $jsonLd = JsonLD::fromRdf($quadList);
53
        $compacted =
54 1
            JsonLD::compact($jsonLd, '{"@context": {"l10n" : "' . self::NS . '"}}', array('compactArrays' => false));
55
56 1
        return JsonLD::toString($compacted, true);
57
    }
58
}
59