<?php
/**
 * Utilities
 *
 * @author Lucas Dantas <lucas@caju.nu>
 * @package classes
 * @subpackage util
 * @version 1.0
 * @since 2018/04/10 16:45
 **/
Class Util{

	/**
	 * Formatting the fields
	 *
	 * @access public
	 * @static
	 * @param array $fields Fields
	 * @return array
	 */
	public static function formatFields( $fields = [] ){

		//Listing
		foreach( $fields as $key => $field )
			//Defining
			$fields[ $key ]	= ( isset( $fields[ $key ] ) && $fields[ $key ] != '' ) ? $fields[ $key ] : null;

		//Returning
		return $fields;

	}

	/**
	 * Defining the description
	 *
	 * @access public
	 * @static
	 * @param string $text Text
	 * @param integer $wordQuantity Word's quantity
	 * @param boolean $strip Remove or not the strip tags
	 * @return string
	 */
	public static function defineDescription( $text, $wordQuantity = 15, $strip = true ){

		//Defining
		$word		= explode( ' ', preg_replace( "/\r\n|\r|\n|\t/", '', strip_tags( $text ) ) );

		//Defining
		$quantity	= count( $word );

		//Checking
		if( $quantity <= $wordQuantity )
			//Returning
			return ( $strip ) ? rtrim( ltrim( trim( strip_tags( $text ) ) ) ) : $text;
		else{

			//Defining
			$text	= null;

			//Listing
			for( $intI = 0; $intI < $wordQuantity; $intI++ )
				//Defining
				$text .= $word[ $intI ] . ' ';

			//Defining
			$text	.= '...';

			//Returning
			return ( $strip ) ? rtrim( ltrim( trim( strip_tags( $text ) ) ) ) : $text;

		}

	}

	/**
	 * Defining the database order
	 *
	 * @access public
	 * @static
	 * @param string $order Order
	 * @return string
	 */
	public static function defineDatabaseOrder( $order = null ){

		//Checking
		switch( $order ){

			//Name
			case 'name':

				//Returning
				return 'name';

			break;

			//Random
			case 'random':

				//Returning
				return 'RAND()';

			break;

			//Title
			case 'title':

				//Returning
				return 'title';

			break;

			//Sequence
			case 'sequence':

				//Returning
				return 'sequence';

			break;

			//Date
			case 'date':

				//Returning
				return 'date';

			break;

			//Date DESC
			case 'date-desc':

				//Returning
				return 'date DESC';

			break;

			//Default
			case 'id':

				//Returning
				return 'id';

			break;

			//Default
			default:

				//Returning
				return 'id DESC';

			break;

		}

	}

	/**
	 * Formatting the date
	 *
	 * @access public
	 * @static
	 * @param string $date Date
	 * @param string $type Type
	 * @return mixed
	 */
	public static function formatDate( $date, $type = 'default' ){

		//Checking
		if( !is_null( $date ) ){

			//Defining
			$date	= new DateTime( str_replace( '/', '-', str_replace( '-', '', $date ) ) );

			//Checking
			switch( $type ){

				//Database without hour
				case 'database-no-hour':

					//Defining
					$date	= $date->format( 'Y-m-d' );

				break;

				//database
				case 'database':

					//Defining
					$date	= $date->format( 'Y-m-d H:i:s' );

				break;

				//Default without hour
				case 'default-no-hour';

					//Defining
					$date	= $date->format( 'd/m/Y' );

				break;

				//Hour and minute
				case 'hour-minute';

					//Defining
					$date	= $date->format( 'H:i' );

				break;

				//Messages
				case 'message';

					//Defining
					$date	= $date->format( 'd.m.Y - H:i' );

				break;

				//Site
				case 'site':

					//Defining
					$date	= $date->format( 'd.m.Y' );

				break;

				//Separated
				case 'separate':

					//Defining
					$dateHour	= explode( ' ', $date->format( 'Y-m-d H:i:s' ) );

					//Defining the array
					$dates		= explode( '-', $dateHour[ 0 ] );
					$hour		= explode( ':', $dateHour[ 1 ] );

					//Definindo
					$object		= new stdclass();

					//Defining the values
					$object->date			= $dateHour[ 0 ];
					$object->hours			= $dateHour[ 1 ];
					$object->day			= $dates[ 2 ];
					$object->month			= $dates[ 1 ];
					$object->month_long		= strftime( "%B", strtotime( $date->format( 'd-m-Y' ) ) );
					$object->month_short	= strftime( "%b", strtotime( $date->format( 'd-m-Y' ) ) );
					$object->year			= $dates[ 0 ];
					$object->hour			= $hour[ 0 ];
					$object->minute			= $hour[ 1 ];
					$object->second			= $hour[ 2 ];

					//Defining
					$date	= $object;

				break;

				//Default
				default:

					//Defining
					$date	= $date->format( 'd/m/Y - H:i' );

				break;

			}

			//Returning
			return ( !$date instanceof stdclass ) ? str_replace( [ ' - 00:00:00', ' - 00:00' ], '', $date ) : $date;

		}else
			//Returning
			return null;

	}

	/**
	 * Defining the destination's options
	 *
	 * @access public
	 * @static
	 * @param integer $destination Destination
	 * @return stdclass
	 */
	public static function defineDestinationOptions( $destination ){

		//Defining
		$object	= new \stdclass();

		//Checking
		switch( $destination ){

			//Contact
			case DESTINATION_CONTACT:

				//Defining
				$object->type			= 'contact';
				$object->answer_table	= 'contact_answer';
				$object->table			= 'contact';

			break;

			//Proposal
			case DESTINATION_PROPOSAL:

				//Defining
				$object->type			= 'proposal';
				$object->answer_table	= 'proposal_answer';
				$object->table			= 'proposal';

			break;

			//Order
			case DESTINATION_ORDER:

				//Defining
				$object->type			= 'order';
				$object->answer_table	= 'orders_answer';
				$object->table			= 'orders';

			break;

		}

		//Returning
		return $object;

	}

}