Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (warning)
85.71%
6 / 7
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Collection
85.71% covered (warning)
85.71%
6 / 7
50.00% covered (danger)
50.00%
1 / 2
4.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 write
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Api;
6
7use LambdaTwelve\OneRecord\JsonLd\Nodes;
8use LambdaTwelve\OneRecord\Rdf\Iri;
9
10/**
11 * api:Collection: how a list of logistics events is returned. The spec's
12 * JSON-LD rule applies: no hasItem for an empty list, a single object for one
13 * item, an array for several.
14 */
15final class Collection
16{
17    private function __construct() {}
18
19    /**
20     * @param list<array<string, mixed>> $items compacted nodes without @context
21     * @return array<string, mixed>
22     */
23    public static function write(Iri $id, array $items): array
24    {
25        $node = ['@context' => Nodes::context(), '@id' => $id->value, '@type' => 'api:Collection', 'api:hasTotalItems' => \count($items)];
26        if (\count($items) === 1) {
27            $node['api:hasItem'] = $items[0];
28        } elseif ($items !== []) {
29            $node['api:hasItem'] = $items;
30        }
31
32        return $node;
33    }
34}