Setter::setLoadedAllProperties()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
/*
3
 * This file is part of the Scrawler package.
4
 *
5
 * (c) Pranjal Pandey <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Scrawler\Arca\Traits\Model;
12
13
use Doctrine\DBAL\Types\Type;
14
use Scrawler\Arca\Model;
15
16
/**
17
 * Setter trait to provide setter methods to the model.
18
 */
19
trait Setter
20
{
21
    /**
22
     * Set all properties of model via array.
23
     *
24
     * @param array<mixed> $properties
25
     */
26
    public function setProperties(array $properties): Model
27
    {
28
        foreach ($properties as $key => $value) {
29
            $this->set($key, $value);
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
            $this->/** @scrutinizer ignore-call */ 
30
                   set($key, $value);
Loading history...
30
        }
31
32
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Scrawler\Arca\Traits\Model\Setter which includes types incompatible with the type-hinted return Scrawler\Arca\Model.
Loading history...
33
    }
34
35
    /**
36
     * Set all properties of model loaded via database.
37
     *
38
     * @param array<mixed> $properties
39
     */
40
    public function setLoadedProperties(array $properties): Model
41
    {
42
        $this->__properties['self'] = $properties;
0 ignored issues
show
Bug Best Practice introduced by
The property __properties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        $this->setLoadedAllProperties($properties);
44
        foreach (array_keys($properties) as $key) {
45
            $this->__properties['type'][$key] = $this->tableManager->getTable($this->table)->getColumn($key)->getComment();
46
        }
47
        $this->__meta['id'] = $properties['id'];
0 ignored issues
show
Bug Best Practice introduced by
The property __meta does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
48
49
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Scrawler\Arca\Traits\Model\Setter which includes types incompatible with the type-hinted return Scrawler\Arca\Model.
Loading history...
50
    }
51
52
    /**
53
     * get php value from database value.
54
     *
55
     * @param array<mixed> $properties
56
     */
57
    private function setLoadedAllProperties(array $properties): void
58
    {
59
        foreach ($properties as $key => $value) {
60
            $type = Type::getType($this->tableManager->getTable($this->table)->getColumn($key)->getComment());
61
            $this->__properties['all'][$key] = $type->convertToPHPValue($value, $this->connection->getDatabasePlatform());
0 ignored issues
show
Bug Best Practice introduced by
The property __properties does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
62
        }
63
    }
64
65
    /**
66
     * call when model is loaded from database.
67
     */
68
    public function setLoaded(): Model
69
    {
70
        $this->__meta['is_loaded'] = true;
0 ignored issues
show
Bug Best Practice introduced by
The property __meta does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
71
        $this->__meta['id_error'] = false;
72
73
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Scrawler\Arca\Traits\Model\Setter which includes types incompatible with the type-hinted return Scrawler\Arca\Model.
Loading history...
74
    }
75
}
76