summaryrefslogtreecommitdiff
path: root/lib/Format.php
blob: ab932cc74d964bb91f32159903f44a88d8e613a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
require_once(__DIR__ . '/FormatInterface.php');
class Format {

	static protected $dirFormat;

	public function __construct(){
		throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
	}

	static public function create($nameFormat){
		if(!preg_match('@^[A-Z][a-zA-Z]*$@', $nameFormat)) {
			throw new \InvalidArgumentException('Name format must be at least
 one uppercase follow or not by alphabetic characters.');
		}

		$nameFormat = $nameFormat . 'Format';
		$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) {
			foreach($dirFiles as $fileName) {
				if(preg_match('@^([^.]+)Format\.php$@U', $fileName, $out)) { // Is PHP file ?
					$listFormat[] = $out[1];
				}
			}
		}

		return $listFormat;
	}
}