Passed
Pull Request — master (#125)
by
unknown
02:35
created

Result::setHidden()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.0
13
 */
14
15
namespace Quantum\Libraries\Database\Sleekdb\Statements;
16
17
use Quantum\Libraries\Database\DbalInterface;
18
19
/**
20
 * Trait Result
21
 * @package Quantum\Libraries\Database\Sleekdb\Statements
22
 */
23
trait Result
24
{
25
    /**
26
     * @inheritDoc
27
     * @return array
28
     * @throws \Quantum\Exceptions\DatabaseException
29
     * @throws \Quantum\Exceptions\ModelException
30
     * @throws \SleekDB\Exceptions\IOException
31
     * @throws \SleekDB\Exceptions\InvalidArgumentException
32
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
33
     */
34
    public function get(): array
35
    {
36
        return $this->getBuilder()->getQuery()->fetch();
0 ignored issues
show
Bug introduced by
It seems like getBuilder() 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

36
        return $this->/** @scrutinizer ignore-call */ getBuilder()->getQuery()->fetch();
Loading history...
37
    }
38
39
    /**
40
     * @inheritDoc
41
     * @return DbalInterface
42
     * @throws \Quantum\Exceptions\DatabaseException
43
     * @throws \SleekDB\Exceptions\IOException
44
     * @throws \SleekDB\Exceptions\InvalidArgumentException
45
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
46
     */
47
    public function findOne(int $id): DbalInterface
48
    {
49
        $result = $this->getOrmModel()->findById($id);
0 ignored issues
show
Bug introduced by
It seems like getOrmModel() 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

49
        $result = $this->/** @scrutinizer ignore-call */ getOrmModel()->findById($id);
Loading history...
50
51
        $this->data = $result;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
52
        $this->modifiedFields = $result;
0 ignored issues
show
Bug Best Practice introduced by
The property modifiedFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
        $this->isNew = false;
0 ignored issues
show
Bug Best Practice introduced by
The property isNew does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
54
55
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...eekdb\Statements\Result which is incompatible with the type-hinted return Quantum\Libraries\Database\DbalInterface.
Loading history...
56
    }
57
58
    /**
59
     * @inheritDoc
60
     * @throws \Quantum\Exceptions\DatabaseException
61
     * @throws \SleekDB\Exceptions\IOException
62
     * @throws \SleekDB\Exceptions\InvalidArgumentException
63
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
64
     */
65
    public function findOneBy(string $column, $value): DbalInterface
66
    {
67
        $result = $this->getOrmModel()->findOneBy([$column, '=', $value]);
68
69
        $this->data = $result;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
        $this->modifiedFields = $result;
0 ignored issues
show
Bug Best Practice introduced by
The property modifiedFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
71
        $this->isNew = false;
0 ignored issues
show
Bug Best Practice introduced by
The property isNew does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
72
73
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...eekdb\Statements\Result which is incompatible with the type-hinted return Quantum\Libraries\Database\DbalInterface.
Loading history...
74
    }
75
76
    /**
77
     * @inheritDoc
78
     * @throws \Quantum\Exceptions\DatabaseException
79
     * @throws \Quantum\Exceptions\ModelException
80
     * @throws \SleekDB\Exceptions\IOException
81
     * @throws \SleekDB\Exceptions\InvalidArgumentException
82
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
83
     */
84
    public function first(): DbalInterface
85
    {
86
        $result = $this->getBuilder()->getQuery()->first();
87
88
        $this->data = $result;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
89
        $this->modifiedFields = $result;
0 ignored issues
show
Bug Best Practice introduced by
The property modifiedFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
90
        $this->isNew = false;
0 ignored issues
show
Bug Best Practice introduced by
The property isNew does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
91
92
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...eekdb\Statements\Result which is incompatible with the type-hinted return Quantum\Libraries\Database\DbalInterface.
Loading history...
93
    }
94
95
    /**
96
     * @inheritDoc
97
     * @throws \Quantum\Exceptions\DatabaseException
98
     * @throws \Quantum\Exceptions\ModelException
99
     * @throws \SleekDB\Exceptions\IOException
100
     * @throws \SleekDB\Exceptions\InvalidArgumentException
101
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
102
     */
103
    public function count(): int
104
    {
105
        return count($this->getBuilder()->getQuery()->fetch());
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function asArray(): array
112
    {
113
        $result = $this->data ?: [];
114
115
        if (count($this->hidden) > 0 && count($result)) {
116
            $result = $this->setHidden($result);
117
        }
118
119
        return $result;
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function setHidden($result)
126
    {
127
        return array_diff_key($result, array_flip($this->hidden));
128
    }
129
}
130