Completed
Push — master ( 8e13bf...38fa9e )
by Arman
18s queued 15s
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\PaginatorInterface;
18
use Quantum\Libraries\Database\Sleekdb\Paginator;
19
use Quantum\Libraries\Database\DbalInterface;
20
21
/**
22
 * Trait Result
23
 * @package Quantum\Libraries\Database\Sleekdb\Statements
24
 */
25
trait Result
26
{
27
    /**
28
     * @inheritDoc
29
     * @return array
30
     * @throws \Quantum\Exceptions\DatabaseException
31
     * @throws \Quantum\Exceptions\ModelException
32
     * @throws \SleekDB\Exceptions\IOException
33
     * @throws \SleekDB\Exceptions\InvalidArgumentException
34
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
35
     */
36
    public function get(): array
37
    {
38
        $result = array_map(function ($element) {
39
            $item = clone $this;
40
            $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...
41
            $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...
42
            $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...
43
            return $item;
44
        }, $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

44
        }, $this->/** @scrutinizer ignore-call */ getBuilder()->getQuery()->fetch());
Loading history...
45
46
        return $result;
47
    }
48
49
	  /**
50
	   * @param int $perPage
51
	   * @param int $currentPage
52
	   * @return PaginatorInterface
53
	   */
54
	  public function paginate(int $perPage, int $currentPage = 1): PaginatorInterface
55
	  {
56
		  return new Paginator($this, $perPage, $currentPage);
57
	  }
58
59
    /**
60
     * @inheritDoc
61
     * @return DbalInterface
62
     * @throws \Quantum\Exceptions\DatabaseException
63
     * @throws \SleekDB\Exceptions\IOException
64
     * @throws \SleekDB\Exceptions\InvalidArgumentException
65
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
66
     */
67
    public function findOne(int $id): DbalInterface
68
    {
69
        $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

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