PHP code example of andro-adel / microservice-package

1. Go to this page and download the library: Download andro-adel/microservice-package library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andro-adel / microservice-package example snippets


use DD\MicroserviceCore\Classes\ApiResponses;

ApiResponses::successResponse(array $data, string $reason, string|null $message = null, array $additionData = [], $status = 200)

return ApiResponses::successResponse(data: ["user" : $user], reason: "User retrieved successfully", additionData: ["extra" : "extra value"]);

ApiResponses::successNoContentResponse()

return ApiResponses::successNoContentResponse();

ApiResponses::notModifiedResponse(string|null $resourceName = null,string|null $message = null,array $additionData = [])

return ApiResponses::notModifiedResponse(resource: "User", message: "User not modified", additionData: ["extra" : "extra value"]);

ApiResponses::badRequestResponse(string|null $message = null, array $additionData = [], $status = 400)

return ApiResponses::badRequestResponse(message: "Invalid data", additionData: ["extra" : "extra value"]);

ApiResponses::unauthorizedResponse(string|null $message = null, array $additionData = [])

return ApiResponses::unauthorizedResponse(message: "Unauthorized User", additionData: ["extra" : "extra value"]);

ApiResponses::unauthenticatedResponse(string|null $message = null, array $additionData = [])

return ApiResponses::unauthenticatedResponse(message: "Unauthenticated User", additionData: ["extra" : "extra value"]);

ApiResponses::notFoundResponse(string|null $resourceName = null, string|null $message = null, array $additionData = [])

return ApiResponses::notFoundResponse(resource: "User", message: "User not found", additionData: ["extra" : "extra value"]);

ApiResponses::conflictResponse(string $type,array $data,string|null $resourceName = null, string|null $message = null, array $additionData = [])

return ApiResponses::conflictResponse(type:"User",data: ["user" : $user], resourceName: "User", message: "User already exists", additionData: ["extra" : "extra value"]);

ApiResponses::notValidResponse(array $errors, array $data, string|null $message = null, array $additionData = [])

