zend-blog-3-backend
152 строки · 4.4 Кб
1<?php
2/**
3* Created by PhpStorm.
4* User: morontt
5* Date: 29.11.14
6* Time: 15:19
7*/
8
9namespace App\API\Transformers;
10
11use App\Entity\Comment;
12use App\Entity\CommentInterface;
13use App\Entity\ViewComment;
14use App\Entity\ViewCommentator;
15use App\Utils\EmojiFlagSymbol;
16use League\Fractal\Resource\ResourceInterface;
17
18class CommentTransformer extends BaseTransformer
19{
20/**
21* @var array
22*/
23protected array $availableIncludes = [
24'commentator',
25];
26
27/**
28* @param CommentInterface $item
29*
30* @return array
31*/
32public 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
47if ($item instanceof Comment) {
48$commentator = $item->getCommentator();
49if ($commentator) {
50$commentatorId = $commentator->getId();
51$username = $commentator->getName();
52$email = $commentator->getEmail();
53$website = $commentator->getWebsite();
54} else {
55$user = $item->getUser();
56if ($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();
65if ($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();
73if ($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();
96if ($parent) {
97$parentId = $parent->getId();
98}
99
100try {
101$flag = $countryCode ? EmojiFlagSymbol::get($countryCode) : '';
102} catch (\Exception $e) {
103$flag = '';
104}
105
106return [
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*/
132public 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*/
142public function includeCommentator(Comment $entity): ResourceInterface
143{
144$commentator = $entity->getCommentator();
145$items = [];
146if ($commentator) {
147$items = [$commentator];
148}
149
150return $this->collection($items, new CommentatorTransformer(), 'commentators');
151}
152}
153