Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
46.15% covered (danger)
46.15%
6 / 13
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
LazyLoadedListIdentifier
46.15% covered (danger)
46.15%
6 / 13
28.57% covered (danger)
28.57%
2 / 7
21.65
0.00% covered (danger)
0.00%
0 / 1
 __construct
57.14% covered (warning)
57.14%
4 / 7
0.00% covered (danger)
0.00%
0 / 1
3.71
 getBoundedContextId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createFrom
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromNative
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 toNative
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 asUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Core\Datalayers\ValueObjects;
3
4use Apie\Core\BoundedContext\BoundedContextId;
5use Apie\Core\Entities\EntityInterface;
6use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException;
7use Apie\Core\ValueObjects\Interfaces\ValueObjectInterface;
8use Apie\Core\ValueObjects\Utils;
9use ReflectionClass;
10use Throwable;
11
12/**
13 * @template T of EntityInterface
14 */
15final class LazyLoadedListIdentifier implements ValueObjectInterface
16{
17    private BoundedContextId $boundedContextId;
18
19    /**
20     * @var ReflectionClass<T>
21     */
22    private ReflectionClass $class;
23
24    public function __construct(string $input)
25    {
26        $split = explode(',', $input);
27        if (count($split) !== 2) {
28            throw new InvalidStringForValueObjectException($input, $this);
29        }
30        try {
31            $this->boundedContextId = new BoundedContextId($split[0]);
32            $this->class = new ReflectionClass($split[1]);
33        } catch (Throwable $throwable) {
34            throw new InvalidStringForValueObjectException($input, $this, $throwable);
35        }
36    }
37
38    public function getBoundedContextId(): BoundedContextId
39    {
40        return $this->boundedContextId;
41    }
42
43    /**
44     * @return ReflectionClass<T>
45     */
46    public function getClass(): ReflectionClass
47    {
48        return $this->class;
49    }
50
51    /**
52     * @template U of EntityInterface
53     * @param ReflectionClass<U> $class
54     * @return LazyLoadedListIdentifier<U>
55     */
56    public static function createFrom(BoundedContextId $boundedContextId, ReflectionClass $class): self
57    {
58        return new self($boundedContextId . ',' . $class->name);
59    }
60
61    /**
62     * @return LazyLoadedListIdentifier<EntityInterface>
63     */
64    public static function fromNative(mixed $input): self
65    {
66        return new self(Utils::toString($input));
67    }
68
69    public function toNative(): string
70    {
71        return $this->boundedContextId . ',' . $this->class->name;
72    }
73
74    public function asUrl(): string
75    {
76        return '/' . $this->boundedContextId . '/' . $this->class->getShortName();
77    }
78}