zend-blog-3-backend

Форк
0
/
CommentTransformer.php 
152 строки · 4.4 Кб
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: morontt
5
 * Date: 29.11.14
6
 * Time: 15:19
7
 */
8

9
namespace App\API\Transformers;
10

11
use App\Entity\Comment;
12
use App\Entity\CommentInterface;
13
use App\Entity\ViewComment;
14
use App\Entity\ViewCommentator;
15
use App\Utils\EmojiFlagSymbol;
16
use League\Fractal\Resource\ResourceInterface;
17

18
class CommentTransformer extends BaseTransformer
19
{
20
    /**
21
     * @var array
22
     */
23
    protected array $availableIncludes = [
24
        'commentator',
25
    ];
26

27
    /**
28
     * @param CommentInterface $item
29
     *
30
     * @return array
31
     */
32
    public function transform(CommentInterface $item): array
33
    {
34
        $commentatorId = null;
35
        $username = null;
36
        $email = null;
37
        $website = null;
38
        $locationCity = null;
39
        $locationRegion = null;
40
        $locationCountry = null;
41
        $imageHash = null;
42
        $countryCode = null;
43

44
        $userAgent = null;
45
        $bot = false;
46

47
        if ($item instanceof Comment) {
48
            $commentator = $item->getCommentator();
49
            if ($commentator) {
50
                $commentatorId = $commentator->getId();
51
                $username = $commentator->getName();
52
                $email = $commentator->getEmail();
53
                $website = $commentator->getWebsite();
54
            } else {
55
                $user = $item->getUser();
56
                if ($user) {
57
                    $commentatorId = ViewCommentator::USER_ID_OFFSET + $user->getId();
58
                    $username = $user->getUsername();
59
                    $email = $user->getEmail();
60
                }
61
            }
62

63
            $imageHash = $item->getAvatarHash();
64
            $location = $item->getGeoLocation();
65
            if ($location && $city = $location->getCity()) {
66
                $locationCity = $city->getCity();
67
                $locationRegion = $city->getRegion();
68
                $locationCountry = $city->getCountry()->getName();
69
                $countryCode = $city->getCountry()->getCode();
70
            }
71

72
            $trackingAgent = $item->getTrackingAgent();
73
            if ($trackingAgent) {
74
                $userAgent = $trackingAgent->getUserAgent();
75
                $bot = $trackingAgent->isBot();
76
            }
77
        } elseif ($item instanceof ViewComment) {
78
            $commentatorId = $item->getVirtualUserId();
79

80
            $username = $item->getUsername();
81
            $email = $item->getEmail();
82
            $website = $item->getWebsite();
83
            $imageHash = $item->getAvatarHash();
84

85
            $locationCity = $item->getCity();
86
            $locationRegion = $item->getRegion();
87
            $locationCountry = $item->getCountry();
88
            $countryCode = $item->getCode();
89

90
            $userAgent = $item->getUserAgent();
91
            $bot = $item->isBot();
92
        }
93

94
        $parentId = null;
95
        $parent = $item->getParent();
96
        if ($parent) {
97
            $parentId = $parent->getId();
98
        }
99

100
        try {
101
            $flag = $countryCode ? EmojiFlagSymbol::get($countryCode) : '';
102
        } catch (\Exception $e) {
103
            $flag = '';
104
        }
105

106
        return [
107
            'id' => $item->getId(),
108
            'text' => $item->getText(),
109
            'commentator' => $commentatorId,
110
            'commentatorId' => $commentatorId,
111
            'username' => $username,
112
            'email' => $email,
113
            'website' => $website,
114
            'ipAddr' => $item->getIpAddress(),
115
            'city' => $locationCity,
116
            'region' => $locationRegion,
117
            'country' => $locationCountry,
118
            'countryFlag' => $flag,
119
            'parent' => $parentId,
120
            'imageHash' => $imageHash,
121
            'deleted' => $item->isDeleted(),
122
            'userAgent' => $userAgent,
123
            'bot' => $bot,
124
            'createdAt' => $this->dateTimeToISO($item->getTimeCreated()),
125
        ];
126
    }
127

128
    /**
129
     * @param Comment $entity
130
     * @param array $data
131
     */
132
    public static function reverseTransform(Comment $entity, array $data)
133
    {
134
        $entity->setText($data['text']);
135
    }
136

137
    /**
138
     * @param Comment $entity
139
     *
140
     * @return ResourceInterface
141
     */
142
    public function includeCommentator(Comment $entity): ResourceInterface
143
    {
144
        $commentator = $entity->getCommentator();
145
        $items = [];
146
        if ($commentator) {
147
            $items = [$commentator];
148
        }
149

150
        return $this->collection($items, new CommentatorTransformer(), 'commentators');
151
    }
152
}
153

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.