From 2d2b727f7b6aa49da7999024f721eee7e50f56f2 Mon Sep 17 00:00:00 2001 From: Peter Normann Date: Thu, 16 Jul 2020 06:21:59 +0200 Subject: [PATCH 1/2] - add warning and error checks --- common/Request.php | 124 ++++++++++++---------- common/ValidationException.php | 185 +++++++++++++++++++++------------ 2 files changed, 188 insertions(+), 121 deletions(-) diff --git a/common/Request.php b/common/Request.php index ab91f1c..6a55b8b 100644 --- a/common/Request.php +++ b/common/Request.php @@ -1,65 +1,81 @@ hasAuthToken($name) ? $_REQUEST[$name] : null; - } + /** + * Get the authentification token. + * + * @param string + * $name the name of token + * + * @return string|null + * the token if found otherwise null + */ + public function getAuthToken(string $name): ?string + { + return $this->hasAuthToken($name) ? $_REQUEST[$name] : null; + } - /** - * Checks if a cookie with $cookieName exists - * @param string - * $cookieName the of the cookie - * @return bool - * true if the cookie exists otherwise false - */ - public function hasCookie(string $cookieName) : bool{ - return isset($_COOKIE[$cookieName]) && !empty($_COOKIE[$cookieName]); - } + /** + * Checks if a cookie with $cookieName exists + * + * @param string + * $cookieName the of the cookie + * + * @return bool + * true if the cookie exists otherwise false + */ + public function hasCookie(string $cookieName): bool + { + return isset($_COOKIE[$cookieName]) && !empty($_COOKIE[$cookieName]); + } - /** - * Set the cookie with $value - * @param string $cookieName - * the name of the cookie - * @param string $value - * the value to set - * @param int $ttl - * the time-to-live in seconds - */ - public function setCookie(string $cookieName, string $value, int $ttl) { - setcookie($cookieName, $value, $ttl); // this does not set $_COOKIE, but only the header - $_COOKIE[$cookieName] = $value; - } + /** + * Set the cookie with $value + * + * @param string $cookieName + * the name of the cookie + * @param string $value + * the value to set + * @param int $ttl + * the time-to-live in seconds + */ + public function setCookie(string $cookieName, string $value, int $ttl): void + { + setcookie($cookieName, $value, $ttl); // this does not set $_COOKIE, but only the header + $_COOKIE[$cookieName] = $value; + } - /** - * Get the value of cookie by name - * @param string $cookieName - * the of the cookie - * @return string|null - * the value of the cookie - */ - public function getCookie(string $cookieName) : string { - return $_COOKIE[$cookieName]; - } + /** + * Get the value of cookie by name + * + * @param string $cookieName + * the of the cookie + * + * @return string|null + * the value of the cookie + */ + public function getCookie(string $cookieName): string + { + return $_COOKIE[$cookieName]; + } } \ No newline at end of file diff --git a/common/ValidationException.php b/common/ValidationException.php index 2331de2..1a60627 100644 --- a/common/ValidationException.php +++ b/common/ValidationException.php @@ -1,4 +1,5 @@ addWaring('name','A message to user'); * ``` */ -class ValidationException extends ApplicationException { - private $validations; - private $cls; +class ValidationException extends ApplicationException +{ + private $errors; + private $warnings; + private $cls; + + /** + * ValidationException constructor. + * + * @param string $cls + * The name of the class for the properties that will be added by the add methods. + */ + public function __construct(string $cls) + { + parent::__construct(); + $this->cls = $cls; + $this->errors = []; + $this->warnings = []; + } + + /** + * @return array + * The validations + */ + public function validations(): array + { + return array_merge($this->errors, $this->warnings); + } + + /** + * Transform this class to the following json format: + * ``` + * [{"property":"some_property", "msg":"some_message", "type":"error|warning", "class":"SomeProperty"}, ...] + * ``` + * + * @return string + * json encoded array of validations + */ + public function toJson(): string + { + // consider using JSON_THROW_ON_ERROR instead when switching php 7.3+ + $json = json_encode($this->validations()); + return $json ? $json : '{}'; + } - /** - * ValidationException constructor. - * @param string $cls - * The name of the class for the properties that will be added by the add methods. - */ - public function __construct(string $cls) { - parent::__construct(); - $this->cls = $cls; - $this->validations = []; - } + /** + * Delegates to @return false|string + * + * @see toJson + */ + public function __toString(): string + { + return $this->toJson(); + } - /** - * @return array - * The validations - */ - public function validations() : array { - return $this->validations; - } - /** - * Transform this class to the following json format: - * ``` - * [{"property":"some_property", "msg":"some_message", "type":"error|warning", "class":"SomeProperty"}, ...] - * ``` - * - * @return false|string - * json encoded array of validations - */ - public function toJson() { - return json_encode($this->validations); - } + /** + * Test whether some validations has been added. + * + * @return bool + * true if some validations has been added, otherwise false + */ + public function hasValidations(): bool + { + return $this->hasErrors() | $this->hasWarnings(); + } - /** - * Delegates to @see toJson - * @return false|string - */ - public function __toString() { - return $this->toJson(); - } + /** + * Test whether errors has been added. + * + * @return bool + * true if some errors has been added, otherwise false + */ + public function hasErrors(): bool + { + return count($this->errors) > 0; + } - /** - * Test whether some validations has been added. - * @return bool - * true if some validations has been added, otherwaie false - */ - public function hasValidations() { - return count($this->validations) > 0; - } + /** + * Test whether warnings has been added. + * + * @return bool + * true if some errors has been added, otherwise false + */ + public function hasWarnings(): bool + { + return count($this->warnings) > 0; + } - /** - * Adds a error validation - * @param string $property - * A property belowing to @cls - * @param string $msg - * The message to the user - */ - public function addError(string $property, string $msg) { - $this->validations[] = ['property' => $property, 'msg' => $msg, 'type' => 'error', 'class' => $this->cls]; - } + /** + * Adds a error validation + * + * @param string $property + * A property belowing to @cls + * @param string $msg + * The message to the user + */ + public function addError(string $property, string $msg): void + { + $this->errors[] = [ + 'property' => $property, + 'msg' => $msg, + 'type' => 'error', + 'class' => $this->cls, + ]; + } - /** - * Adds a waring validation - * @param string $property - * A property belowing to @cls - * @param string $msg - * The message to the user - */ - public function addWarning(string $property, string $msg) { - $this->validations[] = ['property' => $property, 'msg' => $msg, 'type' => 'warning', 'class' => $this->cls]; - } + /** + * Adds a waring validation + * + * @param string $property + * A property belowing to @cls + * @param string $msg + * The message to the user + */ + public function addWarning(string $property, string $msg): void + { + $this->warnings[] = [ + 'property' => $property, + 'msg' => $msg, + 'type' => 'warning', + 'class' => $this->cls, + ]; + } } From de338851169b93ed651c73f63d5879c91a63a367 Mon Sep 17 00:00:00 2001 From: Peter Normann Date: Thu, 16 Jul 2020 06:32:11 +0200 Subject: [PATCH 2/2] - add warning and error checks --- common/ValidationException.php | 49 ++++++---------------------------- 1 file changed, 8 insertions(+), 41 deletions(-) diff --git a/common/ValidationException.php b/common/ValidationException.php index 1a60627..08409eb 100644 --- a/common/ValidationException.php +++ b/common/ValidationException.php @@ -22,8 +22,7 @@ class ValidationException extends ApplicationException /** * ValidationException constructor. * - * @param string $cls - * The name of the class for the properties that will be added by the add methods. + * @param string $cls The name of the class for the properties that will be added by the add methods. */ public function __construct(string $cls) { @@ -33,10 +32,6 @@ public function __construct(string $cls) $this->warnings = []; } - /** - * @return array - * The validations - */ public function validations(): array { return array_merge($this->errors, $this->warnings); @@ -48,8 +43,7 @@ public function validations(): array * [{"property":"some_property", "msg":"some_message", "type":"error|warning", "class":"SomeProperty"}, ...] * ``` * - * @return string - * json encoded array of validations + * @return string json encoded array of validations */ public function toJson(): string { @@ -58,56 +52,31 @@ public function toJson(): string return $json ? $json : '{}'; } - /** - * Delegates to @return false|string - * - * @see toJson - */ public function __toString(): string { return $this->toJson(); } - /** - * Test whether some validations has been added. - * - * @return bool - * true if some validations has been added, otherwise false - */ public function hasValidations(): bool { return $this->hasErrors() | $this->hasWarnings(); } - /** - * Test whether errors has been added. - * - * @return bool - * true if some errors has been added, otherwise false - */ public function hasErrors(): bool { return count($this->errors) > 0; } - /** - * Test whether warnings has been added. - * - * @return bool - * true if some errors has been added, otherwise false - */ public function hasWarnings(): bool { return count($this->warnings) > 0; } /** - * Adds a error validation + * Add an error validation * - * @param string $property - * A property belowing to @cls - * @param string $msg - * The message to the user + * @param string $property A property belowing to @cls + * @param string $msg The message to the user */ public function addError(string $property, string $msg): void { @@ -120,12 +89,10 @@ public function addError(string $property, string $msg): void } /** - * Adds a waring validation + * Add a warning validation * - * @param string $property - * A property belowing to @cls - * @param string $msg - * The message to the user + * @param string $property A property belonging to @cls + * @param string $msg The message to the user */ public function addWarning(string $property, string $msg): void {