Builder   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 71
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A newInstance() 0 38 5
1
<?php
2
/**
3
 *
4
 * This file is part of the Aura project for PHP.
5
 *
6
 * @package Aura.Marshal
7
 *
8
 * @license https://opensource.org/licenses/mit-license.php MIT
9
 *
10
 */
11
namespace Aura\Marshal\Type;
12
13
use Aura\Marshal\Collection\Builder as CollectionBuilder;
14
use Aura\Marshal\Exception;
15
use Aura\Marshal\Entity\Builder as EntityBuilder;
16
use Aura\Marshal\Lazy\Builder as LazyBuilder;
17
18
/**
19
 *
20
 * Builds a type object from an array of description information.
21
 *
22
 * @package Aura.Marshal
23
 *
24
 */
25
class Builder
26
{
27
    /**
28
     *
29
     * The class to use for new instances.
30
     *
31
     * @var class-string<GenericType>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<GenericType> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<GenericType>.
Loading history...
32
     *
33
     */
34
    protected $class = 'Aura\Marshal\Type\GenericType';
35
36
    /**
37
     *
38
     * Returns a new type instance.
39
     *
40
     * The `$info` array should have four keys:
41
     *
42
     * - `'identity_field'` (string): The name of the identity field for
43
     *   entities of this type. This key is required.
44
     *
45
     * - `entity_builder` (Entity\BuilderInterface): A builder to create
46
     *   entity objects for the type. This key is optional, and defaults to a
47
     *   new Entity\Builder object.
48
     *
49
     * - `collection_builder` (Collection\BuilderInterface): A
50
     *   A builder to create collection objects for the type. This key
51
     *   is optional, and defaults to a new Collection\Builder object.
52
     *
53
     * @param array<string, mixed> $info An array of information about the type.
54
     *
55
     * @return GenericType
56
     *
57
     */
58
    public function newInstance($info)
59
    {
60
        $base = [
61
            'identity_field'        => null,
62
            'index_fields'          => [],
63
            'entity_builder'        => null,
64
            'collection_builder'    => null,
65
            'lazy_builder'          => null,
66
        ];
67
68
        $info = array_merge($base, $info);
69
70
        if (! $info['identity_field']) {
71
            throw new Exception('No identity field specified.');
72
        }
73
74
        if (! $info['entity_builder']) {
75
            $info['entity_builder'] = new EntityBuilder;
76
        }
77
78
        if (! $info['collection_builder']) {
79
            $info['collection_builder'] = new CollectionBuilder;
80
        }
81
82
        if (! $info['lazy_builder']) {
83
            $info['lazy_builder'] = new LazyBuilder;
84
        }
85
86
        $class = $this->class;
87
        $type = new $class;
88
89
        $type->setIdentityField($info['identity_field']);
90
        $type->setIndexFields($info['index_fields']);
91
        $type->setEntityBuilder($info['entity_builder']);
92
        $type->setCollectionBuilder($info['collection_builder']);
93
        $type->setLazyBuilder($info['lazy_builder']);
94
95
        return $type;
96
    }
97
}
98