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