<?php 



namespace Retorno;

/**
 * Classe de Utilidades
 * 
 * @package Retorno
 * @author Mauricio Rodrigues <mauricio.vsr@gmail.com>
 * @copyright  Copyright (c) 2014 M2 Digital (http://www.m2digital.com.br)
 * @license MIT License
 * @version 0.1
 * 
 */

class Utils{

	/**
	 * PRE - Tag de abertura
	 * @var string
	 */
	const PRE_INI = "<pre>";

	/**
	 * PRE - tag de fechamento
	 * @var string
	 */
	const PRE_END = "</pre>";


	/**
	 * Execute a var_dump and die methods.
	 * The script will be stoped after call this method.
	 * @param array $data
	 * @return void
	 */
	public static function dd($data){
		echo self::PRE_INI;
		var_dump($data);
		echo self::PRE_END;
		die();
	}


	/**
	 * Print array data recursively.
	 * @param array $data
	 * @return void
	 */
	public static function pre($data){
		echo self::PRE_INI;
		print_r($data);
		echo self::PRE_END;
	}


	/**
	 * Replaces assumed decimal comma
	 * 
	 * @param string $valor Valor with decimal comma assumed
	 * @param boolean $raw If true, return brazilian real formated decimal
	 * @param int $decimalLength Set de length 
	 * @return string
	 */
	public static function numberFormat($valor, $raw = true, $decimalLength = 2){

		$left 	= substr($valor, 0, strlen($valor)-$decimalLength);
		$right 	= substr($valor, strlen($valor)-$decimalLength, strlen($valor));
		$tmp 	= $left .'.'. $right;  

		return $raw ? number_format($tmp,2,".","") : number_format($tmp, 2, ',', '.');
	}


	/**
	 * Método para tratar as datas existentes no arquivo de retorno.
	 * 
	 * @param string $data data no formato (dmy - 311214)
	 * @param string $formatToReturn Formato da data que será retornada. More info: http://php.net/manual/en/datetime.formats.php
	 * @param string $timezone Representation of time zone. More info: http://php.net/manual/en/class.datetimezone.php
	 * @return mixed string|DateTime Object
	 */
	public static function dateFormat($data, $formatToReturn = null, $timezone = 'America/Fortaleza'){
		
		$dia = substr($data, 0, 2);
		$mes = substr($data, 2, 2);
		$ano = '20' . substr($data, 4, 2);
		
		$format = "{$ano}-{$mes}-{$dia}";
		
		$dateTime = new \DateTime($format, new \DateTimeZone($timezone));
		
		return is_null($formatToReturn) ? $dateTime : $dateTime->format($formatToReturn);

	}

}