コード例 #1
0
 private function BindModel($annotations)
 {
     $bindingNamespace = null;
     $appConfig = App::getInstance()->getConfig()->app;
     $namespaces = $appConfig['namespaces'];
     foreach ($namespaces as $key => $value) {
         if (strpos($key, "BindingModels")) {
             $bindingNamespace = $key;
         }
     }
     $bindingModelName = null;
     foreach ($annotations as $annotation) {
         $bindingAnnotation = explode(' ', $annotation);
         if ($bindingAnnotation[0] === 'BingingModel') {
             $bindingModelName = $bindingAnnotation[1];
         }
     }
     $bindingModel = null;
     if ($bindingNamespace && $bindingModelName) {
         $bindingModelClass = $bindingNamespace . "\\" . $bindingModelName;
         $bindingModel = new $bindingModelClass();
         $reflectionModel = new ReflectionClass($bindingModel);
         $properties = $reflectionModel->getProperties();
         $post = $this->input->post();
         foreach ($properties as $property) {
             $propertyName = $property->getName();
             $propertyDoc = $property->getDocComment();
             $annotations = array();
             preg_match_all('#@(.*?)\\n#s', $propertyDoc, $annotations);
             $set = 'set' . $propertyName;
             if ($annotations[1][0] === "Required" && !$post[$propertyName]) {
                 throw new \Exception("Field " . $propertyName . " is required");
             }
             if (array_key_exists($propertyName, $post)) {
                 $bindingModel->{$set}($post[$propertyName]);
             } else {
                 throw new \Exception("Field " . $propertyName . " is not accepted");
             }
         }
     }
     return $bindingModel;
 }