PHP code example of sentinelphp / drift

1. Go to this page and download the library: Download sentinelphp/drift 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/ */

    

sentinelphp / drift example snippets


use SentinelPHP\Drift\Detector;
use SentinelPHP\Schema\Validator;

$detector = new Detector(new Validator());

$schema = [
    'type' => 'object',
    'properties' => [
        'id' => ['type' => 'integer'],
        'name' => ['type' => 'string'],
    ],
    'tf(
        "[%s] %s at %s\n",
        $drift->severity->value,
        $drift->type->value,
        $drift->path
    );
}

use SentinelPHP\Drift\Classifier;
use SentinelPHP\Drift\Enum\DriftType;
use SentinelPHP\Drift\Enum\DriftSeverity;

$classifier = new Classifier();

// Field removed = Critical
$severity = $classifier->classify(DriftType::FieldRemoved, 'email', null);
// Returns: DriftSeverity::Critical

// Field added = Info
$severity = $classifier->classify(DriftType::FieldAdded, null, 'extra_field');
// Returns: DriftSeverity::Info

// Type changed (object → primitive) = Critical
$severity = $classifier->classify(DriftType::TypeChanged, 'object', 'string');
// Returns: DriftSeverity::Critical

$classifier = new Classifier(defaultThreshold: DriftSeverity::Warning);

// Check if drift should trigger an alert
if ($classifier->shouldAlert($drift->severity)) {
    // Send notification
}

// Override threshold per-check
$classifier->shouldAlert($drift->severity, DriftSeverity::Critical);

use SentinelPHP\Drift\Diff\JsonDiff;

$diff = new JsonDiff();

$expected = ['id' => 1, 'name' => 'John', 'email' => '[email protected]'];
$actual = ['id' => 1, 'name' => 'Jane', 'phone' => '555-1234'];

$result = $diff->generateDiff($expected, $actual);

echo "Added: " . count($result->added);     // phone
echo "Removed: " . count($result->removed); // email
echo "Changed: " . count($result->changed); // name