return ApiResponses::notValidResponse(errors: ["name" : "Name is 

ApiResponses::serverErrorResponse(string|int  $errorCode,string|null $message = null, array $additionData = [])

return ApiResponses::serverErrorResponse(errorCode: 500, message: "Server error", additionData: ["extra" : "extra value"]);

ApiResponses::successPaginationResponse(LengthAwarePaginator $data, string $reason = 'Show', string|null $message = null, array $additionData = [])

return ApiResponses::successPaginationResponse(data: $users, reason: "Users retrieved successfully", additionData: ["extra" : "extra value"]);

ApiResponses::successShowPaginationResponse($data, $meta, string $reason = 'Show')

return ApiResponses::successShowPaginationResponse(data: $users, meta: $meta, reason: "Users retrieved successfully");

ApiResponses::successShowPaginatedDataResponse(JsonResource $data, string $reason = 'Show')

return ApiResponses::successShowPaginatedDataResponse(data: $users, reason: "Users retrieved successfully");

ApiResponses::createdSuccessfullyResponse($data = null, string|null $resourceName = null,?string $message = null)

return ApiResponses::createdSuccessfullyResponse(data: ["user" : $user], resourceName: "User", message: "User created successfully");

use DD\MicroserviceCore\Classes\Caching;

$caching = new Caching();

$caching = new Caching('connection_name');

$caching->setConnection('connection_name');

public function __construct(protected readonly Caching $caching) {}

$caching->setConnection('connection_name');

$caching->set(string $key, mixed $value);

use DD\MicroserviceCore\Classes\SetOptions;
$options = new SetOptions();
$options->setExpiration(int $ttl);
$options->setExpirationTimestamp(string $timestamp);
$options->setExpirationInMilliseconds(int $ttl);
$options->setExpirationTimestampInMilliseconds(string $timestamp);
$options->setExpirationSameAsOld();
$options->returnOldValue();
$options->setIfNew();
$options->setIfExist();
$caching->set(string $key, mixed $value, SetOptions $options);

$values = [
   'key1' => 'value1',
   'key2' => 'value2',
   'key3' => 'value3',
];
$caching->setMany(array $values);

$caching->get(string|array $key);

$caching->rename(string $oldKey, string $newKey);

$caching->delete(string|array $key);

$caching->getType(string $key);

$caching->setExpire(string $key, int $ttl);

use DD\MicroserviceCore\Classes\ExpireOptions;
$options = new ExpireOptions();
$options->setIfNew();
$options->setIfExist();
$options->setIfGreaterThanCurrent();
$options->setIfLessThanCurrent();
$caching->set(string $key, int $ttl, ExpireOptions $options);

$caching->isKeyExist(string|array $key);

$caching->keySearching(string $pattern);

$caching->getAllKeys();

$caching->transaction(Closure $callback);

$caching->command(string $command, array $values);

use DD\MicroserviceCore\Classes\FilterManager;

$filterManager = new FilterManager();

$filterManager = new FilterManager(array $filtersData);

$filterManager->setFiltersData(array $filtersData);

public function __construct(protected readonly FilterManager $filterManager) {}

$caching->setFiltersData(array $filtersData);

$filterManager->addWhereFilter(string $column);

$filterManager->addWhereFilter(string $column, string $operator);

$filterManager->addWhereFilter(string $column, string $operator, string $valueKey);

$filterManager->addWhereInFilter(string $column);

$filterManager->addWhereInFilter(string $column, string $valueKey);

$filterManager->addWhereNotInFilter(string $column);

$filterManager->addWhereNotInFilter(string $column, string $valueKey);

$filterManager->addWhereHasFilter(string $relationName, Closure $callback);

$filterManager->addWhereHasFilter(string $relationName, Closure $callback, string $valueKey);

$filterManager->addWhereHasMorphFilter(string $relationName, string|array $types, Closure $callback);

$filterManager->addWhereHasMorphFilter(string $relationName, string|array $types, Closure $callback, string $valueKey);

$filterManager->addHasFilter(string $relationName);

$filterManager->addHasFilter(string $relationName, string $operator);

$filterManager->addHasFilter(string $relationName, string $operator, string $valueKey);

  $filtersData = [
       $column => ['value1','value2'],
  ];
  $filterManager->addWhereBetweenFilter(string $column);
  

  $filtersData = [
       $valueKey => ['value1','value2'],
  ];
  $filterManager->addWhereBetweenFilter(string $column, string $valueKey);
  

  $filtersData = [
     'valueKey1' => 'value1',
     'valueKey2' => 'value2',
  ];
  $filterManager->addWhereBetweenFilter(string $column, ['valueKey1','valueKey2']);
  

  $callback = function(): array {};
  $filterManager->addWhereBetweenFilter(string $column, Closure $callback);
  

    $filtersData = [
       $column => ['value1','value2'],
    ];
    $filterManager->addWhereBetweenFilter(string $column);
    

    $filtersData = [
       $valueKey => ['value1','value2'],
    ];
    $filterManager->addWhereBetweenFilter(string $column, string $valueKey);
    

    $filtersData = [
       'valueKey1' => 'value1',
       'valueKey2' => 'value2',
    ];
    $filterManager->addWhereBetweenFilter(string $column, ['valueKey1','valueKey2']);
    

    $callback = function(): array {};
    $filterManager->addWhereBetweenFilter(string $column, Closure $callback);
    

$filterManager->addWhereFunctionFilter(Closure $callback, string $filterKey);

$columns = [
   'column1',
   'column2' => 'valueKey',
   'column3'
];
$filterManager->addMultipleWhereFilter(array $columns);

$filterManager->addMultipleWhereFilter(array $columns, string $operator);

$columns = [
   'column1',
   'column2' => 'valueKey',
   'column3'
];
$filterManager->addMultipleWhereInFilter(array $columns);

$columns = [
   'column1',
   'column2' => 'valueKey',
   'column3'
];
$filterManager->addMultipleWhereNotInFilter(array $columns);

$relations = [
   'relation1' => function($query){},
   'relation2' => function($query){},
   'relation3' => function($query){}
];
$filterManager->addMultipleWhereHasFilter(array $relations);

$relations = [
   'relation1',
   'relation2' => 'valueKey',
   'relation3'
];
$filterManager->addMultipleHasFilter(array $relations);

$filterManager->addMultipleHasFilter(array $relations, string $operator);

$data = [
   'column1',
   'column2' => 'valueKey',
   'column3' => ['valueKey1', 'valueKey2'],
   'column4' => functioin(): array {}
];
$filterManager->addMultipleWhereBetweenFilter(array $data);

$data = [
   'column1',
   'column2' => 'valueKey',
   'column3' => ['valueKey1', 'valueKey2'],
   'column4' => functioin(): array {}
];
$filterManager->addMultipleWhereNotBetweenFilter(array $data);

$filterManager->applyFilters(Relation|Builder|Collection|array &$data)

use DD\MicroserviceCore\Classes\FilterManager;
use DD\MicroserviceCore\Interfaces\ServiceInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;

class ModelService implements ServiceInterface
{

   /**
   * @inheritDoc
   */
   public function applyFilters(Relation|Builder|array|Collection &$data, ?array $filtersData = null): void
   {
      $filterManager = new FilterManager($filtersData);

      $filterManager->addMultipleWhereFilter(['name', 'email' => 'mail'], 'LIKE');
      $filterManager->addWhereInFilter('id');

      $filterManager->applyFilters($data);
   }
}

use DD\MicroserviceCore\Classes\Logging

Logging::emergency(string $message, array $context);
Logging::alert(string $message, array $context);
Logging::critical(string $message, array $context);
Logging::error(string $message, array $context);
Logging::warning(string $message, array $context);
Logging::notice(string $message, array $context);
Logging::info(string $message, array $context);
Logging::debug(string $message, array $context);

use DD\MicroserviceCore\Abstracts\ApiFormRequest;
class YourRequestForm extends ApiFormRequest
{}
bash
php artisan vendor:publish --provider="DD\MicroserviceCore\MicroserviceCoreServiceProvider"
bash
php artisan vendor:publish --tag=dd-config
bash
php artisan vendor:publish --tag=dd-lang