<commit_before><?php

namespace Netrunnerdb\BuilderBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\JsonResponse;
use FOS\RestBundle\Controller\FOSRestController;
use Netrunnerdb\BuilderBundle\Entity\Deck;

class PrivateApi20Controller extends FOSRestController
{
	private function prepareResponse(array $data)
	{
		$response = new JsonResponse();
		$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
		$response->setEncodingOptions(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
		$response->setPrivate();
		
		$content = [ 'version_number' => '2.0' ];
		
		$content['data'] = array_map(function ($entity) {
			return (is_object($entity) && $entity instanceof \Serializable) ? $entity->serialize() : $entity;
		}, $data);
		
		$content['total'] = count($content['data']);
		
		$content['success'] = TRUE;
		
		$response->setData($content);
		
		return $response;
	}
	
	private function prepareFailedResponse($msg)
	{
		$response = new JsonResponse();
		$response->setEncodingOptions(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
		$response->setPrivate();
		
		$content = [ 'version_number' => '2.0' ];
		
		$content['success'] = FALSE;
		
		$content['msg'] = $msg;
		
		return $response;
	}

	/**
	 * Get one deck
	 *
	 * @ApiDoc(
	 *  section="Deck",
	 *  resource=true,
	 *  description="Get one (private) deck of authenticated user",
	 *  parameters={
	 *    {"name"="include_history", "dataType"="boolean", "required"=false, "description"="truthy value (eg '1') to include the deck changes"},
	 *  }
	 * )
	 */
	public function loadDeckAction($deck_id, Request $request)
	{
		/* @var $user \Netrunnerdb\UserBundle\Entity\User */
		$user = $this->getUser();
		
		if(!$user) {
			throw $this->createAccessDeniedException("No user.");
		}
		
		$includeHistory = $request->query->has('include_history') && $request->query->get('include_history');
		
		$deck = $this->getDoctrine()->getManager()->getRepository('NetrunnerdbBuilderBundle:Deck')->findOneBy(['user' => $user, 'id' => $deck_id]);
		
		if(!$deck) {
			throw $this->createNotFoundException("Deck not found");
		}
		
		$history = [];
		
		if($includeHistory) 
		{
			$qb = $this->getDoctrine()->getManager()->getRepository('NetrunnerdbBuilderBundle:Deckchange')->createQueryBuilder('h');
			$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
			$qb->select('h')
				->from('NetrunnerdbBuilderBundle:Deckchange', 'h')
				->where('h.deck = :deck')
				->andWhere('h.saved = :saved')
				->orderBy('h.dateCreation', 'DESC')
				->setParameter('deck', $deck)
				->setParameter('saved', TRUE);
			$query = $qb->getQuery();
			$result = $query->getResult();
			
			foreach($result as $deckchange) {
				/* @var $deckchange \Netrunnerdb\BuilderBundle\Entity\Deckchange */
				$history[$deckchange->getDatecreation()->format('c')] = json_decode($deckchange->getVariation(), TRUE);
			}
		}
		
		$data = $deck->serialize();
		$data['history'] = $history;
		
		return $this->prepareResponse([$data]);<commit_msg>returns the list of changes at one history as one array of signed integers instead of two arrays of unsigned integers; fix #5<commit_after><?php

namespace Netrunnerdb\BuilderBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Symfony\Component\HttpFoundation\JsonResponse;
use FOS\RestBundle\Controller\FOSRestController;
use Netrunnerdb\BuilderBundle\Entity\Deck;

class PrivateApi20Controller extends FOSRestController
{
	private function prepareResponse(array $data)
	{
		$response = new JsonResponse();
		$response->headers->set('Content-Type', 'application/json; charset=UTF-8');
		$response->setEncodingOptions(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
		$response->setPrivate();
		
		$content = [ 'version_number' => '2.0' ];
		
		$content['data'] = array_map(function ($entity) {
			return (is_object($entity) && $entity instanceof \Serializable) ? $entity->serialize() : $entity;
		}, $data);
		
		$content['total'] = count($content['data']);
		
		$content['success'] = TRUE;
		
		$response->setData($content);
		
		return $response;
	}
	
	private function prepareFailedResponse($msg)
	{
		$response = new JsonResponse();
		$response->setEncodingOptions(JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
		$response->setPrivate();
		
		$content = [ 'version_number' => '2.0' ];
		
		$content['success'] = FALSE;
		
		$content['msg'] = $msg;
		
		return $response;
	}

	/**
	 * Get one deck
	 *
	 * @ApiDoc(
	 *  section="Deck",
	 *  resource=true,
	 *  description="Get one (private) deck of authenticated user",
	 *  parameters={
	 *    {"name"="include_history", "dataType"="boolean", "required"=false, "description"="truthy value (eg '1') to include the deck changes"},
	 *  }
	 * )
	 */
	public function loadDeckAction($deck_id, Request $request)
	{
		/* @var $user \Netrunnerdb\UserBundle\Entity\User */
		$user = $this->getUser();
		
		if(!$user) {
			throw $this->createAccessDeniedException("No user.");
		}
		
		$includeHistory = $request->query->has('include_history') && $request->query->get('include_history');
		
		$deck = $this->getDoctrine()->getManager()->getRepository('NetrunnerdbBuilderBundle:Deck')->findOneBy(['user' => $user, 'id' => $deck_id]);
		
		if(!$deck) {
			throw $this->createNotFoundException("Deck not found");
		}
		
		$history = [];
		
		if($includeHistory) 
		{
			$qb = $this->getDoctrine()->getManager()->getRepository('NetrunnerdbBuilderBundle:Deckchange')->createQueryBuilder('h');
			$qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder();
			$qb->select('h')
				->from('NetrunnerdbBuilderBundle:Deckchange', 'h')
				->where('h.deck = :deck')
				->andWhere('h.saved = :saved')
				->orderBy('h.dateCreation', 'DESC')
				->setParameter('deck', $deck)
				->setParameter('saved', TRUE);
			$query = $qb->getQuery();
			$result = $query->getResult();
			
			foreach($result as $deckchange) {
				/* @var $deckchange \Netrunnerdb\BuilderBundle\Entity\Deckchange */
				$variation = json_decode($deckchange->getVariation(), TRUE);
				$changes = [];
				foreach($variation[0] as $card_code => $addition) {
					$changes[$card_code] = $addition;
				}
				foreach($variation[1] as $card_code => $substraction) {
					$changes[$card_code] = - $substraction;
				}
				$history[$deckchange->getDatecreation()->format('c')] = $changes;
			}
		}
		
		$data = $deck->serialize();
		$data['history'] = $history;
		
		return $this->prepareResponse([$data]);
{'fraction_non_alphanumeric': 0.17543344108139877, 'fraction_numerical': 0.002791654422568322, 'mean_word_length': 4.705783738474434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7321502', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2128, 'n_words': 364}