comexio / php-utils by logcomex

PHP Utils for Lumen and Laravel projects
142,462
1
5
Package Data
Maintainer Username: logcomex
Package Create Date: 2020-04-09
Package Last Update: 2023-11-13
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-04-18 15:25:17
Package Statistics
Total Downloads: 142,462
Monthly Downloads: 4,164
Daily Downloads: 120
Total Stars: 1
Total Watchers: 5
Total Forks: 2
Total Open Issues: 2

php-utils

PHP Utilities for Laravel/Lumen

Installation

cd /path/to/your/project
composer require logcomex/php-utils

Utilities Packages

  • Contracts
  • Exceptions
  • Functionalities
  • Helpers
  • Handlers
  • Logs
  • Middlewares

Contracts

Have all the contracts (interfaces) used by the php-utils classes and other that you can use in your project.

Exceptions

Have all the exceptions used by the php-utils classes. And others that you can use in your project to segregate your errors types .

ApiException

You can use for all exceptions in 400 range http code

ApiException(string $token,
			string $message, 
			int $httpCode = Response::HTTP_BAD_REQUEST, 
			Exception $previous = null)  

| Visibility | Function | Return Type | | ---------- | -------------- | ----------- | | public | getHttpCode | int | | public | getToken | string | | public | __toString | string | | public | toArray | array | | public | toJson | string |

BadImplementationException

This exception means that a situation has been overlooked or incorrectly done by the developer.

BadImplementationException(string $message,
			int $httpCode = Response::HTTP_INTERNAL_SERVER_ERROR, 
			Exception $previous = null)  

| Visibility | Function | Return Type | | ---------- | -------------- | ----------- | | public | getHttpCode | int | | public | __toString | string | | public | toArray | array | | public | toJson | string |

SecurityException

This exception serves to point out some security problem in your application.

SecurityException(string $token,
			string $message, 
			int $httpCode = Response::HTTP_FORBIDDEN, 
			Exception $previous = null)  

| Visibility | Function | Return Type | | ---------- | -------------- | ----------- | | public | getHttpCode | int | | public | getToken | string | | public | __toString | string | | public | toArray | array | | public | toJson | string |

Functionalities

They're a pack of traits that can be useful in your code

PropertiesExporterFunctionality

You can use this functionality to export an array with you class properties

public static function properties(): array  

PropertiesAttacherFunctionality

You can use this functionality to attach in your class properties the values passed in the parameter.

Note: To uses this functionality, you need use the PropertiesExporterFunctionality in the class.

public function attachValues(array $values): void

| Exception | Reason | | ---------- | ----------- | | BadImplementationException | When you don't use PropertiesExporterFunctionality |

ValuesExporterToArrayFunctionality

You can use this functionality to easily get all the properties of class in an array.

Note: To uses this functionality, you need to do two things:

  1. The class must implement Illuminate\Contracts\Support\Arrayable.
  2. The class must use PropertiesExporterFunctionality.
public function toArray()  

| Exception | Reason | | ---------- | ----------- | | BadImplementationException | When yout don't implement the the Arrayable contract | | BadImplementationException | When you don't use PropertiesExporterFunctionality |

Helpers

They're a pack of Helpers classes and traits.

EnumHelper

It's a trait that provide some utilities to your Enumerators classes.

| Visibility | Function | Return Type | Purpose | | :--- | :--- | :--- |:--- | | public | all | array | Get all the constants of your Enumerator |

use Logcomex\PhpUtils\Helpers\EnumHelper;

class ProductEnum
{
	user EnumHelper;
	public const EXAMPLE = 'example';
	public const EXAMPLE2 = 'example2';
}

$allProducts = ProductEnum::all();

Unit Tests Coverage

100% at moment.

TODO

  • [ ] HttpHelper Doc
  • [ ] TokenHelper Doc
  • [ ] Handlers Package Doc
    • [ ] ExceptionHandler Doc
  • [ ] Logs Package Doc
    • [ ] RequestLog Doc
  • [ ] Middlewares Package Doc
    • [ ] AccreditedApiKeysMiddleware Doc
    • [ ] AllowedHostsMiddleware Doc
    • [ ] CorsMiddleware Doc
    • [ ] RequestLogMiddleware Doc
  • [ ] Implement build tests requirement

Contributing

  • Open an issue first to discuss potential changes/additions.
  • Open a pull request, you need two approvals and tests need to pass Travis CI.