Completed
Branch feature/pre-split (0400ca)
by Anton
03:49
created

RecordInstantiator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * components
4
 *
5
 * @author    Wolfy-J
6
 */
7
namespace Spiral\ORM\Entities;
8
9
use Spiral\ORM\Exceptions\InstantionException;
10
use Spiral\ORM\InstantiatorInterface;
11
use Spiral\ORM\ORMInterface;
12
use Spiral\ORM\Record;
13
14
/**
15
 * Default instantiator for records.
16
 */
17
class RecordInstantiator implements InstantiatorInterface
18
{
19
    /**
20
     * @invisible
21
     * @var ORMInterface
22
     */
23
    private $orm;
24
25
    /**
26
     * Record class.
27
     *
28
     * @var string
29
     */
30
    private $class = '';
31
32
    /**
33
     * Normalized schema delivered by RecordSchema.
34
     *
35
     * @var array
36
     */
37
    private $schema = [];
38
39
    /**
40
     * @param ORMInterface $odm
41
     * @param string       $class
42
     * @param array        $schema
43
     */
44
    public function __construct(ORMInterface $odm, string $class, array $schema)
45
    {
46
        $this->orm = $odm;
47
        $this->class = $class;
48
        $this->schema = $schema;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     *
54
     * @return Record
55
     *
56
     * @throws InstantionException
57
     */
58
    public function instantiate($fields, bool $filter = true): Record
59
    {
60
        if (!is_array($fields)) {
61
            $fields = $this->normalizeFields($fields);
62
        }
63
64
        $class = $this->class;
65
66
        //Now we can construct needed class, in this case we are following DocumentEntity declaration
67
        if (!$filter) {
68
            //No need to filter values, passing directly in constructor
69
            return new $class($fields, $this->schema, $this->orm);
70
        }
71
72
        $entity = new $class($fields, $this->schema, $this->orm);
73
        if (!$entity instanceof Record) {
74
            throw new InstantionException(
75
                "Unable to set filtered values for {$class}, must be instance of Record"
76
            );
77
        }
78
79
        //Must pass value thought all needed filters
80
        $entity->stateValue($fields);
81
82
        return $entity;
83
    }
84
85
    /**
86
     * @param \Traversable|\ArrayAccess $fields
87
     *
88
     * @return array
89
     */
90 View Code Duplication
    private function normalizeFields($fields): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $result = [];
93
        if (!is_scalar($fields)) {
94
            //Trying to iterate over
95
            foreach ($fields as $name => $value) {
96
                $result[$name] = $value;
97
            }
98
        }
99
100
        return $result;
101
    }
102
}