Searchable::toArray()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace leinonen\Yii2Algolia\ActiveRecord;
4
5
use Yii;
6
use leinonen\Yii2Algolia\AlgoliaManager;
7
8
/**
9
 * Simple trait that implements methods needed for the SearchableInterface + couple helpers for dealing with ActiveRecords.
10
 */
11
trait Searchable
12
{
13
    /**
14
     * @see \yii\base\ArrayableTrait::toArray()
15
     *
16
     * @param array $fields
17
     * @param array $expand
18
     * @param bool $recursive
19
     */
20
    abstract public function toArray(array $fields = [], array $expand = [], $recursive = true);
21
22
    /**
23
     * @see \yii\db\BaseActiveRecord::getPrimaryKey()
24
     *
25
     * @param bool $asArray
26
     */
27
    abstract public function getPrimaryKey($asArray = false);
28
29
    /**
30
     * An array of indices that this model uses. If none specified
31
     * The name of the model will be used as the index.
32
     *
33
     * @return array
34
     */
35 5
    public function indices()
36
    {
37 5
        return [];
38
    }
39
40
    /**
41
     * Returns an array of indices for this model.
42
     *
43
     * @return array
44
     */
45 6
    public function getIndices()
46
    {
47 6
        $indices = $this->indices();
48
49 6
        if (empty($indices)) {
50 5
            $className = (new \ReflectionClass($this))->getShortName();
51
52 5
            return [$className];
53
        }
54
55 1
        return $indices;
56
    }
57
58
    /**
59
     * Returns the model in algolia friendly array form.
60
     *
61
     * @return array
62
     */
63 6
    public function getAlgoliaRecord()
64
    {
65 6
        return $this->toArray();
66
    }
67 6
68
    /**
69
     * Returns an unique identifier for the Model.
70
     *
71
     * @return int
72
     */
73
    public function getObjectID()
74
    {
75 5
        return $this->getPrimaryKey();
76
    }
77 5
78
    /**
79
     * @see AlgoliaManager::pushToIndices()
80
     *
81
     * @return array
82
     */
83
    public function index()
84
    {
85 1
        $manager = static::getAlgoliaManager();
86
87 1
        return $manager->pushToIndices($this);
88
    }
89 1
90
    /**
91
     * @see AlgoliaManager::removeFromIndices()
92
     *
93
     * @return array
94
     */
95
    public function removeFromIndices()
96
    {
97 1
        $manager = static::getAlgoliaManager();
98
99 1
        return $manager->removeFromIndices($this);
100
    }
101 1
102
    /**
103
     * @see AlgoliaManager::updateInIndices()
104
     *
105
     * @return array
106
     */
107
    public function updateInIndices()
108
    {
109 1
        $manager = static::getAlgoliaManager();
110
111 1
        return $manager->updateInIndices($this);
112
    }
113 1
114
    /**
115
     * @see AlgoliaManager::reindex()
116
     *
117
     * @return array
118
     */
119
    public static function reindex()
120
    {
121 1
        $manager = static::getAlgoliaManager();
122
123 1
        return $manager->reindex(__CLASS__);
124
    }
125 1
126
    /**
127
     * @see AlgoliaManager::clearIndices()
128
     *
129
     * @return array
130
     */
131
    public static function clearIndices()
132
    {
133 1
        $manager = static::getAlgoliaManager();
134
135 1
        return $manager->clearIndices(__CLASS__);
136
    }
137 1
138
    /**
139
     * @see AlgoliaManager::search()
140
     *
141
     * @param string $query
142
     * @param null|array $searchParameters
143
     *
144
     * @return array
145
     */
146
    public static function search($query, array $searchParameters = null)
147
    {
148 2
        $manager = static::getAlgoliaManager();
149
150 2
        return $manager->search(__CLASS__, $query, $searchParameters);
151
    }
152 2
153
    /**
154
     * Returns the AlgoliaManager Instance.
155
     *
156
     * @internal Marked as protected only to counter the problems with mocking.
157
     *
158
     * @return AlgoliaManager
159
     */
160
    protected static function getAlgoliaManager()
161 7
    {
162
        return Yii::$container->get(AlgoliaManager::class);
163 7
    }
164
}
165