src/Serialization.php
53e0de50
 <?php
 
 namespace elanpl\L3;
 
 class Serialization{
     protected static $serializers; //registered serializers dictionary
 
     public function __construct()
     {
         if(!isset(self::$serializers)) self::$serializers = array(); 
     }
 
c6173b1c
     public static function register($contentType, $serializer, $viewModelClass = null){
         if(isset($viewModelClass) && $viewModelClass!=''){
             $class_key = $viewModelClass.'|';
53e0de50
         }
         else{
             $class_key = '';
         }
c6173b1c
         self::$serializers[$class_key.$contentType] = $serializer;
53e0de50
     }
 
c6173b1c
     public function match($acceptTypes, $viewModel=null){
         if(isset($viewModel)){
             if(is_object($viewModel)){
                 $viewModelClass = get_class($viewModel);
53e0de50
             }
c6173b1c
             if(is_string($viewModel)){
                 $viewModelClass = $viewModel;
53e0de50
             }
         }
 
c6173b1c
         if(is_array($acceptTypes)){
             $registeredTypes = array_keys(self::$serializers);
53e0de50
 
c6173b1c
             foreach ($acceptTypes as $type){
53e0de50
                 //Dedicated config for ViewModel class first...
c6173b1c
                 if(array_key_exists($viewModelClass."|".$type, self::$serializers)){
53e0de50
                     return $type;
                 }
c6173b1c
                 foreach($registeredTypes as $rtype_with_class){
53e0de50
                     $rtype_parts = explode("|", $rtype_with_class);
                     if(count($rtype_parts)==2){
                         $rclass = $rtype_parts[0];
                         $t = explode("/", $type);
                         $rt = explode("/", $rtype_parts[1]);
c6173b1c
                         if($rclass==$viewModelClass && ($t[0]=="*" || $rt[0]=="*" || $t[0]==$rt[0]) && ($t[1]=="*" || $rt[1]=="*" || $t[1]==$rt[1])){
53e0de50
                             return $rtype_with_class;
                         }
                     }
                 }
 
                 //Then check configs without the ViewModel class name
                 if(array_key_exists($type, self::$serializers)){
                     return $type;
                 }
c6173b1c
                 foreach($registeredTypes as $rtype){
53e0de50
                     $t = explode("/", $type);
                     $rt = explode("/", $rtype);
                     if(($t[0]=="*" || $rt[0]=="*" || $t[0]==$t[0]) && ($t[1]=="*" || $rt[1]=="*" || $t[1]==$t[1])){
                         return $rtype;
                     }
                 }
             }
         }
         return false;
     }
 
c6173b1c
     public function serialize($contentType, $viewModel){
         if(isset(Serialization::$serializers[get_class($viewModel).'|'.$contentType])){
             $serializerClass = Serialization::$serializers[get_class($viewModel).'|'.$contentType];
53e0de50
         }
c6173b1c
         else if(isset(Serialization::$serializers[$contentType])){
             $serializerClass = Serialization::$serializers[$contentType];
53e0de50
         }
 
         if(isset($serializerClass)){
             $serializer = new $serializerClass();
c6173b1c
             return $serializer->Serialize($viewModel);
53e0de50
         }
         else{
             return null;
         }
     }
 }