Completed
Push — feature-laravel-5.4 ( c7e673...3c5a91 )
by Kirill
92:00 queued 45:37
created

Docs::setImporterConfigAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of laravel.su package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace App\Models;
10
11
use Illuminate\Support\Str;
12
use Illuminate\Database\Eloquent\Model;
13
use Illuminate\Contracts\Support\Arrayable;
14
use Illuminate\Database\Eloquent\Relations\HasMany;
15
use Service\DocsImporter\DocsConnectionConfigInterface;
16
17
/**
18
 * Class Docs.
19
 */
20
class Docs extends Model
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $table = 'docs';
26
27
    /**
28
     * @var array
29
     */
30
    protected $casts = [
31
        'importer_config' => 'collection',
32
    ];
33
34
    /**
35
     * @return HasMany
36
     */
37
    public function pages(): HasMany
38
    {
39
        return $this->hasMany(DocsPage::class);
40
    }
41
42
    /**
43
     * @param string $title
44
     * @return string
45
     */
46
    public function getTitleAttribute(string $title): string
47
    {
48
        return Str::ucfirst($title) . ' (' . $this->version . ')';
0 ignored issues
show
Documentation introduced by
The property version does not exist on object<App\Models\Docs>. 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...
49
    }
50
51
    /**
52
     * @param DocsConnectionConfigInterface|array $config
53
     */
54
    public function setImporterConfigAttribute($config)
55
    {
56
        $this->attributes['importer_config'] = $config instanceof Arrayable
57
            ? $config->toArray()
58
            : json_encode($config);
59
    }
60
61
    /**
62
     * @param string $data
63
     * @return array
64
     */
65
    public function getImporterConfigAttribute(string $data): array
66
    {
67
        if ($data) {
68
            return json_decode($data, true);
69
        }
70
71
        return [];
72
    }
73
}
74