Bail out if the input is not okay.
authorGunnar Wrobel <p@rdus.de>
Wed, 12 Jan 2011 10:36:25 +0000 (11:36 +0100)
committerGunnar Wrobel <p@rdus.de>
Wed, 12 Jan 2011 10:36:25 +0000 (11:36 +0100)
components/lib/Components/Helper/Template/Printf.php
components/test/Components/Unit/Components/Helper/TemplatesTest.php

index 1f26742..3103392 100644 (file)
@@ -37,6 +37,18 @@ extends Components_Helper_Template
      */
     public function write(array $parameters = array())
     {
+        foreach ($parameters as $key => $value) {
+            if (!is_string($value)) {
+                throw new Components_Exception(
+                    sprintf(
+                        'File %s is a printf() based template and requires string input only. Key "%s" however is of type %s!',
+                        $this->_source,
+                        $key,
+                        gettype($value)
+                    )
+                );
+            }
+        }
         $source = file_get_contents($this->_source);
         file_put_contents(
             $this->_target, vsprintf($source, $parameters)
index af7762b..d6282a7 100644 (file)
@@ -89,6 +89,21 @@ extends Components_TestCase
         );
     }
 
+    /**
+     * @expectedException Components_Exception
+     */
+    public function testNoStringInput()
+    {
+        $tdir =  $this->getTemporaryDirectory();
+        $templates = new Components_Helper_Templates_Single(
+            dirname(__FILE__) . '/../../../fixture/templates',
+            $tdir,
+            'variables',
+            'target'
+        );
+        $templates->write(array('1' => new stdClass, '2' => 'Two'));
+    }
+
     public function testPrefix()
     {
         $tdir =  $this->getTemporaryDirectory();