diff --git a/includes/class-Months.php b/includes/class-Months.php new file mode 100644 index 0000000..c65e8a4 --- /dev/null +++ b/includes/class-Months.php @@ -0,0 +1,90 @@ +. + +namespace itwikidelbot; + +/** + * Singleton that handle months + */ +class Months { + + /** + * Months + * + * @var array + */ + private static $_months; + + /** + * Get the names of the months + * + * @return array Names of months + */ + public static function months() { + if( ! self::$_months ) { + self::$_months = [ + 'gennaio', + 'febbraio', + 'marzo', + 'aprile', + 'maggio', + 'giugno', + 'luglio', + 'agosto', + 'settembre', + 'ottobre', + 'novembre', + 'dicembre' + ]; + } + return self::$_months; + } + + /** + * Get a month number from a month name + * + * @param $month_name string Month name e.g. 'gennaio' + * @return int Month number 0-11 e.g. 0 + */ + public static function monthName2MonthNumber( $month_name ) { + $i = array_search( $month_name, self::months(), true ); + if( false === $i ) { + throw new \InvalidArgumentException( 'unexpected month name' ); + } + return $i; + } + + /** + * Get a month name from a month number + * + * @param $month_number int Month number 0-11 e.g. 0 + * @return string Month name e.g. 'gennaio' + */ + public static function monthNumber2MonthName( $month_number ) { + if( $month_number < 0 || $month_number > 11 ) { + throw new \InvalidArgumentException( 'unexpected month number' ); + } + $months = self::months(); + return $months[ $month_number ]; + } +}