JsonSerializing   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A json() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\ImmutableCollection;
6
7
use function json_encode;
8
use Stratadox\Collection\Collection;
9
use Stratadox\Collection\ConversionFailed;
10
use Stratadox\Collection\JsonSerializable;
11
use Throwable;
12
13
/**
14
 * Behaviour to serialize the collection to json.
15
 *
16
 * Provides access to serialisation behaviour in the form of a method that
17
 * serializes the collection to a json-encoded string.
18
 *
19
 * @package Stratadox\Collection
20
 * @author  Stratadox
21
 */
22
trait JsonSerializing
23
{
24
    /**
25
     * @see JsonSerializable::json()
26
     * @return string
27
     * @throws ConversionFailed
28
     */
29
    public function json(): string
30
    {
31
        try {
32
            return json_encode($this->items());
33
        } catch (Throwable $exception) {
34
            /** @var Collection $this */
35
            throw CouldNotConvertToJson::encountered($this, $exception);
36
        }
37
    }
38
39
    /** @see Collection::items() */
40
    abstract public function items(): array;
41
}
42