Completed
Push — master ( e03c3a...46c0e5 )
by Vladimir
01:43
created

src/data/SearchResult.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 * @link https://github.com/Vintage-web-production/yii2-search
4
 * @copyright Copyright (c) 2017 Vintage Web Production
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace vintage\search\data;
9
10
use yii\base\Object;
11
12
/**
13
 * Model for store of search result.
14
 *
15
 * @property string $title
16
 * @property string $description
17
 * @property string $url
18
 *
19
 * @author Vladimir Kuprienko <[email protected]>
20
 * @since 1.0
21
 */
22
class SearchResult extends Object
23
{
24
    /**
25
     * @var string
26
     */
27
    public $title;
28
    /**
29
     * @var string
30
     */
31
    public $description;
32
    /**
33
     * @var string
34
     */
35
    public $url;
36
    /**
37
     * @var integer
38
     */
39
    public $modelId;
40
    /**
41
     * @var string
42
     */
43
    public $modelName;
44
45
46
    /**
47
     * Building the result from Active Record object.
48
     *
49
     * @param \yii\db\BaseActiveRecord|\vintage\search\interfaces\SearchInterface $modelObject
50
     * @return SearchResult
51
     */
52
    public static function build($modelObject)
53
    {
54
        return new self([
55
            'modelId'       => $modelObject->getPrimaryKey(),
0 ignored issues
show
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

55
            'modelId'       => $modelObject->/** @scrutinizer ignore-call */ getPrimaryKey(),
Loading history...
56
            'modelName'     => $modelObject::className(),
0 ignored issues
show
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

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