1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of GitterBot package. |
4
|
|
|
* |
5
|
|
|
* @author Serafim <[email protected]> |
6
|
|
|
* @date 09.10.2015 16:59 |
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 Core\Mappers; |
12
|
|
|
|
13
|
|
|
use Domains\Room; |
14
|
|
|
use Domains\User; |
15
|
|
|
use Domains\Message; |
16
|
|
|
use Carbon\Carbon; |
17
|
|
|
use Interfaces\Gitter\Support\AttributeMapper; |
18
|
|
|
use Illuminate\Database\Eloquent\Model; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class MessageMapperTrait |
22
|
|
|
*/ |
23
|
|
|
trait MessageMapperTrait |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param array $attributes |
27
|
|
|
* @return Message |
28
|
|
|
* @throws \InvalidArgumentException |
29
|
|
|
*/ |
30
|
|
|
public static function fromGitterObject(array $attributes) |
31
|
|
|
{ |
32
|
|
|
$fields = ['gitter_id', 'text', 'html', 'edited', 'user', 'unread', |
33
|
|
|
'read_by', 'urls', 'mentions', 'issues', 'meta', 'created_at', 'updated_at', 'room_id']; |
34
|
|
|
|
35
|
|
|
$values = (new AttributeMapper($attributes)) |
36
|
|
|
->rename('readBy', 'read_by') |
37
|
|
|
->rename('id', 'gitter_id') |
38
|
|
|
->value('editedAt', function ($val) { |
39
|
|
|
return !!$val; |
40
|
|
|
}, 'edited') |
41
|
|
|
->value('fromUser', function ($user) { |
42
|
|
|
return User::fromGitterObject($user); |
43
|
|
|
}, 'user') |
44
|
|
|
->value('sent', function ($date) { |
45
|
|
|
return (new Carbon($date))->setTimezone('Europe/Moscow'); |
46
|
|
|
}, 'created_at') |
47
|
|
|
->value('editedAt', function ($date) { |
48
|
|
|
return (new Carbon($date))->setTimezone('Europe/Moscow'); |
49
|
|
|
}, 'updated_at') |
50
|
|
|
->value('mentions', function ($mentions) { |
51
|
|
|
return static::parseMentions($mentions); |
52
|
|
|
}) |
53
|
|
|
->only($fields) |
54
|
|
|
->toArray(); |
55
|
|
|
|
56
|
|
|
if (!array_key_exists('room_id', $values)) { |
57
|
|
|
$values['room_id'] = \App::make(Room::class)->id; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return static::unguarded(function () use ($values) { |
61
|
|
|
return new static($values); |
|
|
|
|
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param array $inputMentions |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
protected static function parseMentions(array $inputMentions) |
70
|
|
|
{ |
71
|
|
|
$ids = []; |
72
|
|
|
|
73
|
|
|
$mentions = []; |
74
|
|
|
|
75
|
|
|
foreach ($inputMentions as $mention) { |
76
|
|
|
if (array_key_exists('userId', $mention)) { |
77
|
|
|
$user = User::where('gitter_id', $mention['userId'])->first(); |
78
|
|
|
|
79
|
|
|
if ($user && !in_array($user->gitter_id, $ids, false)) { |
80
|
|
|
$ids[] = $user->gitter_id; |
81
|
|
|
$mentions[] = $user; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $mentions; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.