PHP code example of timefrontiers / php-instance-error

1. Go to this page and download the library: Download timefrontiers/php-instance-error 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/ */

    

timefrontiers / php-instance-error example snippets


/** @var array{0: int, 1: int, 2: string, 3: string, 4: int} $error */
$error = [$minimumRank, $code, $message, $file, $line];

/** @var array<string, list<array{0: int, 1: int, 2: string, 3: string, 4: int}>> $errors */
$errors = [
  'validation' => [
    [0, 400, 'Invalid email address.', '/app/User.php', 42],
  ],
];

use TimeFrontiers\InstanceError;

$extractor = new InstanceError($service);

// Grouped tuples visible to the current session rank.
$visible = $extractor->get();

// Flat list of tuples from one context.
$validation = $extractor->get('validation');

// Flat list of strings from one context.
$validationMessages = $extractor->get('validation', true);

$groupedMessages = $extractor->get('', true);
// ['validation' => ['Invalid email address.'], 'save' => ['Save failed.']]

$flatMessages = $extractor->messages();
// ['Invalid email address.', 'Save failed.']

if ($extractor->has('validation')) {
  $first = $extractor->first('validation');
  $count = $extractor->count('validation');
}

use TimeFrontiers\AccessRank;
use TimeFrontiers\InstanceError;

$developerView = new InstanceError($service, AccessRank::DEVELOPER);
$rankFiveView = new InstanceError($service, 5);

use TimeFrontiers\InstanceError;

// Logs only errors visible to the normal session-resolved extractor.
$displayExtractor = new InstanceError($service);
$displayExtractor->log('validation');

// Trusted server-side flow: deliberately ;

$extractor->put('custom', [
  AccessRank::GUEST->value,
  400,
  'Something went wrong.',
  __FILE__,
  __LINE__,
]);

use TimeFrontiers\Helper\HasErrors;

final class Service {
  use HasErrors;

  public function process():bool {
    $this->_userError('process', 'The request could not be processed.', 400);
    return false;
  }
}

_addError(
  string $context,
  string $message,
  int $minimumRank = AccessRank::GUEST->value,
  int $code = 256,
  string $file = '',
  int $line = 0,
): void
bash
composer