Completed
Push — master ( 85f2e1...d1e37b )
by Sebastian
04:23
created

Fragment::getGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.4285
cc 1
eloc 10
nc 1
nop 2
1
<?php
2
3
namespace App\Models;
4
5
use App\Foundation\Models\Base\TranslatableEloquent;
6
use App\Foundation\Models\Traits\Presentable;
7
8
class Fragment extends TranslatableEloquent
9
{
10
    use Presentable;
11
12
    protected $with = ['translations'];
13
14
    public $translatedAttributes = ['text'];
15
16
    /**
17
     * @return \App\Models\Fragment|null
18
     */
19
    public static function findByName(string $name)
20
    {
21
        return app('cache')->rememberForever("fragment.findByName.{$name}", function () use ($name) {
22
            return static::where('name', $name)->first();
23
        });
24
    }
25
26
    public static function getGroup(string $group, string $locale) : array
27
    {
28
        return static::query()
29
            ->where('name', 'LIKE', "{$group}.%")
30
            ->get()
31
            ->map(function (Fragment $fragment) use ($locale, $group) {
32
                return [
33
                    'key' => preg_replace("/{$group}\\./", '', $fragment->name, 1),
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<App\Models\Fragment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
34
                    'text' => $fragment->translate($locale)->text,
35
                ];
36
            })
37
            ->pluck('text', 'key')
38
            ->toArray();
39
    }
40
}
41