Passed
Push — main ( 7078ce...216f58 )
by Pranjal
02:32 queued 13s
created

Setter::setLoadedAllProperties()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
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 4
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 Scrawler\Arca\Model;
14
use Doctrine\DBAL\Types\Type;
15
16
17
/**
18
 * Setter trait to provide setter methods to the model.
19
 */
20
trait Setter
21
{
22
    /**
23
     * Set all properties of model via array.
24
     *
25
     * @param array<mixed> $properties
26
     */
27
    public function setProperties(array $properties): Model
28
    {
29
        foreach ($properties as $key => $value) {
30
            $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

30
            $this->/** @scrutinizer ignore-call */ 
31
                   set($key, $value);
Loading history...
31
        }
32
33
        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...
34
    }
35
36
    /**
37
     * Set all properties of model loaded via database.
38
     *
39
     * @param array<mixed> $properties
40
     */
41
    public function setLoadedProperties(array $properties): Model
42
    {
43
        $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...
44
        $this->setLoadedAllProperties($properties);
45
        foreach ($properties as $key => $value) {
46
            $this->__properties['type'][$key] = $this->tableManager->getTable($this->table)->getColumn($key)->getComment();
47
        }
48
        $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...
49
50
        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...
51
    }
52
53
    /**
54
     * get php value from database value
55
     *
56
     */
57
    private function setLoadedAllProperties($properties): void{
58
        foreach ($properties as $key => $value) {
59
            $type = Type::getType($this->tableManager->getTable($this->table)->getColumn($key)->getComment());
60
            $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...
61
        }
62
    }
63
64
    /**
65
     * call when model is loaded from database.
66
     */
67
    public function setLoaded(): Model
68
    {
69
        $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...
70
        $this->__meta['id_error'] = false;
71
72
        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...
73
    }
74
}
75