PHP Classes

File: Component/HttpKernel/DataCollector/TranslationsDataCollector.php

Recommend this page to a friend!
  Classes of Joseluis Laso   Tradukoj.com Translations API Bundle   Component/HttpKernel/DataCollector/TranslationsDataCollector.php   Download  
File: Component/HttpKernel/DataCollector/TranslationsDataCollector.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Tradukoj.com Translations API Bundle
Get remote translations for Symfony projects
Author: By
Last change:
Date: 9 years ago
Size: 1,509 bytes
 

Contents

Class file image Download
<?php

namespace JLaso\TranslationsApiBundle\Component\HttpKernel\DataCollector;

use
Symfony\Component\HttpKernel\DataCollector\DataCollector;
use
Symfony\Component\HttpFoundation\Request;
use
Symfony\Component\HttpFoundation\Response;
use
Doctrine\Common\Collections\ArrayCollection;

class
TranslationsDataCollector extends DataCollector
{

    private
$translator;

    public function
__construct($translator)
    {
       
$this->translator = $translator;
    }

    public function
collect(Request $request, Response $response, \Exception $exception = null)
    {
       
$this->data = array(
           
'translations' => $request->get('usedTranslations'),
        );
    }

    public function
getTotalTranslations()
    {
        return
count($this->data['translations']);
    }

    public function
getTranslations()
    {
       
$translationsCollection = $this->data['translations'];

        if (!
$translationsCollection instanceof \Doctrine\Common\Collections\ArrayCollection) {
            return new
ArrayCollection;
        }

       
$iterator = $translationsCollection->getIterator();

       
$iterator->uasort(function ($first, $second) {
                return
$first->getDomain() . $first->getKeyword() > $second->getDomain() . $second->getKeyword() ? 1 : -1;
            });

        return
$iterator;
    }

    public function
getName()
    {
        return
'translator';
    }

    public function
setTranslator($translator)
    {
       
$this->translator = $translator;
    }
}