示例#1
0
 /**
  * 
  * @param string $className
  * @param ReflectionProperty|string $property
  * @return \PopCode\Framework\Form\FormType
  */
 public static function getFormType($className, $property)
 {
     if (!$property instanceof ReflectionProperty) {
         $property = new ReflectionProperty($className, $property);
     }
     //read the form type from annotation
     $formType = static::$annotationReader->getPropertyAnnotation($property, 'PopCode\\Framework\\Form\\FormType');
     //override formType from model method
     $reflection = new ReflectionClass($className);
     if ($reflection->implementsInterface('PopCode\\Framework\\Model\\FormConfigInterface')) {
         $reflection->getMethod('getFormType')->invoke(new $className(), $property->getName(), $formType);
     }
     if (!$formType) {
         return;
     }
     //Auto group ?
     if ($formType->getGroup() !== false) {
         $formType->setGroup(true);
     }
     //Auto label ?
     if ($formType->getLabel() === null) {
         //translate label here ?
         $formType->setLabel(Helper::stringToLabel($property->getName()));
     }
     //themings @todo moove this part with template render
     $formType->setLabelAttrs(array('class' => 'control-label'));
     //merge the attrs with predefined data to get the form-control class
     if ($formType->getType() !== 'radio' && $formType->getType() !== 'checkbox') {
         $attrs = $formType->getAttrs();
         $formType->setAttrs(array_merge(array('class' => 'form-control'), $attrs));
     }
     if ($formType->getType() != 'radio' && $formType->getType() != 'checkbox') {
         $attrs = $formType->getAttrs();
         $formType->setAttrs(array_merge(array('class' => 'form-control'), $attrs));
     }
     //set the classes on container to form-group
     $classes = $formType->getClasses();
     $classes = trim(preg_replace('/form-group/', '', $classes) . ' form-group');
     $formType->setClasses($classes);
     return $formType;
 }
示例#2
0
 /**
  * Hydrate an object from an array
  * 
  * @param type $object
  * @param type $data
  */
 public function hydrateObject($object, $data)
 {
     foreach (Helper::keysToCamelCase($data) as $key => $data) {
         if ($this->reflection->hasProperty($key)) {
             $this->hydrate($object, $key, $data);
         }
     }
 }
示例#3
0
 /**
  * Get the request submited to tthe form from the global request
  * 
  * @param array $request
  * @return mixed
  */
 public function getFormRequest($request)
 {
     if (!is_array($request)) {
         return $request;
     }
     if (!$this->hasRequest($request)) {
         return null;
     }
     return Helper::keysToCamelCase($request[$this->id]);
 }
 /**
  * Generate a shortcode from the Model object.
  *
  * @param string $slug
  * @param Model $object
  *
  * @return string
  */
 public static function shortCodeGenerator($slug, Model $object)
 {
     $reflector = new ReflectionClass(get_class($object));
     $shortcodeTmp = '';
     foreach ($reflector->getProperties() as $property) {
         $method = 'get' . ucfirst($property->getName());
         $value = null;
         if ($reflector->hasMethod($method)) {
             $value = $reflector->getMethod($method)->invoke($object);
             if ($value === false) {
                 $value = '0';
             }
             if (is_array($value)) {
                 $value = implode(',', $value);
             }
             if ($value != null) {
                 $shortcodeTmp .= Helper::ccToUnderscore($property->getName()) . '="' . $value . '" ';
             }
         }
     }
     $shortcode = '[' . $slug . ' ' . rtrim($shortcodeTmp) . ']';
     return $shortcode;
 }