summaryrefslogtreecommitdiff
path: root/lib/FormatFactory.php
blob: 28db7596ffb5ea54106475cc4fd6669bf8a249b6 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
 * This file is part of RSS-Bridge, a PHP project capable of generating RSS and
 * Atom feeds for websites that don't have one.
 *
 * For the full license information, please view the UNLICENSE file distributed
 * with this source code.
 *
 * @package	Core
 * @license	http://unlicense.org/ UNLICENSE
 * @link	https://github.com/rss-bridge/rss-bridge
 */

/**
 * Factory class responsible for creating format objects from a given working
 * directory.
 *
 * This class is capable of:
 * - Locating format classes in the specified working directory (see {@see Format::$workingDir})
 * - Creating new format instances based on the format's name (see {@see Format::create()})
 *
 * The following example illustrates the intended use for this class.
 *
 * ```PHP
 * require_once __DIR__ . '/rssbridge.php';
 *
 * // Step 1: Set the working directory
 * Format::setWorkingDir(__DIR__ . '/../formats/');
 *
 * // Step 2: Create a new instance of a format object (based on the name)
 * $format = Format::create('Atom');
 * ```
 */
class FormatFactory extends FactoryAbstract {
	/**
	 * Creates a new format object from the working directory.
	 *
	 * @throws \InvalidArgumentException if the requested format name is invalid.
	 * @throws \Exception if the requested format file doesn't exist in the
	 * working directory.
	 * @param string $name Name of the format object.
	 * @return object|bool The format object or false if the class is not instantiable.
	 */
	public function create($name){
		if(!$this->isFormatName($name)) {
			throw new \InvalidArgumentException('Format name invalid!');
		}

		$name = $this->sanitizeFormatName($name) . 'Format';
		$pathFormat = $this->getWorkingDir() . $name . '.php';

		if(!file_exists($pathFormat)) {
			throw new \Exception('Format file ' . $filePath . ' does not exist!');
		}

		require_once $pathFormat;

		if((new \ReflectionClass($name))->isInstantiable()) {
			return new $name();
		}

		return false;
	}

	/**
	 * Returns true if the provided name is a valid format name.
	 *
	 * A valid format name starts with a capital letter ([A-Z]), followed by
	 * zero or more alphanumeric characters or hyphen ([A-Za-z0-9-]).
	 *
	 * @param string $name The format name.
	 * @return bool true if the name is a valid format name, false otherwise.
	 */
	public function isFormatName($name){
		return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1;
	}

	/**
	 * Returns the list of format names from the working directory.
	 *
	 * The list is cached internally to allow for successive calls.
	 *
	 * @return array List of format names
	 */
	public function getFormatNames(){
		static $formatNames = array(); // Initialized on first call

		if(empty($formatNames)) {
			$files = scandir($this->getWorkingDir());

			if($files !== false) {
				foreach($files as $file) {
					if(preg_match('/^([^.]+)Format\.php$/U', $file, $out)) {
						$formatNames[] = $out[1];
					}
				}
			}
		}

		return $formatNames;
	}

	/**
	 * Returns the sanitized format name.
	 *
	 * The format name can be specified in various ways:
	 * * The PHP file name (i.e. `AtomFormat.php`)
	 * * The PHP file name without file extension (i.e. `AtomFormat`)
	 * * The format name (i.e. `Atom`)
	 *
	 * Casing is ignored (i.e. `ATOM` and `atom` are the same).
	 *
	 * A format file matching the given format name must exist in the working
	 * directory!
	 *
	 * @param string $name The format name
	 * @return string|null The sanitized format name if the provided name is
	 * valid, null otherwise.
	 */
	protected function sanitizeFormatName($name) {

		if(is_string($name)) {

			// Trim trailing '.php' if exists
			if(preg_match('/(.+)(?:\.php)/', $name, $matches)) {
				$name = $matches[1];
			}

			// Trim trailing 'Format' if exists
			if(preg_match('/(.+)(?:Format)/i', $name, $matches)) {
				$name = $matches[1];
			}

			// Improve performance for correctly written format names
			if(in_array($name, $this->getFormatNames())) {
				$index = array_search($name, $this->getFormatNames());
				return $this->getFormatNames()[$index];
			}

			// The name is valid if a corresponding format file is found on disk
			if(in_array(strtolower($name), array_map('strtolower', $this->getFormatNames()))) {
				$index = array_search(strtolower($name), array_map('strtolower', $this->getFormatNames()));
				return $this->getFormatNames()[$index];
			}

			Debug::log('Invalid format name: "' . $name . '"!');

		}

		return null; // Bad parameter

	}
}