Completed
Push — master ( a1f3ed...74c577 )
by Kirill
02:49
created

AbstractAchieve::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6666
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 11.10.2015 6:09
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Interfaces\Gitter\Achieve;
12
13
use Domains\User;
14
use Domains\Achieve;
15
use Carbon\Carbon;
16
use Illuminate\Contracts\Support\Jsonable;
17
use Illuminate\Contracts\Support\Arrayable;
18
use Interfaces\Gitter\Subscriber\SubscriberInterface;
19
20
/**
21
 * Class AbstractAchieve
22
 */
23
abstract class AbstractAchieve implements
24
    AchieveInterface,
25
    SubscriberInterface,
26
    Arrayable,
27
    Jsonable
28
{
29
    /**
30
     * Achieve title
31
     * @var string
32
     */
33
    public $title = 'undefined';
34
35
    /**
36
     * Achieve description
37
     * @var string
38
     */
39
    public $description = 'undefined';
40
41
    /**
42
     * Achieve image link
43
     * @var string
44
     */
45
    public $image = '/img/achievements/karma-10.gif';
46
47
    /**
48
     * @var null
49
     */
50
    public $name = null;
51
52
    /**
53
     * @var array
54
     */
55
    protected $properties = [];
56
57
    /**
58
     * @constructor
59
     */
60
    public function __construct()
61
    {
62
        $this->name = static::class;
63
    }
64
65
    /**
66
     * @param User $user
67
     * @param Carbon|null $createdAt
68
     * @return Achieve
69
     * @throws \LogicException
70
     */
71
    public function create(User $user, Carbon $createdAt = null): Achieve
72
    {
73
        $achieve = Achieve::create([
74
            'name'       => $this->name,
75
            'user_id'    => $user->id,
76
            'created_at' => $createdAt ?: Carbon::now(),
77
        ]);
78
79
        return $achieve;
80
    }
81
82
    /**
83
     * @param $key
84
     * @param $value
85
     */
86
    public function __set($key, $value)
87
    {
88
        $this->properties[$key] = $value;
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function toArray(): array
95
    {
96
        return array_merge([
97
            'name'        => $this->name,
98
            'title'       => $this->title,
99
            'description' => $this->description,
100
            'image'       => $this->image,
101
        ], $this->properties);
102
    }
103
104
    /**
105
     * @param int $options
106
     * @return string
107
     */
108
    public function toJson($options = 0): string
109
    {
110
        return json_encode($this->toArray(), $options);
111
    }
112
}