2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
2018-11-06 19:23:32 +01:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
class Format {
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
static protected $dirFormat;
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
public function __construct(){
|
|
|
|
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
static public function create($nameFormat){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!preg_match('@^[A-Z][a-zA-Z]*$@', $nameFormat)) {
|
2016-09-10 21:01:02 +02:00
|
|
|
throw new \InvalidArgumentException('Name format must be at least
|
|
|
|
one uppercase follow or not by alphabetic characters.');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
$nameFormat = $nameFormat . 'Format';
|
|
|
|
$pathFormat = self::getDir() . $nameFormat . '.php';
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!file_exists($pathFormat)) {
|
2018-11-13 17:24:36 +01:00
|
|
|
throw new \Exception('The format you are looking for does not exist.');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
require_once $pathFormat;
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
return new $nameFormat();
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
static public function setDir($dirFormat){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_string($dirFormat)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \InvalidArgumentException('Dir format must be a string.');
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!file_exists($dirFormat)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \Exception('Dir format does not exist.');
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
self::$dirFormat = $dirFormat;
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
static public function getDir(){
|
|
|
|
$dirFormat = self::$dirFormat;
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(is_null($dirFormat)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \LogicException(__CLASS__ . ' class need to know format path !');
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
return $dirFormat;
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
/**
|
|
|
|
* Read format dir and catch informations about each format depending annotation
|
|
|
|
* @return array Informations about each format
|
|
|
|
*/
|
|
|
|
static public function searchInformation(){
|
|
|
|
$pathDirFormat = self::getDir();
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
$listFormat = array();
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
$searchCommonPattern = array('name');
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
$dirFiles = scandir($pathDirFormat);
|
2017-07-29 19:28:00 +02:00
|
|
|
if($dirFiles !== false) {
|
|
|
|
foreach($dirFiles as $fileName) {
|
|
|
|
if(preg_match('@^([^.]+)Format\.php$@U', $fileName, $out)) { // Is PHP file ?
|
2016-09-10 20:41:11 +02:00
|
|
|
$listFormat[] = $out[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
return $listFormat;
|
|
|
|
}
|
2016-08-23 14:29:53 +02:00
|
|
|
}
|