Skip to content

Commit

Permalink
Merge pull request #2 from peternormann/master
Browse files Browse the repository at this point in the history
Diversify handling of warnings and errors
  • Loading branch information
sorenbronsted authored Jul 16, 2020
2 parents a41f01a + de33885 commit 856e165
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 121 deletions.
124 changes: 70 additions & 54 deletions common/Request.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,81 @@
<?php

namespace sbronsted;

/**
* Class Request helps managing cookie information and authentication tokens.
*/
class Request {
/**
* Checks if the token is in the request.
* @param string
* $name the name of token to look for
* @return bool
* true when $_REQUEST[$name] exists otherwise false
*/
public function hasAuthToken(string $name) : bool {
return isset($_REQUEST[$name]) && !empty($_REQUEST[$name]);
}
class Request
{
/**
* Checks if the token is in the request.
*
* @param string
* $name the name of token to look for
*
* @return bool
* true when $_REQUEST[$name] exists otherwise false
*/
public function hasAuthToken(string $name): bool
{
return isset($_REQUEST[$name]) && !empty($_REQUEST[$name]);
}

/**
* Get the authtification 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;
}
/**
* 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];
}
}
152 changes: 85 additions & 67 deletions common/ValidationException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace sbronsted;

/**
Expand All @@ -12,77 +13,94 @@
* $e->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 = [];
}

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 = [];
}
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);
}
public function hasValidations(): bool
{
return $this->hasErrors() | $this->hasWarnings();
}

/**
* Delegates to @see toJson
* @return false|string
*/
public function __toString() {
return $this->toJson();
}
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;
}
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];
}
/**
* Add an 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];
}
/**
* Add a warning validation
*
* @param string $property A property belonging 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,
];
}
}

0 comments on commit 856e165

Please sign in to comment.