rss-bridge/lib/Format.php

74 lines
1.7 KiB
PHP
Raw Normal View History

2013-08-11 13:30:41 +02:00
<?php
require_once(__DIR__ . '/FormatInterface.php');
class Format {
2013-08-11 13:30:41 +02:00
static protected $dirFormat;
2013-08-11 13:30:41 +02:00
public function __construct(){
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
}
2013-08-11 13:30:41 +02:00
static public function create($nameFormat){
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.');
}
2013-08-11 13:30:41 +02:00
$nameFormat = $nameFormat . 'Format';
$pathFormat = self::getDir() . $nameFormat . '.php';
2013-08-11 13:30:41 +02:00
if(!file_exists($pathFormat)){
throw new \Exception('The format you looking for does not exist.');
}
2013-08-11 13:30:41 +02:00
require_once $pathFormat;
2013-08-11 13:30:41 +02:00
return new $nameFormat();
}
2013-08-11 13:30:41 +02:00
static public function setDir($dirFormat){
if(!is_string($dirFormat)){
throw new \InvalidArgumentException('Dir format must be a string.');
}
2013-08-11 13:30:41 +02:00
if(!file_exists($dirFormat)){
throw new \Exception('Dir format does not exist.');
}
2013-08-11 13:30:41 +02:00
self::$dirFormat = $dirFormat;
}
2013-08-11 13:30:41 +02:00
static public function getDir(){
$dirFormat = self::$dirFormat;
2013-08-11 13:30:41 +02:00
if(is_null($dirFormat)){
throw new \LogicException(__CLASS__ . ' class need to know format path !');
}
2013-08-11 13:30:41 +02:00
return $dirFormat;
}
2013-08-11 13:30:41 +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
$listFormat = array();
2013-08-11 13:30:41 +02:00
$searchCommonPattern = array('name');
2013-08-11 13:30:41 +02: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 13:30:41 +02:00
return $listFormat;
}
}