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