Completed
Push — master ( d072d1...74ad36 )
by Michael
11:11
created

MapperFactory::getCustomMappers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 ##DOC-SIGNATURE##
5
6
 This file is part of WideImage.
7
8
 WideImage is free software; you can redistribute it and/or modify
9
 it under the terms of the GNU Lesser General Public License as published by
10
 the Free Software Foundation; either version 2.1 of the License, or
11
 (at your option) any later version.
12
13
 WideImage is distributed in the hope that it will be useful,
14
 but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 GNU Lesser General Public License for more details.
17
18
 You should have received a copy of the GNU Lesser General Public License
19
 along with WideImage; if not, write to the Free Software
20
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21
22
 * @package WideImage
23
 **/
24
25
namespace WideImage;
26
27
use WideImage\Mapper;
28
use WideImage\Exception\UnsupportedFormatException;
29
30
/**
31
 * Mapper factory
32
 * 
33
 * @package Internals
34
 **/
35
abstract class MapperFactory
36
{
37
	static protected $mappers       = array();
38
	static protected $customMappers = array();
39
	
40
	static protected $mimeTable = array(
41
		'image/jpg'   => 'JPEG', 
42
		'image/jpeg'  => 'JPEG', 
43
		'image/pjpeg' => 'JPEG', 
44
		'image/gif'   => 'GIF', 
45
		'image/png'   => 'PNG',
46
		'image/webp'  => 'WEBP'
47
	);
48
	
49
	/**
50
	 * Returns a mapper, based on the $uri and $format
51
	 * 
52
	 * @param string $uri File URI
53
	 * @param string $format File format (extension or mime-type) or null
54
	 * @return mixed
55
	 **/
56
	public static function selectMapper($uri, $format = null)
57
	{
58
		$format = self::determineFormat($uri, $format);
59
		
60
		if (array_key_exists($format, self::$mappers)) {
61
			return self::$mappers[$format];
62
		}
63
		
64
		$mapperClassName = '\\WideImage\\Mapper\\' . $format;
65
		
66
		// why not use autoloading?
67
		// if (!class_exists($mapperClassName, false)) {
68
		if (!class_exists($mapperClassName)) {
69
			throw new UnsupportedFormatException("Format '{$format}' is not supported.");
70
		}
71
		
72
		if (class_exists($mapperClassName)) {
73
			self::$mappers[$format] = new $mapperClassName();
74
			return self::$mappers[$format];
75
		}
76
	}
77
	
78
	public static function registerMapper($mapper_class_name, $mime_type, $extension)
79
	{
80
		self::$customMappers[$mime_type] = $mapper_class_name;
81
		self::$mimeTable[$mime_type] = $extension;
82
	}
83
	
84
	public static function getCustomMappers()
85
	{
86
		return self::$customMappers;
87
	}
88
	
89
	public static function determineFormat($uri, $format = null)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
90
	{
91
		if ($format == null) {
92
			$format = self::extractExtension($uri);
93
		}
94
		
95
		// mime-type match
96
		if (preg_match('~[a-z]*/[a-z-]*~i', $format)) {
97
			if (isset(self::$mimeTable[strtolower($format)])) {
98
				return self::$mimeTable[strtolower($format)];
99
			}
100
		}
101
		
102
		// clean the string
103
		$format = strtoupper(preg_replace('/[^a-z0-9_-]/i', '', $format));
104
		if ($format == 'JPG') {
105
			$format = 'JPEG';
106
		}
107
		
108
		return $format;
109
	}
110
	
111
	public static function mimeType($format)
112
	{
113
		$format = strtoupper($format);
114
		
115
		if ($format == 'JPG') {
116
			$format = 'JPEG';
117
		}
118
		
119
		return array_search($format, self::$mimeTable);
120
	}
121
	
122
	public static function extractExtension($uri)
123
	{
124
		$p = strrpos($uri, '.');
125
		
126
		if ($p === false) {
127
			return '';
128
		}
129
		
130
		return substr($uri, $p + 1);
131
	}
132
}
133