Changeset 11961
- Timestamp:
- 10/05/08 20:08:57 (5 years ago)
- Files:
-
- branches/1.1/lib/widget/sfWidgetFormSchema.class.php (modified) (5 diffs)
- branches/1.1/lib/widget/sfWidgetFormSchemaFormatter.class.php (modified) (15 diffs)
- branches/1.1/test/unit/widget/sfWidgetFormSchemaTest.php (modified) (2 diffs)
- branches/1.2/lib/widget/sfWidgetFormSchema.class.php (modified) (5 diffs)
- branches/1.2/lib/widget/sfWidgetFormSchemaFormatter.class.php (modified) (15 diffs)
- branches/1.2/test/unit/widget/sfWidgetFormSchemaTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/lib/widget/sfWidgetFormSchema.class.php
r11509 r11961 26 26 BEFORE = 'before', 27 27 AFTER = 'after'; 28 28 29 29 protected static 30 30 $defaultFormatterName = 'table'; … … 103 103 return $this->formFormatters; 104 104 } 105 106 /** 107 * Sets the generic default formatter name used by the class. If you want all 108 * of your forms to be generated with the <code>list</code> format, you can 105 106 /** 107 * Sets the generic default formatter name used by the class. If you want all 108 * of your forms to be generated with the <code>list</code> format, you can 109 109 * do it in a project or application configuration class: 110 * 110 * 111 111 * <pre> 112 112 * class ProjectConfiguration extends sfProjectConfiguration … … 117 117 * } 118 118 * } 119 * </pre> 119 * </pre> 120 120 * 121 121 * @param string $name New default formatter name … … 160 160 { 161 161 $class = 'sfWidgetFormSchemaFormatter'.ucfirst($name); 162 162 163 163 if (!class_exists($class)) 164 164 { 165 165 throw new InvalidArgumentException(sprintf('The form formatter "%s" does not exist.', $name)); 166 166 } 167 167 168 168 $this->formFormatters[$name] = new $class($this); 169 169 } 170 170 171 171 return $this->formFormatters[$name]; 172 172 } … … 676 676 $this[$name] = $field; 677 677 } 678 679 foreach ($this->formFormatters as &$formFormatter) 680 { 681 $formFormatter = clone $formFormatter; 682 $formFormatter->setWidgetSchema($this); 683 } 678 684 } 679 685 } branches/1.1/lib/widget/sfWidgetFormSchemaFormatter.class.php
r11509 r11961 19 19 abstract class sfWidgetFormSchemaFormatter 20 20 { 21 protected static 21 protected static 22 22 $translationCallable = null; 23 23 24 24 protected 25 25 $rowFormat = '', … … 40 40 public function __construct(sfWidgetFormSchema $widgetSchema) 41 41 { 42 $this-> widgetSchema = $widgetSchema;43 } 44 42 $this->setWidgetSchema($widgetSchema); 43 } 44 45 45 public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null) 46 46 { … … 53 53 )); 54 54 } 55 55 56 56 /** 57 57 * Translates a string using an i18n callable, if it has been provided … … 65 65 if (false === $subject) 66 66 { 67 return false; 67 return false; 68 68 } 69 69 … … 81 81 return strtr($subject, $parameters); 82 82 } 83 83 84 84 $catalogue = $this->getTranslationCatalogue(); 85 85 … … 101 101 return self::$translationCallable; 102 102 } 103 104 /** 105 * Sets a callable which aims to translate form labels, errors and help messages 103 104 /** 105 * Sets a callable which aims to translate form labels, errors and help messages 106 106 * 107 107 * @param mixed $callable 108 * 108 * 109 109 * @throws InvalidArgumentException if an invalid php callable or sfCallable has been provided 110 110 */ … … 115 115 throw new InvalidArgumentException('Provided i18n callable should be either an instance of sfCallable or a valid PHP callable'); 116 116 } 117 117 118 118 self::$translationCallable = $callable; 119 119 } 120 120 121 121 public function formatHelp($help) 122 122 { … … 153 153 return strtr($this->getErrorListFormatInARow(), array('%errors%' => implode('', $this->unnestErrors($errors)))); 154 154 } 155 155 156 156 /** 157 157 * Generates a label for the given field name. … … 170 170 return ''; 171 171 } 172 172 173 173 $widgetId = $this->widgetSchema->generateId($this->widgetSchema->generateName($name)); 174 174 $attributes = array_merge($attributes, array('for' => $widgetId)); 175 175 176 176 return $this->widgetSchema->renderContentTag('label', $labelName, $attributes); 177 177 } … … 187 187 { 188 188 $label = $this->widgetSchema->getLabel($name); 189 189 190 190 if (!$label && false !== $label) 191 191 { … … 195 195 return $this->translate($label); 196 196 } 197 197 198 198 /** 199 199 * Get i18n catalogue name … … 205 205 return $this->translationCatalogue; 206 206 } 207 207 208 208 /** 209 209 * Set an i18n catalogue name … … 218 218 throw new InvalidArgumentException('Catalogue name must be a string'); 219 219 } 220 220 221 221 $this->translationCatalogue = $catalogue; 222 222 } 223 223 224 224 protected function unnestErrors($errors, $prefix = '') 225 225 { … … 256 256 return $newErrors; 257 257 } 258 258 259 259 public function setRowFormat($format) 260 260 { … … 326 326 return $this->helpFormat; 327 327 } 328 329 /** 330 * Sets the widget schema associated with this formatter instance. 331 * 332 * @param sfWidgetFormSchema $widgetSchema A sfWidgetFormSchema instance 333 */ 334 public function setWidgetSchema(sfWidgetFormSchema $widgetSchema) 335 { 336 $this->widgetSchema = $widgetSchema; 337 } 338 339 public function getWidgetSchema() 340 { 341 return $this->widgetSchema; 342 } 328 343 } branches/1.1/test/unit/widget/sfWidgetFormSchemaTest.php
r11509 r11961 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test( 61, new lime_output_color());13 $t = new lime_test(70, new lime_output_color()); 14 14 15 15 $w1 = new sfWidgetFormInput(array(), array('class' => 'foo1')); … … 359 359 } 360 360 361 $w = new sfWidgetFormSchema(); 362 $w->addFormFormatter('table', new sfWidgetFormSchemaFormatterTable($w)); 363 $w->addFormFormatter('list', new sfWidgetFormSchemaFormatterList($w)); 364 $w1 = clone $w; 365 $f1 = $w1->getFormFormatters(); 366 $f = $w->getFormFormatters(); 367 $t->is(array_keys($f1), array_keys($f), '__clone() clones form formatters'); 368 foreach ($f1 as $key => $formFormatter) 369 { 370 $t->ok($formFormatter !== $f[$key], '__clone() clones form formatters'); 371 $t->is(get_class($formFormatter), get_class($f[$key]), '__clone() clones form formatters'); 372 373 $t->ok($formFormatter->getWidgetSchema() !== $f[$key]->getWidgetSchema(), '__clone() clones form formatters'); 374 $t->is(get_class($formFormatter->getWidgetSchema()), get_class($f[$key]->getWidgetSchema()), '__clone() clones form formatters'); 375 } 376 361 377 // setDefaultFormFormatterName() 362 378 $t->diag('setDefaultFormFormatterName()'); branches/1.2/lib/widget/sfWidgetFormSchema.class.php
r11362 r11961 26 26 BEFORE = 'before', 27 27 AFTER = 'after'; 28 28 29 29 protected static 30 30 $defaultFormatterName = 'table'; … … 103 103 return $this->formFormatters; 104 104 } 105 106 /** 107 * Sets the generic default formatter name used by the class. If you want all 108 * of your forms to be generated with the <code>list</code> format, you can 105 106 /** 107 * Sets the generic default formatter name used by the class. If you want all 108 * of your forms to be generated with the <code>list</code> format, you can 109 109 * do it in a project or application configuration class: 110 * 110 * 111 111 * <pre> 112 112 * class ProjectConfiguration extends sfProjectConfiguration … … 117 117 * } 118 118 * } 119 * </pre> 119 * </pre> 120 120 * 121 121 * @param string $name New default formatter name … … 160 160 { 161 161 $class = 'sfWidgetFormSchemaFormatter'.ucfirst($name); 162 162 163 163 if (!class_exists($class)) 164 164 { 165 165 throw new InvalidArgumentException(sprintf('The form formatter "%s" does not exist.', $name)); 166 166 } 167 167 168 168 $this->formFormatters[$name] = new $class($this); 169 169 } 170 170 171 171 return $this->formFormatters[$name]; 172 172 } … … 681 681 $formFormatter->setWidgetSchema($this); 682 682 } 683 684 foreach ($this->formFormatters as &$formFormatter) 685 { 686 $formFormatter = clone $formFormatter; 687 $formFormatter->setWidgetSchema($this); 688 } 683 689 } 684 690 } branches/1.2/lib/widget/sfWidgetFormSchemaFormatter.class.php
r11362 r11961 19 19 abstract class sfWidgetFormSchemaFormatter 20 20 { 21 protected static 21 protected static 22 22 $translationCallable = null; 23 23 24 24 protected 25 25 $rowFormat = '', … … 42 42 $this->setWidgetSchema($widgetSchema); 43 43 } 44 44 45 45 public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null) 46 46 { … … 53 53 )); 54 54 } 55 55 56 56 /** 57 57 * Translates a string using an i18n callable, if it has been provided … … 65 65 if (false === $subject) 66 66 { 67 return false; 67 return false; 68 68 } 69 69 … … 81 81 return strtr($subject, $parameters); 82 82 } 83 83 84 84 $catalogue = $this->getTranslationCatalogue(); 85 85 … … 101 101 return self::$translationCallable; 102 102 } 103 104 /** 105 * Sets a callable which aims to translate form labels, errors and help messages 103 104 /** 105 * Sets a callable which aims to translate form labels, errors and help messages 106 106 * 107 107 * @param mixed $callable 108 * 108 * 109 109 * @throws InvalidArgumentException if an invalid php callable or sfCallable has been provided 110 110 */ … … 115 115 throw new InvalidArgumentException('Provided i18n callable should be either an instance of sfCallable or a valid PHP callable'); 116 116 } 117 117 118 118 self::$translationCallable = $callable; 119 119 } 120 120 121 121 public function formatHelp($help) 122 122 { … … 153 153 return strtr($this->getErrorListFormatInARow(), array('%errors%' => implode('', $this->unnestErrors($errors)))); 154 154 } 155 155 156 156 /** 157 157 * Generates a label for the given field name. … … 170 170 return ''; 171 171 } 172 172 173 173 $widgetId = $this->widgetSchema->generateId($this->widgetSchema->generateName($name)); 174 174 $attributes = array_merge($attributes, array('for' => $widgetId)); 175 175 176 176 return $this->widgetSchema->renderContentTag('label', $labelName, $attributes); 177 177 } … … 187 187 { 188 188 $label = $this->widgetSchema->getLabel($name); 189 189 190 190 if (!$label && false !== $label) 191 191 { … … 195 195 return $this->translate($label); 196 196 } 197 197 198 198 /** 199 199 * Get i18n catalogue name … … 205 205 return $this->translationCatalogue; 206 206 } 207 207 208 208 /** 209 209 * Set an i18n catalogue name … … 218 218 throw new InvalidArgumentException('Catalogue name must be a string'); 219 219 } 220 220 221 221 $this->translationCatalogue = $catalogue; 222 222 } 223 223 224 224 protected function unnestErrors($errors, $prefix = '') 225 225 { … … 256 256 return $newErrors; 257 257 } 258 258 259 259 public function setRowFormat($format) 260 260 { … … 327 327 } 328 328 329 public function setWidgetSchema($widgetSchema) 329 /** 330 * Sets the widget schema associated with this formatter instance. 331 * 332 * @param sfWidgetFormSchema $widgetSchema A sfWidgetFormSchema instance 333 */ 334 public function setWidgetSchema(sfWidgetFormSchema $widgetSchema) 330 335 { 331 336 $this->widgetSchema = $widgetSchema; 332 337 } 338 339 public function getWidgetSchema() 340 { 341 return $this->widgetSchema; 342 } 333 343 } branches/1.2/test/unit/widget/sfWidgetFormSchemaTest.php
r11362 r11961 11 11 require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 12 12 13 $t = new lime_test( 63, new lime_output_color());13 $t = new lime_test(72, new lime_output_color()); 14 14 15 15 $w1 = new sfWidgetFormInput(array(), array('class' => 'foo1')); … … 366 366 $t->is($formatters['testFormatter']->getTranslationCatalogue(), 'english', '__clone() clones formatters, so that changes to the original one have no effect to the cloned formatter.'); 367 367 368 $w = new sfWidgetFormSchema(); 369 $w->addFormFormatter('table', new sfWidgetFormSchemaFormatterTable($w)); 370 $w->addFormFormatter('list', new sfWidgetFormSchemaFormatterList($w)); 371 $w1 = clone $w; 372 $f1 = $w1->getFormFormatters(); 373 $f = $w->getFormFormatters(); 374 $t->is(array_keys($f1), array_keys($f), '__clone() clones form formatters'); 375 foreach ($f1 as $key => $formFormatter) 376 { 377 $t->ok($formFormatter !== $f[$key], '__clone() clones form formatters'); 378 $t->is(get_class($formFormatter), get_class($f[$key]), '__clone() clones form formatters'); 379 380 $t->ok($formFormatter->getWidgetSchema() !== $f[$key]->getWidgetSchema(), '__clone() clones form formatters'); 381 $t->is(get_class($formFormatter->getWidgetSchema()), get_class($f[$key]->getWidgetSchema()), '__clone() clones form formatters'); 382 } 383 368 384 // setDefaultFormFormatterName() 369 385 $t->diag('setDefaultFormFormatterName()');