Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
11 / 12
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
DataModelVersion
91.67% covered (success)
91.67%
11 / 12
80.00% covered (warning)
80.00%
4 / 5
8.04
0.00% covered (danger)
0.00%
0 / 1
 latest
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 tryFromString
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 isAtLeast
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 edition
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
3.14
 ontologyVersionIri
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Spec;
6
7use LogicException;
8
9/**
10 * A version of the ONE Record cargo ontology (the data model).
11 *
12 * The API is independent of the data model version by specification: terms are
13 * never versioned in payloads, and a server validates against the newest
14 * ontology it knows. The version still matters when publishing to a partner
15 * that runs an older ontology, so vocabulary terms record the version they
16 * appeared in and builders can be given a ceiling.
17 */
18enum DataModelVersion: string
19{
20    case V3_2 = '3.2';
21    case V3_3 = '3.3';
22
23    public static function latest(): self
24    {
25        $cases = self::cases();
26
27        return $cases[\count($cases) - 1];
28    }
29
30    public static function tryFromString(string $version): ?self
31    {
32        $normalised = ltrim(trim($version), 'vV');
33        // IATA writes "3.2" in owl:versionInfo but "3.2.0" in some examples.
34        if (preg_match('/^(\d+\.\d+)\.0$/', $normalised, $matches) === 1) {
35            $normalised = $matches[1];
36        }
37
38        return self::tryFrom($normalised);
39    }
40
41    public function isAtLeast(self $other): bool
42    {
43        return version_compare($this->value, $other->value, '>=');
44    }
45
46    public function edition(): Edition
47    {
48        foreach (Edition::cases() as $edition) {
49            if ($edition->dataModelVersion() === $this) {
50                return $edition;
51            }
52        }
53
54        throw new LogicException("No edition declares data model version {$this->value}.");
55    }
56
57    /**
58     * The owl:versionIRI of the cargo ontology at this version, as advertised
59     * in api:hasSupportedOntologyVersion.
60     */
61    public function ontologyVersionIri(): string
62    {
63        return Namespaces::CARGO_ONTOLOGY . '/' . $this->value;
64    }
65}