Passed
Pull Request — master (#241)
by Arman
03:23
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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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.6
13
 */
14
15
namespace Quantum\Libraries\Database\Adapters\Sleekdb\Statements;
16
17
use Quantum\Libraries\Database\Exceptions\DatabaseException;
18
use Quantum\Libraries\Database\Contracts\DbalInterface;
19
use SleekDB\Exceptions\InvalidConfigurationException;
20
use SleekDB\Exceptions\InvalidArgumentException;
21
use Quantum\Model\Exceptions\ModelException;
22
use SleekDB\Exceptions\IOException;
23
24
/**
25
 * Trait Result
26
 * @package Quantum\Libraries\Database
27
 */
28
trait Result
29
{
30
    /**
31
     * @inheritDoc
32
     * @return array
33
     * @throws DatabaseException
34
     * @throws ModelException
35
     * @throws IOException
36
     * @throws InvalidArgumentException
37
     * @throws InvalidConfigurationException
38
     */
39
    public function get(): array
40
    {
41
        return array_map(function ($element) {
42
            $item = clone $this;
43
            $item->data = $element;
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...
44
            $item->modifiedFields = $element;
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...
45
            $item->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...
46
            return $item;
47
        }, $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

47
        }, $this->/** @scrutinizer ignore-call */ getBuilder()->getQuery()->fetch());
Loading history...
48
    }
49
50
    /**
51
     * @inheritDoc
52
     * @return DbalInterface
53
     * @throws DatabaseException
54
     * @throws IOException
55
     * @throws InvalidArgumentException
56
     * @throws InvalidConfigurationException
57
     */
58
    public function findOne(int $id): DbalInterface
59
    {
60
        $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

60
        $result = $this->/** @scrutinizer ignore-call */ getOrmModel()->findById($id);
Loading history...
61
62
        $this->updateOrmModel($result);
0 ignored issues
show
Bug introduced by
It seems like updateOrmModel() 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

62
        $this->/** @scrutinizer ignore-call */ 
63
               updateOrmModel($result);
Loading history...
63
64
        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\Contracts\DbalInterface.
Loading history...
65
    }
66
67
    /**
68
     * @inheritDoc
69
     * @throws DatabaseException
70
     * @throws IOException
71
     * @throws InvalidArgumentException
72
     * @throws InvalidConfigurationException
73
     */
74
    public function findOneBy(string $column, $value): DbalInterface
75
    {
76
        $result = $this->getOrmModel()->findOneBy([$column, '=', $value]);
77
78
        $this->updateOrmModel($result);
79
80
        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\Contracts\DbalInterface.
Loading history...
81
    }
82
83
    /**
84
     * @inheritDoc
85
     * @throws DatabaseException
86
     * @throws ModelException
87
     * @throws IOException
88
     * @throws InvalidArgumentException
89
     * @throws InvalidConfigurationException
90
     */
91
    public function first(): DbalInterface
92
    {
93
        $result = $this->getBuilder()->getQuery()->first();
94
95
        $this->updateOrmModel($result);
96
97
        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\Contracts\DbalInterface.
Loading history...
98
    }
99
100
    /**
101
     * @inheritDoc
102
     * @throws DatabaseException
103
     * @throws ModelException
104
     * @throws IOException
105
     * @throws InvalidArgumentException
106
     * @throws InvalidConfigurationException
107
     */
108
    public function count(): int
109
    {
110
        return count($this->getBuilder()->getQuery()->fetch());
111
    }
112
113
    /**
114
     * @inheritDoc
115
     */
116
    public function asArray(): array
117
    {
118
        $result = $this->data ?: [];
119
120
        if (count($this->hidden) > 0 && count($result)) {
121
            $result = $this->setHidden($result);
122
        }
123
124
        return $result;
125
    }
126
127
    /**
128
     * @param $result
129
     * @return array
130
     */
131
    public function setHidden($result): array
132
    {
133
        return array_diff_key($result, array_flip($this->hidden));
134
    }
135
}