From 217e49871674d1e9a3ed2db9d3477ddc5e4efce8 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 16 May 2014 14:32:45 +0200 Subject: [PATCH] Added method `readJSON()` --- .../AbstractDatabaseObjectAction.class.php | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/wcfsetup/install/files/lib/data/AbstractDatabaseObjectAction.class.php b/wcfsetup/install/files/lib/data/AbstractDatabaseObjectAction.class.php index 6ded6170ad..3945b9465e 100644 --- a/wcfsetup/install/files/lib/data/AbstractDatabaseObjectAction.class.php +++ b/wcfsetup/install/files/lib/data/AbstractDatabaseObjectAction.class.php @@ -7,6 +7,7 @@ use wcf\system\exception\UserInputException; use wcf\system\request\RequestHandler; use wcf\system\WCF; use wcf\util\ClassUtil; +use wcf\util\JSON; use wcf\util\StringUtil; /** @@ -96,6 +97,7 @@ abstract class AbstractDatabaseObjectAction implements IDatabaseObjectAction, ID const TYPE_INTEGER = 1; const TYPE_STRING = 2; const TYPE_BOOLEAN = 3; + const TYPE_JSON = 4; /** * Initialize a new DatabaseObject-related action. @@ -424,6 +426,17 @@ abstract class AbstractDatabaseObjectAction implements IDatabaseObjectAction, ID $this->readValue($variableName, $allowEmpty, $arrayIndex, self::TYPE_BOOLEAN); } + /** + * Reads a json-encoded value and validates it. + * + * @param string $variableName + * @param boolean $allowEmpty + * @param string $arrayIndex + */ + protected function readJSON($variableName, $allowEmpty = false, $arrayIndex = '') { + $this->readValue($variableName, $allowEmpty, $arrayIndex, self::TYPE_JSON); + } + /** * Reads a value and validates it. If you set $allowEmpty to true, no exception will * be thrown if the variable evaluates to 0 (integer) or '' (string). Furthermore the @@ -499,6 +512,29 @@ abstract class AbstractDatabaseObjectAction implements IDatabaseObjectAction, ID } } break; + + case self::TYPE_JSON: + if (!isset($target[$variableName])) { + if ($allowEmpty) { + $target[$variableName] = array(); + } + else { + throw new UserInputException($variableName); + } + } + else { + try { + $target[$variableName] = JSON::decode($target[$variableName]); + } + catch (SystemException $e) { + throw new UserInputException($variableName); + } + + if (!$allowEmpty && empty($target[$variableName])) { + throw new UserInputException($variableName); + } + } + break; } } -- 2.20.1