PHP code example of fyennyi / alerts-in-ua-php

1. Go to this page and download the library: Download fyennyi/alerts-in-ua-php 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/ */

    

fyennyi / alerts-in-ua-php example snippets




use Fyennyi\AlertsInUa\Client\AlertsClient;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Psr16Cache;

$cache = new Psr16Cache(new FilesystemAdapter()); // Or any other PSR-16 cache
$client = new AlertsClient('your_token', $cache);

use function React\Async\await;

try {
    $alerts = await($client->getActiveAlertsAsync());

    echo 'Active alerts: ' . count($alerts->getAllAlerts()) . "\n";

    foreach ($alerts->getAllAlerts() as $alert) {
        echo "{$alert->getAlertType()->value} in {$alert->getLocationTitle()}\n";
    }
} catch (\Throwable $e) {
    echo 'Error: ' . $e->getMessage() . "\n";
}

try {
    $history = await($client->getAlertsHistoryAsync('Харківська область', 'month_ago'));

    echo "\nAlerts history for Kharkiv Oblast: " . count($history->getAllAlerts()) . "\n";

    foreach ($history->getAllAlerts() as $alert) {
        $status = $alert->isFinished() ? 'Finished' : 'Active';
        echo "{$alert->getAlertType()->value} in {$alert->getLocationTitle()} - {$status}\n";
    }
} catch (\Throwable $e) {
    echo 'Error: ' . $e->getMessage() . "\n";
}

try {
    $statuses = await($client->getAirRaidAlertStatusesByOblastAsync());

    echo "\nAir raid alert statuses by oblast:\n";

    foreach ($statuses->getStatuses() as $status) {
        echo "{$status->getOblast()}: {$status->getStatus()}\n";
    }
} catch (\Throwable $e) {
    echo 'Error: ' . $e->getMessage() . "\n";
}

try {
    $statuses = await($client->getAirRaidAlertStatusesAsync());

    echo "\nDetailed air raid alert statuses:\n";

    foreach ($statuses->getStatuses() as $status) {
        echo "{$status->getLocationTitle()}: {$status->getStatus()}\n";
    }
} catch (\Throwable $e) {
    echo 'Error: ' . $e->getMessage() . "\n";
}

try {
    $alerts = await($client->getActiveAlertsAsync());

    // Get only air raid alerts
    $air_raid_alerts = $alerts->getAirRaidAlerts();
    echo "\nAir raid alerts: " . count($air_raid_alerts) . "\n";

    // Get only oblast-level alerts
    $oblast_alerts = $alerts->getOblastAlerts();
    echo "Oblast-level alerts: " . count($oblast_alerts) . "\n";

    // Get alerts for a specific oblast
    $kharkiv_alerts = $alerts->getAlertsByOblast('Харківська область');
    echo "Kharkiv Oblast alerts: " . count($kharkiv_alerts) . "\n";

} catch (\Throwable $e) {
    echo 'Error: ' . $e->getMessage() . "\n";
}

// Export a single alert
echo $alert->toXml('alert');

// Export all active alerts
echo $alerts->toXml('alerts');

use function React\Async\await;
use function React\Promise\all;

$promises = [
    'active' => $client->getActiveAlertsAsync(),
    'history' => $client->getAlertsHistoryAsync('Харківська область', 'month_ago'),
];

try {
    // Wait for both promises to resolve concurrently
    $results = await(all($promises));

    $alerts = $results['active'];
    $history = $results['history'];

    echo "Active alerts: " . count($alerts->getAllAlerts()) . "\n";
    foreach ($alerts->getAllAlerts() as $alert) {
        echo "{$alert->getAlertType()->value} in {$alert->getLocationTitle()}\n";
    }

    echo "\nHistory for Kharkiv Oblast:\n";
    foreach ($history->getAllAlerts() as $alert) {
        $status = $alert->isFinished() ? 'Finished' : 'Active';
        echo "{$alert->getAlertType()->value} in {$alert->getLocationTitle()} - {$status}\n";
    }
} catch (\Throwable $e) {
    echo "Error fetching concurrent alerts: " . $e->getMessage() . "\n";
}

$client->getActiveAlertsAsync()->then(function ($alerts) {
    $air_raid_alerts = $alerts->getAirRaidAlerts();
    $oblast_alerts = $alerts->getOblastAlerts();
    $kharkiv_alerts = $alerts->getAlertsByOblast('Харківська область');

    echo "Air raid alerts: " . count($air_raid_alerts) . "\n";
    echo "Oblast-level alerts: " . count($oblast_alerts) . "\n";
    echo "Kharkiv Oblast alerts: " . count($kharkiv_alerts) . "\n";
});

use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;
use Symfony\Component\Cache\Psr16Cache;

// Example with Filesystem cache
$filesystemCache = new Psr16Cache(new FilesystemAdapter());
$clientWithFilesystemCache = new AlertsClient('your_token', $filesystemCache);

// Example with Redis cache
$redisClient = new \Redis();
$redisClient->connect('localhost', 6379);
$redisCache = new Psr16Cache(new RedisAdapter($redisClient));
$clientWithRedisCache = new AlertsClient('your_token', $redisCache);

// Example with Memcached cache
$memcachedClient = new \Memcached();
$memcachedClient->addServer('localhost', 11211);
$memcachedCache = new Psr16Cache(new \Symfony\Component\Cache\Adapter\MemcachedAdapter($memcachedClient));
$clientWithMemcachedCache = new AlertsClient('your_token', $memcachedCache);

// Basic setup with default cache (if not provided, a no-op cache is used)
$client = new AlertsClient('your_token', $filesystemCache); // Or any other PSR-16 cache
bash
composer