Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Types
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
7 / 7
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 registry
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 schemaOf
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 typeOf
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isNode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 clearCache
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 Cosray\Node;
6
7use Cosray\Node\Schema\Registry;
8
9class Types
10{
11    /** @var array<class-string, Schema> */
12    private array $cache = [];
13
14    private readonly Registry $registry;
15
16    public function __construct(?Registry $registry = null)
17    {
18        $this->registry = $registry ?? Registry::withDefaults();
19    }
20
21    public function registry(): Registry
22    {
23        return $this->registry;
24    }
25
26    /**
27     * @param class-string $class
28     */
29    public function schemaOf(string $class): Schema
30    {
31        return $this->cache[$class] ??= new Schema($class, $this->registry);
32    }
33
34    /**
35     * @param class-string $class
36     */
37    public function typeOf(string $class): Type
38    {
39        return new Type($class, $this);
40    }
41
42    /**
43     * @param class-string $class
44     */
45    public function get(string $class, string $key, mixed $default = null): mixed
46    {
47        return $this->schemaOf($class)->get($key, $default);
48    }
49
50    /**
51     * @param class-string $class
52     */
53    public function isNode(string $class): bool
54    {
55        return class_exists($class);
56    }
57
58    public function clearCache(): void
59    {
60        $this->cache = [];
61    }
62}