... | ... |
@@ -66,7 +66,7 @@ class Serialization{ |
66 | 66 |
foreach($registeredTypes as $rtype){ |
67 | 67 |
$t = explode("/", $type); |
68 | 68 |
$rt = explode("/", $rtype); |
69 |
- if(($t[0]=="*" || $rt[0]=="*" || $t[0]==$t[0]) && ($t[1]=="*" || $rt[1]=="*" || $t[1]==$t[1])){ |
|
69 |
+ if(($t[0]=="*" || $rt[0]=="*" || $t[0]==$rt[0]) && ($t[1]=="*" || $rt[1]=="*" || $t[1]==$rt[1])){ |
|
70 | 70 |
return $rtype; |
71 | 71 |
} |
72 | 72 |
} |
... | ... |
@@ -11,7 +11,12 @@ class Serialization{ |
11 | 11 |
} |
12 | 12 |
|
13 | 13 |
public function register($contentType, $serializer, $viewModelClass = null){ |
14 |
- if(!isset(class_implements($serializer, true)['ISerializer'])){ |
|
14 |
+ |
|
15 |
+ if(!class_exists($serializer,true)){ |
|
16 |
+ throw new \Exception("Class \"$serializer\" not found!"); |
|
17 |
+ } |
|
18 |
+ |
|
19 |
+ if(!isset(class_implements($serializer, true)['elanpl\\L3\\ISerializer'])){ |
|
15 | 20 |
throw new \Exception("Class \"$serializer\" does not implement ISerializer interface!"); |
16 | 21 |
} |
17 | 22 |
|
... | ... |
@@ -3,21 +3,25 @@ |
3 | 3 |
namespace elanpl\L3; |
4 | 4 |
|
5 | 5 |
class Serialization{ |
6 |
- protected static $serializers; //registered serializers dictionary |
|
6 |
+ protected $serializers; //registered serializers dictionary |
|
7 | 7 |
|
8 | 8 |
public function __construct() |
9 | 9 |
{ |
10 |
- if(!isset(self::$serializers)) self::$serializers = array(); |
|
10 |
+ $this->serializers = array(); |
|
11 | 11 |
} |
12 | 12 |
|
13 |
- public static function register($contentType, $serializer, $viewModelClass = null){ |
|
13 |
+ public function register($contentType, $serializer, $viewModelClass = null){ |
|
14 |
+ if(!isset(class_implements($serializer, true)['ISerializer'])){ |
|
15 |
+ throw new \Exception("Class \"$serializer\" does not implement ISerializer interface!"); |
|
16 |
+ } |
|
17 |
+ |
|
14 | 18 |
if(isset($viewModelClass) && $viewModelClass!=''){ |
15 | 19 |
$class_key = $viewModelClass.'|'; |
16 | 20 |
} |
17 | 21 |
else{ |
18 | 22 |
$class_key = ''; |
19 | 23 |
} |
20 |
- self::$serializers[$class_key.$contentType] = $serializer; |
|
24 |
+ $this->serializers[$class_key.$contentType] = $serializer; |
|
21 | 25 |
} |
22 | 26 |
|
23 | 27 |
public function match($acceptTypes, $viewModel=null){ |
... | ... |
@@ -31,11 +35,11 @@ class Serialization{ |
31 | 35 |
} |
32 | 36 |
|
33 | 37 |
if(is_array($acceptTypes)){ |
34 |
- $registeredTypes = array_keys(self::$serializers); |
|
38 |
+ $registeredTypes = array_keys($this->serializers); |
|
35 | 39 |
|
36 | 40 |
foreach ($acceptTypes as $type){ |
37 | 41 |
//Dedicated config for ViewModel class first... |
38 |
- if(array_key_exists($viewModelClass."|".$type, self::$serializers)){ |
|
42 |
+ if(array_key_exists($viewModelClass."|".$type, $this->serializers)){ |
|
39 | 43 |
return $type; |
40 | 44 |
} |
41 | 45 |
foreach($registeredTypes as $rtype_with_class){ |
... | ... |
@@ -51,7 +55,7 @@ class Serialization{ |
51 | 55 |
} |
52 | 56 |
|
53 | 57 |
//Then check configs without the ViewModel class name |
54 |
- if(array_key_exists($type, self::$serializers)){ |
|
58 |
+ if(array_key_exists($type, $this->serializers)){ |
|
55 | 59 |
return $type; |
56 | 60 |
} |
57 | 61 |
foreach($registeredTypes as $rtype){ |
... | ... |
@@ -67,16 +71,16 @@ class Serialization{ |
67 | 71 |
} |
68 | 72 |
|
69 | 73 |
public function serialize($contentType, $viewModel){ |
70 |
- if(isset(Serialization::$serializers[get_class($viewModel).'|'.$contentType])){ |
|
71 |
- $serializerClass = Serialization::$serializers[get_class($viewModel).'|'.$contentType]; |
|
74 |
+ if(isset($this->serializers[get_class($viewModel).'|'.$contentType])){ |
|
75 |
+ $serializerClass = $this->serializers[get_class($viewModel).'|'.$contentType]; |
|
72 | 76 |
} |
73 |
- else if(isset(Serialization::$serializers[$contentType])){ |
|
74 |
- $serializerClass = Serialization::$serializers[$contentType]; |
|
77 |
+ else if(isset($this->serializers[$contentType])){ |
|
78 |
+ $serializerClass = $this->serializers[$contentType]; |
|
75 | 79 |
} |
76 | 80 |
|
77 | 81 |
if(isset($serializerClass)){ |
78 | 82 |
$serializer = new $serializerClass(); |
79 |
- return $serializer->Serialize($viewModel); |
|
83 |
+ return $serializer->serialize($viewModel); |
|
80 | 84 |
} |
81 | 85 |
else{ |
82 | 86 |
return null; |
... | ... |
@@ -10,41 +10,41 @@ class Serialization{ |
10 | 10 |
if(!isset(self::$serializers)) self::$serializers = array(); |
11 | 11 |
} |
12 | 12 |
|
13 |
- public static function Register($ContentType, $Serializer, $ViewModelClass = null){ |
|
14 |
- if(isset($ViewModelClass) && $ViewModelClass!=''){ |
|
15 |
- $class_key = $ViewModelClass.'|'; |
|
13 |
+ public static function register($contentType, $serializer, $viewModelClass = null){ |
|
14 |
+ if(isset($viewModelClass) && $viewModelClass!=''){ |
|
15 |
+ $class_key = $viewModelClass.'|'; |
|
16 | 16 |
} |
17 | 17 |
else{ |
18 | 18 |
$class_key = ''; |
19 | 19 |
} |
20 |
- self::$serializers[$class_key.$ContentType] = $Serializer; |
|
20 |
+ self::$serializers[$class_key.$contentType] = $serializer; |
|
21 | 21 |
} |
22 | 22 |
|
23 |
- public function Match($AcceptTypes, $ViewModel=null){ |
|
24 |
- if(isset($ViewModel)){ |
|
25 |
- if(is_object($ViewModel)){ |
|
26 |
- $ViewModelClass = get_class($ViewModel); |
|
23 |
+ public function match($acceptTypes, $viewModel=null){ |
|
24 |
+ if(isset($viewModel)){ |
|
25 |
+ if(is_object($viewModel)){ |
|
26 |
+ $viewModelClass = get_class($viewModel); |
|
27 | 27 |
} |
28 |
- if(is_string($ViewModel)){ |
|
29 |
- $ViewModelClass = $ViewModel; |
|
28 |
+ if(is_string($viewModel)){ |
|
29 |
+ $viewModelClass = $viewModel; |
|
30 | 30 |
} |
31 | 31 |
} |
32 | 32 |
|
33 |
- if(is_array($AcceptTypes)){ |
|
34 |
- $RegisteredTypes = array_keys(self::$serializers); |
|
33 |
+ if(is_array($acceptTypes)){ |
|
34 |
+ $registeredTypes = array_keys(self::$serializers); |
|
35 | 35 |
|
36 |
- foreach ($AcceptTypes as $type){ |
|
36 |
+ foreach ($acceptTypes as $type){ |
|
37 | 37 |
//Dedicated config for ViewModel class first... |
38 |
- if(array_key_exists($ViewModelClass."|".$type, self::$serializers)){ |
|
38 |
+ if(array_key_exists($viewModelClass."|".$type, self::$serializers)){ |
|
39 | 39 |
return $type; |
40 | 40 |
} |
41 |
- foreach($RegisteredTypes as $rtype_with_class){ |
|
41 |
+ foreach($registeredTypes as $rtype_with_class){ |
|
42 | 42 |
$rtype_parts = explode("|", $rtype_with_class); |
43 | 43 |
if(count($rtype_parts)==2){ |
44 | 44 |
$rclass = $rtype_parts[0]; |
45 | 45 |
$t = explode("/", $type); |
46 | 46 |
$rt = explode("/", $rtype_parts[1]); |
47 |
- if($rclass==$ViewModelClass && ($t[0]=="*" || $rt[0]=="*" || $t[0]==$rt[0]) && ($t[1]=="*" || $rt[1]=="*" || $t[1]==$rt[1])){ |
|
47 |
+ if($rclass==$viewModelClass && ($t[0]=="*" || $rt[0]=="*" || $t[0]==$rt[0]) && ($t[1]=="*" || $rt[1]=="*" || $t[1]==$rt[1])){ |
|
48 | 48 |
return $rtype_with_class; |
49 | 49 |
} |
50 | 50 |
} |
... | ... |
@@ -54,7 +54,7 @@ class Serialization{ |
54 | 54 |
if(array_key_exists($type, self::$serializers)){ |
55 | 55 |
return $type; |
56 | 56 |
} |
57 |
- foreach($RegisteredTypes as $rtype){ |
|
57 |
+ foreach($registeredTypes as $rtype){ |
|
58 | 58 |
$t = explode("/", $type); |
59 | 59 |
$rt = explode("/", $rtype); |
60 | 60 |
if(($t[0]=="*" || $rt[0]=="*" || $t[0]==$t[0]) && ($t[1]=="*" || $rt[1]=="*" || $t[1]==$t[1])){ |
... | ... |
@@ -66,17 +66,17 @@ class Serialization{ |
66 | 66 |
return false; |
67 | 67 |
} |
68 | 68 |
|
69 |
- public function Serialize($ContentType, $ViewModel){ |
|
70 |
- if(isset(Serialization::$serializers[get_class($ViewModel).'|'.$ContentType])){ |
|
71 |
- $serializerClass = Serialization::$serializers[get_class($ViewModel).'|'.$ContentType]; |
|
69 |
+ public function serialize($contentType, $viewModel){ |
|
70 |
+ if(isset(Serialization::$serializers[get_class($viewModel).'|'.$contentType])){ |
|
71 |
+ $serializerClass = Serialization::$serializers[get_class($viewModel).'|'.$contentType]; |
|
72 | 72 |
} |
73 |
- else if(isset(Serialization::$serializers[$ContentType])){ |
|
74 |
- $serializerClass = Serialization::$serializers[$ContentType]; |
|
73 |
+ else if(isset(Serialization::$serializers[$contentType])){ |
|
74 |
+ $serializerClass = Serialization::$serializers[$contentType]; |
|
75 | 75 |
} |
76 | 76 |
|
77 | 77 |
if(isset($serializerClass)){ |
78 | 78 |
$serializer = new $serializerClass(); |
79 |
- return $serializer->Serialize($ViewModel); |
|
79 |
+ return $serializer->Serialize($viewModel); |
|
80 | 80 |
} |
81 | 81 |
else{ |
82 | 82 |
return null; |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,85 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+namespace elanpl\L3; |
|
4 |
+ |
|
5 |
+class Serialization{ |
|
6 |
+ protected static $serializers; //registered serializers dictionary |
|
7 |
+ |
|
8 |
+ public function __construct() |
|
9 |
+ { |
|
10 |
+ if(!isset(self::$serializers)) self::$serializers = array(); |
|
11 |
+ } |
|
12 |
+ |
|
13 |
+ public static function Register($ContentType, $Serializer, $ViewModelClass = null){ |
|
14 |
+ if(isset($ViewModelClass) && $ViewModelClass!=''){ |
|
15 |
+ $class_key = $ViewModelClass.'|'; |
|
16 |
+ } |
|
17 |
+ else{ |
|
18 |
+ $class_key = ''; |
|
19 |
+ } |
|
20 |
+ self::$serializers[$class_key.$ContentType] = $Serializer; |
|
21 |
+ } |
|
22 |
+ |
|
23 |
+ public function Match($AcceptTypes, $ViewModel=null){ |
|
24 |
+ if(isset($ViewModel)){ |
|
25 |
+ if(is_object($ViewModel)){ |
|
26 |
+ $ViewModelClass = get_class($ViewModel); |
|
27 |
+ } |
|
28 |
+ if(is_string($ViewModel)){ |
|
29 |
+ $ViewModelClass = $ViewModel; |
|
30 |
+ } |
|
31 |
+ } |
|
32 |
+ |
|
33 |
+ if(is_array($AcceptTypes)){ |
|
34 |
+ $RegisteredTypes = array_keys(self::$serializers); |
|
35 |
+ |
|
36 |
+ foreach ($AcceptTypes as $type){ |
|
37 |
+ //Dedicated config for ViewModel class first... |
|
38 |
+ if(array_key_exists($ViewModelClass."|".$type, self::$serializers)){ |
|
39 |
+ return $type; |
|
40 |
+ } |
|
41 |
+ foreach($RegisteredTypes as $rtype_with_class){ |
|
42 |
+ $rtype_parts = explode("|", $rtype_with_class); |
|
43 |
+ if(count($rtype_parts)==2){ |
|
44 |
+ $rclass = $rtype_parts[0]; |
|
45 |
+ $t = explode("/", $type); |
|
46 |
+ $rt = explode("/", $rtype_parts[1]); |
|
47 |
+ if($rclass==$ViewModelClass && ($t[0]=="*" || $rt[0]=="*" || $t[0]==$rt[0]) && ($t[1]=="*" || $rt[1]=="*" || $t[1]==$rt[1])){ |
|
48 |
+ return $rtype_with_class; |
|
49 |
+ } |
|
50 |
+ } |
|
51 |
+ } |
|
52 |
+ |
|
53 |
+ //Then check configs without the ViewModel class name |
|
54 |
+ if(array_key_exists($type, self::$serializers)){ |
|
55 |
+ return $type; |
|
56 |
+ } |
|
57 |
+ foreach($RegisteredTypes as $rtype){ |
|
58 |
+ $t = explode("/", $type); |
|
59 |
+ $rt = explode("/", $rtype); |
|
60 |
+ if(($t[0]=="*" || $rt[0]=="*" || $t[0]==$t[0]) && ($t[1]=="*" || $rt[1]=="*" || $t[1]==$t[1])){ |
|
61 |
+ return $rtype; |
|
62 |
+ } |
|
63 |
+ } |
|
64 |
+ } |
|
65 |
+ } |
|
66 |
+ return false; |
|
67 |
+ } |
|
68 |
+ |
|
69 |
+ public function Serialize($ContentType, $ViewModel){ |
|
70 |
+ if(isset(Serialization::$serializers[get_class($ViewModel).'|'.$ContentType])){ |
|
71 |
+ $serializerClass = Serialization::$serializers[get_class($ViewModel).'|'.$ContentType]; |
|
72 |
+ } |
|
73 |
+ else if(isset(Serialization::$serializers[$ContentType])){ |
|
74 |
+ $serializerClass = Serialization::$serializers[$ContentType]; |
|
75 |
+ } |
|
76 |
+ |
|
77 |
+ if(isset($serializerClass)){ |
|
78 |
+ $serializer = new $serializerClass(); |
|
79 |
+ return $serializer->Serialize($ViewModel); |
|
80 |
+ } |
|
81 |
+ else{ |
|
82 |
+ return null; |
|
83 |
+ } |
|
84 |
+ } |
|
85 |
+} |
|
0 | 86 |
\ No newline at end of file |