Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
66.67% covered (warning)
66.67%
10 / 15
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
QtiIdentifierGenerator
66.67% covered (warning)
66.67%
10 / 15
33.33% covered (danger)
33.33%
1 / 3
10.37
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 generate
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
2.02
 getResource
50.00% covered (danger)
50.00%
4 / 8
0.00% covered (danger)
0.00%
0 / 1
8.12
1<?php
2
3namespace oat\taoQtiTest\models\IdentifierGenerator\Generator;
4
5use core_kernel_classes_Resource;
6use InvalidArgumentException;
7use oat\generis\model\data\Ontology;
8use oat\tao\model\IdentifierGenerator\Generator\IdentifierGeneratorInterface;
9use qtism\common\utils\Format;
10
11class QtiIdentifierGenerator implements IdentifierGeneratorInterface
12{
13    private Ontology $ontology;
14
15    public function __construct(Ontology $ontology)
16    {
17        $this->ontology = $ontology;
18    }
19
20    public function generate(array $options = []): string
21    {
22        $resource = $this->getResource($options);
23        $label = $resource->getLabel();
24
25        $identifier = null;
26
27        if (preg_match('/^\d/', $label)) {
28            $identifier = 't_' . $label;
29        }
30
31        return str_replace('_', '-', Format::sanitizeIdentifier($identifier));
32    }
33
34    private function getResource(array $options): core_kernel_classes_Resource
35    {
36        if (isset($options[self::OPTION_RESOURCE_ID]) && is_string($options[self::OPTION_RESOURCE_ID])) {
37            return $this->ontology->getResource($options[self::OPTION_RESOURCE_ID]);
38        }
39
40        if (
41            isset($options[self::OPTION_RESOURCE])
42            && $options[self::OPTION_RESOURCE] instanceof core_kernel_classes_Resource
43        ) {
44            return $options[self::OPTION_RESOURCE];
45        }
46
47        throw new InvalidArgumentException(
48            'Test QTI Identifier generation failure: resource is required'
49        );
50    }
51}