Skip to content

Commit

Permalink
- add warning and error checks
Browse files Browse the repository at this point in the history
  • Loading branch information
peternormann committed Jul 16, 2020
1 parent 2d2b727 commit de33885
Showing 1 changed file with 8 additions and 41 deletions.
49 changes: 8 additions & 41 deletions common/ValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
Expand All @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
{
Expand Down

0 comments on commit de33885

Please sign in to comment.