浏览代码

Use entity interface and more specific exception.

dereuromark 8 年之前
父节点
当前提交
17fc227010

+ 9 - 7
src/Model/Behavior/BitmaskedBehavior.php

@@ -3,12 +3,14 @@
 namespace Tools\Model\Behavior;
 
 use ArrayObject;
+use Cake\Datasource\EntityInterface;
 use Cake\Event\Event;
 use Cake\ORM\Behavior;
 use Cake\ORM\Entity;
 use Cake\ORM\Query;
 use Cake\Utility\Inflector;
 use Exception;
+use RuntimeException;
 use Tools\Utility\Text;
 
 /**
@@ -68,7 +70,7 @@ class BitmaskedBehavior extends Behavior {
 			$config['bits'] = false;
 		}
 		if (empty($config['bits'])) {
-			throw new Exception('Bits not found');
+			throw new RuntimeException('Bits not found');
 		}
 		ksort($config['bits'], SORT_NUMERIC);
 
@@ -97,11 +99,11 @@ class BitmaskedBehavior extends Behavior {
 
 	/**
 	 * @param \Cake\Event\Event $event
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @param \ArrayObject $options
 	 * @return void
 	 */
-	public function beforeRules(Event $event, Entity $entity, ArrayObject $options) {
+	public function beforeRules(Event $event, EntityInterface $entity, ArrayObject $options) {
 		if ($this->_config['on'] !== 'beforeRules' || !$options['checkRules']) {
 			return;
 		}
@@ -110,11 +112,11 @@ class BitmaskedBehavior extends Behavior {
 
 	/**
 	 * @param \Cake\Event\Event $event
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @param \ArrayObject $options
 	 * @return void
 	 */
-	public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
+	public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
 		if ($this->_config['on'] !== 'beforeSave') {
 			return;
 		}
@@ -190,10 +192,10 @@ class BitmaskedBehavior extends Behavior {
 	}
 
 	/**
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @return void
 	 */
-	public function encodeBitmaskData(Entity $entity) {
+	public function encodeBitmaskData(EntityInterface $entity) {
 		$field = $this->_config['field'];
 		if (!($mappedField = $this->_config['mappedField'])) {
 			$mappedField = $field;

+ 8 - 6
src/Model/Behavior/JsonableBehavior.php

@@ -4,11 +4,13 @@ namespace Tools\Model\Behavior;
 
 use ArrayObject;
 use Cake\Database\Type;
+use Cake\Datasource\EntityInterface;
 use Cake\Event\Event;
 use Cake\ORM\Behavior;
 use Cake\ORM\Entity;
 use Cake\ORM\Query;
 use Exception;
+use RuntimeException;
 use Tools\Database\Type\ArrayType;
 use Tools\Utility\Text;
 
@@ -76,7 +78,7 @@ class JsonableBehavior extends Behavior {
 	 */
 	public function initialize(array $config = []) {
 		if (empty($this->_config['fields'])) {
-			throw new Exception('Fields are required');
+			throw new RuntimeException('Fields are required');
 		}
 		if (!is_array($this->_config['fields'])) {
 			$this->_config['fields'] = (array)$this->_config['fields'];
@@ -85,7 +87,7 @@ class JsonableBehavior extends Behavior {
 			$this->_config['map'] = (array)$this->_config['map'];
 		}
 		if (!empty($this->_config['map']) && count($this->_config['fields']) !== count($this->_config['map'])) {
-			throw new Exception('Fields and Map need to be of the same length if map is specified.');
+			throw new RuntimeException('Fields and Map need to be of the same length if map is specified.');
 		}
 		foreach ($this->_config['fields'] as $field) {
 			$this->_table->schema()->columnType($field, 'array');
@@ -121,10 +123,10 @@ class JsonableBehavior extends Behavior {
 	/**
 	 * Decodes the fields of an array/entity (if the value itself was encoded)
 	 *
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @return void
 	 */
-	public function decodeItems(Entity $entity) {
+	public function decodeItems(EntityInterface $entity) {
 		$fields = $this->_getMappedFields();
 
 		foreach ($fields as $map => $field) {
@@ -139,11 +141,11 @@ class JsonableBehavior extends Behavior {
 	 * Saves all fields that do not belong to the current Model into 'with' helper model.
 	 *
 	 * @param \Cake\Event\Event $event
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @param \ArrayObject $options
 	 * @return void
 	 */
-	public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
+	public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
 		$fields = $this->_getMappedFields();
 
 		foreach ($fields as $map => $field) {

+ 10 - 8
src/Model/Behavior/PasswordableBehavior.php

@@ -5,11 +5,13 @@ namespace Tools\Model\Behavior;
 use ArrayObject;
 use Cake\Auth\PasswordHasherFactory;
 use Cake\Core\Configure;
+use Cake\Datasource\EntityInterface;
 use Cake\Event\Event;
 use Cake\ORM\Behavior;
 use Cake\ORM\Entity;
 use Cake\ORM\Table;
 use Exception;
+use RuntimeException;
 
 if (!defined('PWD_MIN_LENGTH')) {
 	define('PWD_MIN_LENGTH', 6);
@@ -131,7 +133,7 @@ class PasswordableBehavior extends Behavior {
 		$formFieldCurrent = $this->_config['formFieldCurrent'];
 
 		if ($formField === $this->_config['field']) {
-			throw new Exception('Invalid setup - the form field must to be different from the model field (' . $this->_config['field'] . ').');
+			throw new RuntimeException('Invalid setup - the form field must to be different from the model field (' . $this->_config['field'] . ').');
 		}
 
 		$rules = $this->_validationRules;
@@ -276,12 +278,12 @@ class PasswordableBehavior extends Behavior {
 	 * Preparing the data
 	 *
 	 * @param \Cake\Event\Event $event
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @param \ArrayObject $options
 	 * @param string $operation
 	 * @return void
 	 */
-	public function beforeRules(Event $event, Entity $entity, ArrayObject $options, $operation) {
+	public function beforeRules(Event $event, EntityInterface $entity, ArrayObject $options, $operation) {
 		$formField = $this->_config['formField'];
 		$formFieldRepeat = $this->_config['formFieldRepeat'];
 		$formFieldCurrent = $this->_config['formFieldCurrent'];
@@ -313,11 +315,11 @@ class PasswordableBehavior extends Behavior {
 	 * Hashing the password and whitelisting
 	 *
 	 * @param \Cake\Event\Event $event
-	 * @param \Cake\ORM\Entity $entity
-	 * @throws \Exception
-	 * @return void
+	 * @param \Cake\Datasource\EntityInterface $entity
+	 * @param ArrayObject $options
+	 * @throws Exception
 	 */
-	public function beforeSave(Event $event, Entity $entity) {
+	public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
 		$formField = $this->_config['formField'];
 		$field = $this->_config['field'];
 
@@ -329,7 +331,7 @@ class PasswordableBehavior extends Behavior {
 			$entity->set($field, $PasswordHasher->hash($entity->get($formField)));
 
 			if (!$entity->get($field)) {
-				throw new Exception('Empty field');
+				throw new RuntimeException('Empty field');
 			}
 
 			$entity->unsetProperty($formField);

+ 3 - 2
src/Model/Behavior/ResetBehavior.php

@@ -6,6 +6,7 @@ use Cake\Core\Configure;
 use Cake\ORM\Behavior;
 use Cake\ORM\Table;
 use Exception;
+use RuntimeException;
 
 /**
  * Allows the model to reset all records as batch command.
@@ -93,7 +94,7 @@ class ResetBehavior extends Behavior {
 		if (!empty($this->_config['fields'])) {
 			foreach ((array)$this->_config['fields'] as $field) {
 				if (!$this->_table->hasField($field)) {
-					throw new Exception('Table does not have field ' . $field);
+					throw new RuntimeException('Table does not have field ' . $field);
 				}
 			}
 			$defaults['fields'] = array_merge([$this->_table->alias() . '.' . $this->_table->primaryKey()], $this->_config['fields']);
@@ -150,7 +151,7 @@ class ResetBehavior extends Behavior {
 
 				$res = $this->_table->save($record, compact('validate', 'fieldList'));
 				if (!$res) {
-					throw new Exception(print_r($this->_table->errors(), true));
+					throw new RuntimeException(print_r($this->_table->errors(), true));
 				}
 				$modified++;
 			}

+ 22 - 17
src/Model/Behavior/SluggedBehavior.php

@@ -3,6 +3,7 @@
 namespace Tools\Model\Behavior;
 
 use Cake\Core\Configure;
+use Cake\Datasource\EntityInterface;
 use Cake\Event\Event;
 use Cake\ORM\Behavior;
 use Cake\ORM\Entity;
@@ -11,6 +12,7 @@ use Cake\ORM\Table;
 use Cake\Utility\Inflector;
 use Exception;
 use InvalidArgumentException;
+use RuntimeException;
 
 /**
  * SluggedBehavior
@@ -131,11 +133,11 @@ class SluggedBehavior extends Behavior {
 				if (strpos($field, '.')) {
 					list($alias, $field) = explode('.', $field);
 					if (!$this->_table->$alias->hasField($field)) {
-						throw new Exception('(SluggedBehavior::setup) model ' . $this->_table->$alias->name . ' is missing the field ' . $field .
+						throw new RuntimeException('(SluggedBehavior::setup) model ' . $this->_table->$alias->name . ' is missing the field ' . $field .
 							' (specified in the setup for model ' . $this->_table->name . ') ');
 					}
 				} elseif (!$this->_table->hasField($field) && !method_exists($this->_table->entityClass(), '_get' . Inflector::classify($field))) {
-					throw new Exception('(SluggedBehavior::setup) model ' . $this->_table->name . ' is missing the field ' . $field . ' specified in the setup.');
+					throw new RuntimeException('(SluggedBehavior::setup) model ' . $this->_table->name . ' is missing the field ' . $field . ' specified in the setup.');
 				}
 			}
 		}
@@ -161,10 +163,10 @@ class SluggedBehavior extends Behavior {
 	 * SluggedBehavior::beforeRules()
 	 *
 	 * @param \Cake\Event\Event $event
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @return void
 	 */
-	public function beforeRules(Event $event, Entity $entity) {
+	public function beforeRules(Event $event, EntityInterface $entity) {
 		if ($this->_config['on'] === 'beforeRules') {
 			$this->slug($entity);
 		}
@@ -174,10 +176,10 @@ class SluggedBehavior extends Behavior {
 	 * SluggedBehavior::beforeSave()
 	 *
 	 * @param \Cake\Event\Event $event
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @return void
 	 */
-	public function beforeSave(Event $event, Entity $entity) {
+	public function beforeSave(Event $event, EntityInterface $entity) {
 		if ($this->_config['on'] === 'beforeSave') {
 			$this->slug($entity);
 		}
@@ -186,11 +188,11 @@ class SluggedBehavior extends Behavior {
 	/**
 	 * SluggedBehavior::slug()
 	 *
-	 * @param \Cake\ORM\Entity $entity Entity
+	 * @param \Cake\Datasource\EntityInterface $entity Entity
 	 * @param array $options Options
 	 * @return void
 	 */
-	public function slug(Entity $entity, array $options = []) {
+	public function slug(EntityInterface $entity, array $options = []) {
 		$overwrite = isset($options['overwrite']) ? $options['overwrite'] : $this->_config['overwrite'];
 		if (!$overwrite && $entity->get($this->_config['overwriteField'])) {
 			$overwrite = true;
@@ -216,11 +218,11 @@ class SluggedBehavior extends Behavior {
 	 * of maybe some not in sync slugs anymore (saving the same title again,
 	 * but the slug is completely different, for example).
 	 *
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @param bool $deep If true it will generate a new slug and compare it to the currently stored one.
 	 * @return bool
 	 */
-	public function needsSlugUpdate($entity, $deep = false) {
+	public function needsSlugUpdate(EntityInterface $entity, $deep = false) {
 		foreach ((array)$this->_config['label'] as $label) {
 			if ($entity->dirty($label)) {
 				return true;
@@ -245,10 +247,11 @@ class SluggedBehavior extends Behavior {
 	 * until a unique slug is found
 	 *
 	 * @param string $value
-	 * @param \Cake\ORM\Entity|null $entity
+	 * @param \Cake\Datasource\EntityInterface|null $entity
 	 * @return string A slug
+	 * @throws Exception
 	 */
-	public function generateSlug($value, Entity $entity = null) {
+	public function generateSlug($value, EntityInterface $entity = null) {
 		$separator = $this->_config['separator'];
 
 		$string = str_replace(["\r\n", "\r", "\n"], ' ', $value);
@@ -300,7 +303,7 @@ class SluggedBehavior extends Behavior {
 		}
 		if ($this->_config['unique']) {
 			if (!$entity) {
-				throw new Exception('Needs an Entity to work on');
+				throw new RuntimeException('Needs an Entity to work on');
 			}
 			$field = $this->_table->alias() . '.' . $this->_config['field'];
 			$conditions = [$field => $slug];
@@ -339,10 +342,11 @@ class SluggedBehavior extends Behavior {
 	 *
 	 * @param array $params
 	 * @return bool Success
+	 * @throws Exception
 	 */
 	public function resetSlugs($params = []) {
 		if (!$this->_table->hasField($this->_config['field'])) {
-			throw new Exception('Table does not have field ' . $this->_config['field']);
+			throw new RuntimeException('Table does not have field ' . $this->_config['field']);
 		}
 		$defaults = [
 			'page' => 1,
@@ -361,6 +365,7 @@ class SluggedBehavior extends Behavior {
 
 		$this->_table->behaviors()->Slugged->config($params, null, false);
 		while (($records = $this->_table->find('all', $params)->toArray())) {
+			/** @var \Cake\ORM\Entity $record */
 			foreach ($records as $record) {
 				$record->isNew(true);
 				$options = [
@@ -368,7 +373,7 @@ class SluggedBehavior extends Behavior {
 					'fieldList' => array_merge([$this->_table->primaryKey(), $this->_config['field']], $this->_config['label'])
 				];
 				if (!$this->_table->save($record, $options)) {
-					throw new Exception(print_r($this->_table->errors(), true));
+					throw new RuntimeException(print_r($record->errors(), true));
 				}
 			}
 			$params['page']++;
@@ -384,10 +389,10 @@ class SluggedBehavior extends Behavior {
 	 *
 	 * //FIXME
 	 *
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @return void
 	 */
-	protected function _multiSlug(Entity $entity) {
+	protected function _multiSlug(EntityInterface $entity) {
 		$label = $this->config('label');
 		$field = current($label);
 		$fields = (array)$entity->get($field);

+ 5 - 4
src/Model/Behavior/StringBehavior.php

@@ -3,6 +3,7 @@
 namespace Tools\Model\Behavior;
 
 use ArrayObject;
+use Cake\Datasource\EntityInterface;
 use Cake\Datasource\ResultSetInterface;
 use Cake\Event\Event;
 use Cake\ORM\Behavior;
@@ -63,11 +64,11 @@ class StringBehavior extends Behavior {
 	/**
 	 * Decodes the fields of an array/entity (if the value itself was encoded)
 	 *
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @param string $type Type (input/output)
 	 * @return void
 	 */
-	public function processItems(Entity $entity, $type = 'input') {
+	public function processItems(EntityInterface $entity, $type = 'input') {
 		$fields = $this->_config['fields'];
 
 		foreach ($fields as $field => $map) {
@@ -98,11 +99,11 @@ class StringBehavior extends Behavior {
 	 * Saves all fields that do not belong to the current Model into 'with' helper model.
 	 *
 	 * @param \Cake\Event\Event $event
-	 * @param \Cake\ORM\Entity $entity
+	 * @param \Cake\Datasource\EntityInterface $entity
 	 * @param \ArrayObject $options
 	 * @return void
 	 */
-	public function beforeSave(Event $event, Entity $entity, ArrayObject $options) {
+	public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) {
 		$this->processItems($entity, 'input');
 	}