Created
January 2, 2019 16:52
-
-
Save yahyaerturan/58f9b6a93e63355e2dcbe1ab18a1fff0 to your computer and use it in GitHub Desktop.
Symfony Doğru Yazdırtıyor :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .... | |
| ###> YahyaERTURAN ### | |
| App\Service\HttpExtensions\XhrResponse: ~ | |
| App\Service\HttpExtensions\XhrResponseInterace: '@App\Service\HttpExtensions\XhrResponse' | |
| ###< YahyaERTURAN ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| * This file is part of the VayesIMS package. | |
| * | |
| * (c) Yahya ERTURAN <root@yahyaerturan.com> | |
| * | |
| * For the full copyright and license information, please view the LICENSE | |
| * file that was distributed with this source code. | |
| */ | |
| namespace App\Controller; | |
| use App\Service\HttpExtensions\XhrResponseInterface; | |
| use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |
| use Symfony\Component\Routing\Annotation\Route; | |
| class TestController extends AbstractController | |
| { | |
| /** | |
| * @Route("/") | |
| */ | |
| public function test1(XhrResponseInterface $xhrResponse) | |
| { | |
| return $xhrResponse->respond('message'); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| * This file is part of the VayesIMS package. | |
| * | |
| * (c) Yahya ERTURAN <root@yahyaerturan.com> | |
| * | |
| * For the full copyright and license information, please view the LICENSE | |
| * file that was distributed with this source code. | |
| */ | |
| namespace App\Service\HttpExtensions; | |
| use Symfony\Component\HttpFoundation\JsonResponse; | |
| use Symfony\Component\Serializer\SerializerInterface; | |
| use Symfony\Contracts\Translation\TranslatorInterface; | |
| class XhrResponse implements XhrResponseInterface | |
| { | |
| private $translator; | |
| private $serializer; | |
| public function __construct(TranslatorInterface $translator, SerializerInterface $serializer) | |
| { | |
| $this->translator = $translator; | |
| $this->serializer = $serializer; | |
| } | |
| public function respond(?string $message, int $statusCode = 200, $data = null, array $serializationGroups = [], array $headers = [], bool $json = false) | |
| { | |
| $response = (object) null; | |
| if ($statusCode >= 200 && $statusCode < 300) { | |
| $response->data = (object) null; | |
| $response->data->status = 'success'; | |
| $response->data->code = $statusCode; | |
| $response->data->message = $this->translator->trans($message); | |
| $response->data->items = $data; | |
| $response->data->items = $this->serializer->normalize( | |
| $data, | |
| 'json', | |
| ['group' => $serializationGroups] | |
| ); | |
| } elseif ($statusCode >= 400 && $statusCode < 500) { | |
| $response->error = (object) null; | |
| $response->error->status = 'error'; | |
| $response->error->code = $statusCode; | |
| $response->error->message = $message | |
| ? $this->translator->trans($message) | |
| : $this->translator->trans('Hmm, there are still some errors that require your attention!'); | |
| $response->error->errors = $data; | |
| } | |
| return new JsonResponse($response, $statusCode, $headers, $json); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| * This file is part of the VayesIMS package. | |
| * | |
| * (c) Yahya ERTURAN <root@yahyaerturan.com> | |
| * | |
| * For the full copyright and license information, please view the LICENSE | |
| * file that was distributed with this source code. | |
| */ | |
| namespace App\Service\HttpExtensions; | |
| use Symfony\Component\HttpFoundation\JsonResponse; | |
| interface XhrResponseInterface | |
| { | |
| /** | |
| * @param null|JsonResponse $data | |
| */ | |
| public function respond( | |
| ?string $message, | |
| int $statusCode = 200, | |
| $data = null, | |
| array $serializationGroups = [], | |
| array $headers = [], | |
| bool $json = false | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment