ソースを参照

BitmaskedBehavior correction

euromark 13 年 前
コミット
863f874f5f

+ 11 - 25
Controller/Component/CommonComponent.php

@@ -121,23 +121,6 @@ class CommonComponent extends Component {
 		//$this->_habtmValidation();
 	}
 
-
-	/**
-	 * Clear the Messages.
-	 *
-	 * Created: 12.10.10 16:01
-	 * Updated: 12.10.10 16:01
-	 * @return void
-	 * @access public
-	 * @author deltacahos
-	 */
-	public function shutdown(Controller $Controller) {
-		parent::shutdown($Controller);
-
-		//$this->Session->write('messages', array());
-		//Configure::write('messages', array());
-	}
-
 /*** Important Helper Methods ***/
 
 	/**
@@ -152,15 +135,16 @@ class CommonComponent extends Component {
 
 	//deprecated - use isPosted instead
 	public function isPost() {
+		trigger_error('deprecated - use isPosted()');
 		return $this->Controller->request->is('post') || $this->Controller->request->is('put');
 	}
 
-
 	/**
 	 * Updates FlashMessage SessionContent (to enable unlimited messages of one case)
+	 *
 	 * @param STRING messagestring
 	 * @param STRING class ['error', 'warning', 'success', 'info']
-	 * @return bool $success
+	 * @return void
 	 * 2008-11-06 ms
 	 */
 	public function flashMessage($messagestring, $class = null) {
@@ -176,17 +160,20 @@ class CommonComponent extends Component {
 
 		$old = (array)$this->Session->read('messages');
 		if (isset($old[$class]) && count($old[$class]) > 99) {
-			return false;
+			array_shift($old[$class]);
 		}
 		$old[$class][] = $messagestring;
 		$this->Session->write('messages', $old);
-		return true;
 	}
 
 	/**
 	 * flashMessages that are not saved (only for current view)
-	 * @return bool $success
-	 * @static
+	 * will be merged into the session flash ones prior to output
+	 *
+	 * @param STRING messagestring
+	 * @param STRING class ['error', 'warning', 'success', 'info']
+	 * @return void
+	 * @access static
 	 * 2010-05-01 ms
 	 */
 	public static function transientFlashMessage($messagestring, $class = null) {
@@ -202,11 +189,10 @@ class CommonComponent extends Component {
 
 		$old = (array)Configure::read('messages');
 		if (isset($old[$class]) && count($old[$class]) > 99) {
-			return false;
+			array_shift($old[$class]);
 		}
 		$old[$class][] = $messagestring;
 		Configure::write('messages', $old);
-		return true;
 	}
 
 

+ 2 - 2
Controller/QloginController.php

@@ -72,7 +72,7 @@ class QloginController extends ToolsAppController {
 	 * 2012-03-04 ms
 	 */
 	public function admin_index() {
-		if ($this->Common->isPost()) {
+		if ($this->Common->isPosted()) {
 			$this->Qlogin->set($this->request->data);
 			if ($this->Qlogin->validates()) {
 				$id = $this->Qlogin->generate($this->Qlogin->data['Qlogin']['url'], $this->Qlogin->data['Qlogin']['user_id']);
@@ -105,7 +105,7 @@ class QloginController extends ToolsAppController {
 	}
 
 	public function admin_reset() {
-		if (!$this->Common->isPost()) {
+		if (!$this->Common->isPosted()) {
 			throw new MethodNotAllowedException();
 		}
 		$this->CodeKey = ClassRegistry::init('Tools.CodeKey');

+ 2 - 2
Controller/TinyUrlsController.php

@@ -60,7 +60,7 @@ class TinyUrlsController extends ToolsAppController {
 	public function admin_index() {
 		//TODO
 
-		if ($this->Common->isPost()) {
+		if ($this->Common->isPosted()) {
 			$this->TinyUrl->set($this->request->data);
 			if ($this->TinyUrl->validates()) {
 				$id = $this->TinyUrl->generate($this->TinyUrl->data['TinyUrl']['url']);
@@ -81,7 +81,7 @@ class TinyUrlsController extends ToolsAppController {
 	}
 
 	public function admin_reset() {
-		if (!$this->Common->isPost()) {
+		if (!$this->Common->isPosted()) {
 			throw new MethodNotAllowedException();
 		}
 		$this->TinyUrl->truncate();

+ 3 - 7
Model/Behavior/BitmaskedBehavior.php

@@ -112,17 +112,13 @@ class BitmaskedBehavior extends ModelBehavior {
 	 */
 	public function decodeBitmask(Model $Model, $value) {
 		$res = array();
-		$i = 0;
-		$value = (int) $value;
-
+		$value = (int)$value;
 		foreach ($this->settings[$Model->alias]['bits'] as $key => $val) {
-			$val = (($value & pow(2, $i)) != 0) ? true : false;
+			$val = (($value & $key) !== 0) ? true : false;
 			if ($val) {
 				$res[] = $key;
 			}
-			$i++;
- 		}
-
+		}
 		return $res;
 	}
 

+ 49 - 32
Model/Behavior/DecimalInputBehavior.php

@@ -1,10 +1,15 @@
 <?php
 App::uses('ModelBehavior', 'Model');
+
 /**
  * //ALREADY exists as number_format in a slightly different way!
  *
+ * IN:
  * 20,01 => 20.01 (!)
- * 11.222 => 11222
+ * 11.222 => 11222 (or 11#222 in strict mode to invalidate correctly)
+ *
+ * OUT:
+ * 20.01 => 20,01
  *
  * @author Mark Scherer
  * @license MIT
@@ -15,8 +20,8 @@ App::uses('ModelBehavior', 'Model');
  */
 class DecimalInputBehavior extends ModelBehavior {
 
-	public $default = array(
-		'before' => 'validate', // safe or validate
+	protected $_defaults = array(
+		'before' => 'validate', // save or validate
 		'input' => true, // true = activated
 		'output' => false, // true = activated
 		'fields' => array(
@@ -32,6 +37,7 @@ class DecimalInputBehavior extends ModelBehavior {
 			//'multiply' => 0
 		),
 		'transformReverse' => array(),
+		'strict' => false,
 	);
 
 	public $delimiterBaseFormat = array();
@@ -43,7 +49,11 @@ class DecimalInputBehavior extends ModelBehavior {
 	* leave fields empty to auto-detect all float inputs
 	*/
 	public function setup(Model $Model, $config = array()) {
-		$this->config[$Model->alias] = $this->default;
+		$this->config[$Model->alias] = $this->_defaults;
+
+		if (!empty($config['strict'])) {
+			$this->config[$Model->alias]['transform']['.'] = '#';
+		}
 		$this->config[$Model->alias] = array_merge($this->config[$Model->alias], $config);
 
 		$numberFields = array();
@@ -55,6 +65,7 @@ class DecimalInputBehavior extends ModelBehavior {
 			}
 		}
 		$this->config[$Model->alias]['fields'] = array_merge($this->config[$Model->alias]['fields'], $numberFields);
+
 		/*
 		if ($this->config[$Model->alias]['localeconv']) {
 			# use locale settings
@@ -64,11 +75,10 @@ class DecimalInputBehavior extends ModelBehavior {
 			$loc = (array)Configure::read('Localization');
 		}
 		*/
+		//TODO: remove to avoid conflicts
 		$this->Model = $Model;
 	}
 
-
-	//Function before save.
 	public function beforeValidate(Model $Model) {
 		if ($this->config[$Model->alias]['before'] != 'validate') {
 			return true;
@@ -78,8 +88,6 @@ class DecimalInputBehavior extends ModelBehavior {
 		return true;
 	}
 
-
-	//Function before save.
 	public function beforeSave(Model $Model) {
 		if ($this->config[$Model->alias]['before'] != 'save') {
 			return true;
@@ -89,7 +97,6 @@ class DecimalInputBehavior extends ModelBehavior {
 		return true;
 	}
 
-
 	public function afterFind(Model $Model, $results) {
 		if (!$this->config[$Model->alias]['output'] || empty($results)) {
 			return $results;
@@ -99,15 +106,22 @@ class DecimalInputBehavior extends ModelBehavior {
 		return $results;
 	}
 
-
+	/**
+	 * @param array $results (by reference)
+	 * @return void
+	 */
 	public function prepInput(&$data) {
 		foreach ($data[$this->Model->alias] as $key => $field) {
 			if (in_array($key, $this->config[$this->Model->alias]['fields'])) {
-				$data[$this->Model->alias][$key] = $this->_format($field, 'in');
+				$data[$this->Model->alias][$key] = $this->formatInputOutput(null, $field, 'in');
 			}
 		}
 	}
 
+	/**
+	 * @param array $results
+	 * @return array $results
+	 */
 	public function prepOutput($data) {
 		foreach ($data as $datakey => $record) {
 			if (!isset($record[$this->Model->alias])) {
@@ -115,26 +129,35 @@ class DecimalInputBehavior extends ModelBehavior {
 			}
 			foreach ($record[$this->Model->alias] as $key => $field) {
 				if (in_array($key, $this->config[$this->Model->alias]['fields'])) {
-					$data[$datakey][$this->Model->alias][$key] = $this->_format($field, 'out');
+					$data[$datakey][$this->Model->alias][$key] = $this->formatInputOutput(null, $field, 'out');
 				}
 			}
 		}
 		return $data;
 	}
 
-
-	protected function _format($value, $dir = 'in') {
+	/**
+	 * perform a single transformation
+	 * @return string $cleanedValue
+	 */
+	public function formatInputOutput(Model $model = null, $value, $dir = 'in') {
 		$this->_setTransformations($dir);
 		if ($dir == 'out') {
 			$value = str_replace($this->delimiterFromFormat, $this->delimiterBaseFormat, (String)$value);
 		} else {
 			$value = str_replace(' ', '', $value);
-			$value = (float)str_replace($this->delimiterFromFormat, $this->delimiterBaseFormat, $value);
+			$value = str_replace($this->delimiterFromFormat, $this->delimiterBaseFormat, $value);
+			if (is_numeric($value)) {
+				$value = (float)$value;
+			}
 		}
 		return $value;
 	}
 
-
+	/**
+	 * prep the transformation chars
+	 * @return void
+	 */
 	protected function _setTransformations($dir) {
 		$from = array();
 		$base = array();
@@ -146,7 +169,16 @@ class DecimalInputBehavior extends ModelBehavior {
 				$transform = array_reverse($transform, true);
 			}
 		}
+		$first = true;
 		foreach ($transform as $key => $value) {
+			/*
+			if ($first) {
+				$from[] = $key;
+				$base[] = '#';
+				$key = '#';
+				$first = false;
+			}
+			*/
 			$from[] = $key;
 			$base[] = $value;
 		}
@@ -160,19 +192,4 @@ class DecimalInputBehavior extends ModelBehavior {
 		}
 	}
 
-
-/*
-beforeValidate
-$Model->data[$Model->alias][$field] = str_replace($loc['decimal_point'], "#", $Model->data[$Model->alias][$field]);
-$Model->data[$Model->alias][$field] = str_replace($loc['thousands_sep'], "", $Model->data[$Model->alias][$field]);
-$Model->data[$Model->alias][$field] = str_replace("#", ".", $Model->data[$Model->alias][$field]);
-
-afterFind
-$m[$Model->alias][$field] = str_replace('.', '#', $m[$Model->alias][$field]);
-$m[$Model->alias][$field] = str_replace(',', $loc['thousands_sep'], $m[$Model->alias][$field]);
-$m[$Model->alias][$field] = str_replace('#', $loc['decimal_point'], $m[$Model->alias][$field]);
-*/
-
-
-}
-
+}

+ 3 - 0
Model/Behavior/KeyValueBehavior.php

@@ -86,6 +86,9 @@ class KeyValueBehavior extends ModelBehavior {
 		if ($section === null) {
 			return $detailArray;
 		}
+		if (empty($detailArray[$section])) {
+			return array();
+		}
 		if ($key === null) {
 			return $detailArray[$section];
 		}

+ 20 - 4
Test/Case/Model/Behavior/DecimalInputBehaviorTest.php

@@ -1,10 +1,8 @@
 <?php
 
-App::import('Behavior', 'Tools.DecimalInput');
-App::uses('AppModel', 'Model');
+App::uses('DecimalInputBehavior', 'Tools.Model/Behavior');
 App::uses('MyCakeTestCase', 'Tools.Lib');
 
-
 class DecimalInputBehaviorTest extends MyCakeTestCase {
 
 	public function startTest() {
@@ -81,14 +79,32 @@ class DecimalInputBehaviorTest extends MyCakeTestCase {
 		$res = $this->Comment->find('count', array());
 		echo returns($res);
 		$this->assertSame($res[0][0]['count'], 2);
+	}
+
+	public function testStrict() {
+		$this->Comment->Behaviors->detach('DecimalInput');
+		$this->Comment->Behaviors->attach('Tools.DecimalInput', array('fields'=>array('rel_rate', 'set_rate'), 'strict'=>true));
+
+		$data = array(
+			'name' => 'some Name',
+			'set_rate' => '0.1',
+			'rel_rate' => '-0,02',
+		);
+		$this->Comment->set($data);
+		$res = $this->Comment->validates();
+		$this->assertTrue($res);
 
+		$res = $this->Comment->data;
+		echo returns($res);
+		$this->assertSame($res['TestModel']['set_rate'], '0#1');
+		$this->assertSame($res['TestModel']['rel_rate'], -0.02);
 	}
 
 }
 
 /** other files **/
 
-class DecimalInputTestModel extends AppModel {
+class DecimalInputTestModel extends CakeTestModel {
 
 	public $alias = 'TestModel';