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\Services\DataProviders; |
10
|
|
|
|
11
|
|
|
use App\Models\Article; |
12
|
|
|
use Carbon\Carbon; |
13
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ExternalArticle. |
17
|
|
|
*/ |
18
|
|
|
class ExternalArticle implements Arrayable |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $title; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $body; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $url; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \DateTime |
37
|
|
|
*/ |
38
|
|
|
private $createdAt; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array|string[] |
42
|
|
|
*/ |
43
|
|
|
private $images = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* ExternalArticle constructor. |
47
|
|
|
* @param string $title |
48
|
|
|
* @param string $body |
49
|
|
|
* @param string $url |
50
|
|
|
*/ |
51
|
|
|
public function __construct(string $title, string $body, string $url) |
52
|
|
|
{ |
53
|
|
|
$this->title = $title; |
54
|
|
|
$this->body = $body; |
55
|
|
|
$this->url = $url; |
56
|
|
|
|
57
|
|
|
$this->createdAt = Carbon::now(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $image |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function addImageUrl(string $image) |
65
|
|
|
{ |
66
|
|
|
$this->images[] = $image; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function getImages(): array |
75
|
|
|
{ |
76
|
|
|
return []; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getTitle(): string |
83
|
|
|
{ |
84
|
|
|
return $this->title; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getBody(): string |
91
|
|
|
{ |
92
|
|
|
return $this->body; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getUrl(): string |
99
|
|
|
{ |
100
|
|
|
return $this->url; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return \DateTime |
105
|
|
|
*/ |
106
|
|
|
public function getCreatedAt(): \DateTime |
107
|
|
|
{ |
108
|
|
|
return $this->createdAt; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param \DateTime $createdAt |
113
|
|
|
* @return $this|ExternalArticle |
114
|
|
|
*/ |
115
|
|
|
public function setCreatedAt(\DateTime $createdAt) |
116
|
|
|
{ |
117
|
|
|
$this->createdAt = $createdAt; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function toArray(): array |
126
|
|
|
{ |
127
|
|
|
return [ |
128
|
|
|
'title' => $this->title, |
129
|
|
|
'url' => $this->url, |
130
|
|
|
'images' => $this->images, |
131
|
|
|
'content' => $this->body, |
132
|
|
|
'created_at' => $this->createdAt, |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Article $article |
138
|
|
|
* @return Article |
139
|
|
|
*/ |
140
|
|
|
public function fill(Article $article): Article |
141
|
|
|
{ |
142
|
|
|
$article->title = $this->title; |
|
|
|
|
143
|
|
|
//$article- |
144
|
|
|
$article->image = reset($this->images); |
|
|
|
|
145
|
|
|
|
146
|
|
|
$article->content_source = ''; |
|
|
|
|
147
|
|
|
$article->content_rendered = $this->body; |
|
|
|
|
148
|
|
|
|
149
|
|
|
$article->published_at = $this->createdAt; |
|
|
|
|
150
|
|
|
|
151
|
|
|
$article->status = Article\Status::PUBLISHED; |
|
|
|
|
152
|
|
|
|
153
|
|
|
return $article; |
154
|
|
|
} |
155
|
|
|
} |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write 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.