Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
run: composer install --no-progress --no-dev --prefer-dist --optimize-autoloader
- name: Composer install with dev
run: composer install --no-progress --prefer-dist --optimize-autoloader
- name: phpcs
run: composer phpcs
- name: PHPUnit
run: composer test

Expand Down
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
],
"type": "library",
"require-dev": {
"phpunit/phpunit": "^12.0"
"phpunit/phpunit": "^12.0",
"squizlabs/php_codesniffer": "^3.9"
},
"require": {
"ext-json": "*",
Expand All @@ -32,6 +33,8 @@
}
},
"scripts": {
"test": "phpunit"
"test": "phpunit",
"phpcs": "phpcs",
"phpcbf": "phpcbf"
}
}
7 changes: 7 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<ruleset name="ObjectErrors PSR-12">
<description>PHP_CodeSniffer configuration for ObjectErrors using PSR-12</description>
<rule ref="PSR12"/>
<file>src</file>
<file>tests</file>
</ruleset>
22 changes: 10 additions & 12 deletions src/ErrorsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ class ErrorsCollection implements ArrayAccess, Countable, JsonSerializable, Stri
* @param ParamsMode $mode Mode how this errors collection works
*/
public function __construct(
public readonly ParamsMode $mode = ParamsMode::Mode1
)
{
public readonly ParamsMode $mode = ParamsMode::Mode1
) {
}

/**
Expand Down Expand Up @@ -116,8 +115,8 @@ public function __toString(): string
return '';
}

return implode(PHP_EOL, $this->errors);
}
return implode(PHP_EOL, $this->errors);
}

/**
* Adds an
Expand Down Expand Up @@ -188,14 +187,13 @@ public function toArray(): array
return $this->errors;
}

/**
* Export data to json
*
* @throws JsonException
*/
/**
* Export data to json
*
* @throws JsonException
*/
public function toJson(): string
{
return json_encode($this, JSON_THROW_ON_ERROR);
}

}
}
104 changes: 51 additions & 53 deletions src/ErrorsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,54 @@
*/
trait ErrorsTrait
{

/**
* Errors collection
*/
protected ErrorsCollection $errors;

/**
* ErrorsTrait constructor.
*/
public function __construct()
{
$this->errors = new ErrorsCollection();
}

/**
* return the errors for this object as an array
*
* @param bool $asHTML Format using HTML?
*
* @return array|string an array of errors
*/
public function getErrors(bool $asHTML = true): array|string
{
return $asHTML ? $this->getHtmlErrors() : $this->errors->toArray();
}

/**
* add an error
*
* @param string $err_str error to add
*/
public function setErrors(string $err_str): void
{
call_user_func_array([$this->errors, 'add'], func_get_args());
}

/**
* Returns the errors for this object as html
*/
public function getHtmlErrors(): string
{
return $this->errors->getHtml();
}

/**
* Has some errors?
*/
public function hasError(): bool
{
return !$this->errors->isEmpty();
}

}
/**
* Errors collection
*/
protected ErrorsCollection $errors;

/**
* ErrorsTrait constructor.
*/
public function __construct()
{
$this->errors = new ErrorsCollection();
}

/**
* return the errors for this object as an array
*
* @param bool $asHTML Format using HTML?
*
* @return array|string an array of errors
*/
public function getErrors(bool $asHTML = true): array|string
{
return $asHTML ? $this->getHtmlErrors() : $this->errors->toArray();
}

/**
* add an error
*
* @param string $err_str error to add
*/
public function setErrors(string $err_str): void
{
call_user_func_array([$this->errors, 'add'], func_get_args());
}

/**
* Returns the errors for this object as html
*/
public function getHtmlErrors(): string
{
return $this->errors->getHtml();
}

/**
* Has some errors?
*/
public function hasError(): bool
{
return !$this->errors->isEmpty();
}
}
26 changes: 12 additions & 14 deletions src/ParamsMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@

enum ParamsMode: int
{
/**
* Mode that says that only one param for adding is used
*/
case Mode1 = 0;

/**
* Mode that says that only one param for adding is used
*/
case Mode1 = 0;

/**
* Mode that says two params are used
*/
case Mode2 = 1;

/**
* Mode that says that 2nd param is a used as prefix
*/
case Mode2AsPrefix = 2;
/**
* Mode that says two params are used
*/
case Mode2 = 1;

/**
* Mode that says that 2nd param is a used as prefix
*/
case Mode2AsPrefix = 2;
}
3 changes: 1 addition & 2 deletions src/UnsetErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class UnsetErrorException extends Exception
{

}
}
Loading