-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplate.class.php
More file actions
executable file
·56 lines (55 loc) · 1.87 KB
/
Copy pathTemplate.class.php
File metadata and controls
executable file
·56 lines (55 loc) · 1.87 KB
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
<?php
namespace template;
class Template{
public static function toHtml( $file, array $map, $template = "" ){
$result = NULL;
$template = self::getTemplate( $file, $template );
if( count( $map ) == 0 ){
throw new EmptyMapException();
}
else{
$result = self::mapping( $map, $template );
}
return $result;
}
protected static function mapping( array $map, $template ){
foreach( $map as $name => $value ){
$template = self::itemMapping( $name, $value, $template );
}
return $template;
}
protected static function itemMapping( $name, $value, $template ){
if( is_object( $value ) ){
throw new TemplateValueIsObject( get_class( $value ) );
}
else{
$template = str_replace( $name, $value, $template );
}
return $template;
}
protected static function getTemplate( $file, $template ){
$result = NULL;
if( trim( $template ) == '' ){
if( trim( $file ) == '' ){
throw new EmptyFileNameException();
}
else{
if( file_exists( $file ) ){
$result = file_get_contents( $file );
}
else{
throw new TemplateFileNotExistException( $file );
}
}
}
else{
$result = $template;
}
return $result;
}
}
class EmptyFileNameException extends \Exception{}
class TemplateFileNotExistException extends \Exception{}
class EmptyMapException extends \Exception{}
class TemplateValueIsObject extends \Exception{}
/* eof */