Passed
Push — 1.0.x ( 8353d5...3a2c37 )
by Julien
21:28
created

Distinct::initializeDistinct()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 21
ccs 0
cts 12
cp 0
crap 30
rs 9.6111
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Controller\Traits\Query;
13
14
use Phalcon\Filter\Filter;
15
use Phalcon\Support\Collection;
16
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractParams;
17
use Zemit\Mvc\Controller\Traits\Abstracts\Query\AbstractDistinct;
18
19
trait Distinct
20
{
21
    use AbstractDistinct;
22
    
23
    use AbstractParams;
24
    
25
    protected ?Collection $distinct;
26
    
27
    /**
28
     * Initialize the distinct parameter for the query.
29
     *
30
     * @return void
31
     * @throws \Phalcon\Filter\Exception If an error occurs during filtering
32
     */
33
    public function initializeDistinct(): void
34
    {
35
        $distinct = $this->getParam('distinct', [Filter::FILTER_STRING, Filter::FILTER_TRIM]);
36
        
37
        if (!isset($distinct)) {
38
            $this->setDistinct(null);
39
            return;
40
        }
41
        
42
        if (!is_array($distinct)) {
43
            $distinct = explode(',', $distinct);
44
        }
45
        
46
        foreach ($distinct as $key => $item) {
47
            if (is_int($key)) {
48
                $distinct[trim($item)] = true;
49
            }
50
            unset($distinct[$key]);
51
        }
52
        
53
        $this->setDistinct(new Collection($distinct, false));
54
    }
55
    
56
    /**
57
     * Sets the distinct collection.
58
     *
59
     * @param Collection|null $distinct The distinct collection to set.
60
     *
61
     * @return void
62
     */
63
    public function setDistinct(?Collection $distinct): void
64
    {
65
        $this->distinct = $distinct;
66
    }
67
    
68
    /**
69
     * Gets the distinct collection.
70
     *
71
     * @return Collection|null The distinct collection, if set; otherwise, null.
72
     */
73
    public function getDistinct(): ?Collection
74
    {
75
        return $this->distinct;
76
    }
77
}
78