* @copyright 2016-2017 GFI Informatique, 2016-2017 TNT * @license https://opensource.org/licenses/MIT MIT License */ require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Debug.php'; /** * Class TNTOfficiel_ShippingHelper. */ class TNTOfficiel_ShippingHelper { /** * @var Singleton */ private static $_instance = null; /** * Constructor. */ public function __construct() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); } /** * Creates a singleton. * * @param void * * @return TNTOfficiel_ShippingHelper */ public static function getInstance() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); if (is_null(TNTOfficiel_ShippingHelper::$_instance)) { TNTOfficiel_ShippingHelper::$_instance = new TNTOfficiel_ShippingHelper(); } return TNTOfficiel_ShippingHelper::$_instance; } /** * @var array */ protected $_days = array( 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', ); /** * Returns an array with schedules for each days. * * @param array $params Parameters of the function * * @return array */ public function getSchedules(array $params = array()) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $schedules = array(); if ($hours = $params['hours']) { $index = 0; // Position corresponds to the number of the day $position = 0; // Current day $day = null; // Part of the day $part = null; foreach ($hours as $hour) { $hour = trim($hour); $day = $this->_days[$position]; if (($index % 2 === 0) && ($index % 4 === 0)) { $part = 'AM'; } elseif (($index % 2 === 0) && ($index % 4 === 2)) { $part = 'PM'; } // Prepare the current Day if (!isset($schedules[$day])) { $schedules[$day] = array(); } // Prepare the current period of the current dya if (!isset($schedules[$day][$part]) && $hour) { $schedules[$day][$part] = array(); } // If hours different from 0 if ($hour) { // Add hour $schedules[$day][$part][] = $hour; } ++$index; if ($index % 4 == 0) { ++$position; } } } return $schedules; } }