Browse Source

Documentation.

euromark 12 years ago
parent
commit
689efa9037
3 changed files with 46 additions and 33 deletions
  1. 11 8
      Lib/Utility/FileLib.php
  2. 22 20
      Model/Behavior/SoftDeleteBehavior.php
  3. 13 5
      Test/Case/Lib/Utility/FileLibTest.php

+ 11 - 8
Lib/Utility/FileLib.php

@@ -45,9 +45,10 @@ class FileLib extends File {
 	 * @param string $mode
 	 * @param string $force Force open/read the file
 	 * @param boolean $removeEmpty Remove empty lines (simple newline characters without meaning)
+	 * @param boolean $encode Encode to UTF-8
 	 * @return array Content or false on failure
 	 */
-	public function readCsv($length = 0, $delimiter = null, $enclosure = null, $mode = 'rb', $force = false, $removeEmpty = false) {
+	public function readCsv($length = 0, $delimiter = null, $enclosure = null, $mode = 'rb', $force = false, $removeEmpty = false, $encode = true) {
 		$res = array();
 		if ($this->open($mode, $force) === false) {
 			return false;
@@ -67,7 +68,7 @@ class FileLib extends File {
 				$count++;
 				$tmp = fgets($this->handle, 8000);
 				$tmp = explode($delimiter, $tmp);
-				if (true || WINDOWS) {
+				if ($encode) {
 					$tmp = $this->_encode($tmp);
 				}
 				$isEmpty = true;
@@ -89,7 +90,7 @@ class FileLib extends File {
 				if ($data === false) {
 					break;
 				}
-				if (true || WINDOWS) {
+				if ($encode) {
 					$data = $this->_encode($data);
 				}
 				$isEmpty = true;
@@ -224,7 +225,7 @@ class FileLib extends File {
 	 * @param options
 	 * - keys (defaults to first array content in data otherwise) (order is important!)
 	 * - preserve_keys (do not slug and lowercase)
-	 * @return array result or FALSE on failure
+	 * @return array Result
 	 */
 	public function transfer($data, $options = array()) {
 		$res = array();
@@ -260,11 +261,13 @@ class FileLib extends File {
 			}
 			if (is_array($value)) {
 				$value = $this->_encode($value);
+			} else {
+				if (!mb_check_encoding($value, 'UTF-8')) {
+					$value = utf8_encode($value);
+				}
+				$value = trim($value);
 			}
-			if (!mb_check_encoding($value, 'UTF-8')) {
-				$value = utf8_encode($value);
-			}
-			$convertedArray[$key] = trim($value);
+			$convertedArray[$key] = $value;
 		}
 		return $convertedArray;
 	}

+ 22 - 20
Model/Behavior/SoftDeleteBehavior.php

@@ -50,8 +50,9 @@ class SoftDeleteBehavior extends ModelBehavior {
 	/**
 	 * Setup callback
 	 *
-	 * @param object $model
+	 * @param Model $model
 	 * @param array $settings
+	 * @return void
 	 */
 	public function setup(Model $model, $settings = array()) {
 		$settings = array_merge($this->_defaults, $settings);
@@ -80,7 +81,7 @@ class SoftDeleteBehavior extends ModelBehavior {
 	/**
 	 * Before find callback
 	 *
-	 * @param object $model
+	 * @param Model $model
 	 * @param array $query
 	 * @return array
 	 */
@@ -112,9 +113,9 @@ class SoftDeleteBehavior extends ModelBehavior {
 	/**
 	 * Before delete callback
 	 *
-	 * @param object $model
+	 * @param Model $model
 	 * @param array $query
-	 * @return boolean
+	 * @return boolean Success
 	 */
 	public function beforeDelete(Model $model, $cascade = true) {
 		$runtime = $this->runtime[$model->alias];
@@ -131,9 +132,9 @@ class SoftDeleteBehavior extends ModelBehavior {
 	/**
 	 * Mark record as deleted
 	 *
-	 * @param object $model
+	 * @param Model $model
 	 * @param integer $id
-	 * @return boolean
+	 * @return boolean Success
 	 */
 	public function delete(Model $model, $id) {
 		$runtime = $this->runtime[$model->alias];
@@ -154,15 +155,15 @@ class SoftDeleteBehavior extends ModelBehavior {
 
 		$model->create();
 		$model->set($model->primaryKey, $id);
-		return $model->save(array($model->alias => $data), false, array_keys($data));
+		return (bool)$model->save(array($model->alias => $data), false, array_keys($data));
 	}
 
 	/**
 	 * Mark record as not deleted
 	 *
-	 * @param object $model
+	 * @param Model $model
 	 * @param integer $id
-	 * @return boolean
+	 * @return boolean Success
 	 */
 	public function undelete(Model $model, $id) {
 		$runtime = $this->runtime[$model->alias];
@@ -198,13 +199,13 @@ class SoftDeleteBehavior extends ModelBehavior {
 	 * $this->softDelete(true); enable again for all flag fields
 	 * $config = $this->softDelete(null); for obtaining current setting
 	 *
-	 * @param object $model
+	 * @param Model $model
 	 * @param mixed $active
-	 * @return mixed if $active is null, then current setting/null, or boolean if runtime setting for model was changed
+	 * @return mixed If $active is null, then current setting/null, or boolean if runtime setting for model was changed
 	 */
 	public function softDelete(Model $model, $active) {
 		if ($active === null) {
-			return !empty($this->runtime[$model->alias]) ? $this->runtime[$model->alias] : null;
+			return isset($this->runtime[$model->alias]) ? $this->runtime[$model->alias] : null;
 		}
 
 		$result = !isset($this->runtime[$model->alias]) || $this->runtime[$model->alias] !== $active;
@@ -216,7 +217,7 @@ class SoftDeleteBehavior extends ModelBehavior {
 	/**
 	 * Returns number of outdated softdeleted records prepared for purge
 	 *
-	 * @param object $model
+	 * @param Model $model
 	 * @param mixed $expiration anything parseable by strtotime(), by default '-90 days'
 	 * @return integer
 	 */
@@ -228,9 +229,9 @@ class SoftDeleteBehavior extends ModelBehavior {
 	/**
 	 * Purge table
 	 *
-	 * @param object $model
+	 * @param Model $model
 	 * @param mixed $expiration anything parseable by strtotime(), by default '-90 days'
-	 * @return boolean if there were some outdated records
+	 * @return boolean If there were some outdated records
 	 */
 	public function purgeDeleted(Model $model, $expiration = '-90 days') {
 		$this->softDelete($model, false);
@@ -250,7 +251,7 @@ class SoftDeleteBehavior extends ModelBehavior {
 	/**
 	 * Returns conditions for finding outdated records
 	 *
-	 * @param object $model
+	 * @param Model $model
 	 * @param mixed $expiration anything parseable by strtotime(), by default '-90 days'
 	 * @return array
 	 */
@@ -269,7 +270,7 @@ class SoftDeleteBehavior extends ModelBehavior {
 	/**
 	 * Return normalized field array
 	 *
-	 * @param object $model
+	 * @param Model $model
 	 * @param array $settings
 	 * @return array
 	 */
@@ -289,13 +290,14 @@ class SoftDeleteBehavior extends ModelBehavior {
 	}
 
 	/**
-	 * Modifies conditions of hasOne and hasMany associations
+	 * Modifies conditions of hasOne and hasMany associations.
 	 *
 	 * If multiple delete flags are configured for model, then $active=true doesn't
 	 * do anything - you have to alter conditions in association definition
 	 *
-	 * @param object $model
+	 * @param Model $model
 	 * @param mixed $active
+	 * @return void
 	 */
 	protected function _softDeleteAssociations(Model $model, $active) {
 		if (empty($model->belongsTo)) {
@@ -326,7 +328,7 @@ class SoftDeleteBehavior extends ModelBehavior {
 						if ($active) {
 							if (!isset($conditions[$field]) && !isset($conditions[$assoc . '.' . $field])) {
 								if (is_string($active)) {
-									if ($field == $active) {
+									if ($field === $active) {
 										$conditions[$assoc . '.' . $field] = false;
 									} elseif (isset($conditions[$assoc . '.' . $field])) {
 										unset($conditions[$assoc . '.' . $field]);

+ 13 - 5
Test/Case/Lib/Utility/FileLibTest.php

@@ -34,10 +34,15 @@ class FileLibTest extends CakeTestCase {
 		$this->_printArrays($status, $is, $expected, $pre);
 	}
 
+	/**
+	 * FileLibTest::testReadCsv2() with umlauts
+	 *
+	 * @return void
+	 */
 	public function testReadCsv2() {
 		$handler = new FileLib(TMP . 'test.txt', true);
 
-		$pre = '\'First\', \'Last Name\', \'Email\'' . NL . '\'Example\', \'Firsty\', \'test@test.com\''; //.NL.'\'Next\', \'Secondy\', \'again@test.com\''
+		$pre = '\'First\', \'Last Name\', \'Email\'' . NL . '\'Example Äs\', \'Firsty üs\', \'test@test.com sß\'';
 
 		$handler->write($pre);
 		$handler->close();
@@ -48,10 +53,13 @@ class FileLibTest extends CakeTestCase {
 		$expected = array(array(
 				'First',
 				'Last Name',
-				'Email'), array(
-				'Example',
-				'Firsty',
-				'test@test.com'));
+				'Email'
+			), array(
+				'Example Äs',
+				'Firsty üs',
+				'test@test.com sß'
+			)
+		);
 
 		$status = $this->assertEquals($expected, $is);
 		$this->_printArrays($status, $is, $expected, $pre);