Passed
Push — master ( 8f6ac5...4c4a52 )
by Vladimir
02:11
created

SearchResult::build()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the yii2-search package.
5
 *
6
 * (c) Vladimir Kuprienko <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace vintage\search\models;
13
14
use yii\base\BaseObject;
15
16
/**
17
 * Model for store of search result.
18
 *
19
 * @property string $title
20
 * @property string $description
21
 * @property string $url
22
 *
23
 * @author Vladimir Kuprienko <[email protected]>
24
 * @since 1.0
25
 */
26
class SearchResult extends BaseObject
27
{
28
    /**
29
     * @var string
30
     */
31
    public $title;
32
    /**
33
     * @var string
34
     */
35
    public $description;
36
    /**
37
     * @var string
38
     */
39
    public $url;
40
    /**
41
     * @var integer
42
     */
43
    public $modelId;
44
    /**
45
     * @var string
46
     */
47
    public $modelName;
48
49
50
    /**
51
     * Building the result from Active Record object.
52
     *
53
     * @param \yii\db\BaseActiveRecord|\vintage\search\interfaces\SearchInterface $modelObject
54
     *
55
     * @return SearchResult
56
     */
57
    public static function build($modelObject)
58
    {
59
        return new self([
60
            'modelId' => $modelObject->getPrimaryKey(),
0 ignored issues
show
Bug introduced by
The method getPrimaryKey() does not exist on vintage\search\interfaces\SearchInterface. It seems like you code against a sub-type of said class. However, the method does not exist in vintage\search\interfaces\CustomSearchInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
            'modelId' => $modelObject->/** @scrutinizer ignore-call */ getPrimaryKey(),
Loading history...
61
            'modelName' => $modelObject::className(),
0 ignored issues
show
Bug introduced by
The method className() does not exist on vintage\search\interfaces\SearchInterface. It seems like you code against a sub-type of said class. However, the method does not exist in vintage\search\interfaces\CustomSearchInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            'modelName' => $modelObject::/** @scrutinizer ignore-call */ className(),
Loading history...
62
            'title' => $modelObject->getSearchTitle(),
63
            'description' => $modelObject->getSearchDescription(),
64
            'url' => $modelObject->getSearchUrl(),
65
        ]);
66
    }
67
68
    /**
69
     * Multiply building of result from Active Record objects.
70
     *
71
     * @param \yii\db\BaseActiveRecord[]|\vintage\search\interfaces\SearchInterface[] $modelObjects
72
     *
73
     * @return SearchResult[]
74
     */
75
    public static function buildMultiply($modelObjects)
76
    {
77
        $results = [];
78
79
        foreach ($modelObjects as $object) {
80
            $results[] = static::build($object);
81
        }
82
83
        return $results;
84
    }
85
86
    /**
87
     * Method for sorting results of search by model name.
88
     *
89
     * @param SearchResult[] $searchResults
90
     *
91
     * @return SearchResult[]
92
     */
93
    public static function sortByModel(array $searchResults)
94
    {
95
        $sorted = [];
96
97
        foreach ($searchResults as $obj) {
98
            $sorted[$obj->modelName][] = $obj;
99
        }
100
101
        return $sorted;
102
    }
103
}
104