PHP Classes

File: client/list-unique_number

Recommend this page to a friend!
  Classes of nvb   PHP Unique Number Repository   client/list-unique_number   Download  
File: client/list-unique_number
Role: Example script
Content type: text/plain
Description: Example script
Class: PHP Unique Number Repository
Maintain repositories of unique numbers via an API
Author: By
Last change:
Date: 4 years ago
Size: 1,310 bytes
 

Contents

Class file image Download
#!/usr/bin/env php
<?php

require_once __DIR__ . '/../bootstrap/client.php';

$usage = 'usage: ' . PHP_EOL .
   
' ' . basename(__FILE__) . ' <host> <repository name>' . PHP_EOL;

execute(function () use ($baseUrl, $command, $values) {

   
throwExceptionIfInvalidNumberOfValuesWasProvided($values, 2);

    list(
$host, $repositoryName) = extractValues($values);

   
throwExceptionIfValueIsInvalid($host, 'host');
   
throwExceptionIfValueIsInvalid($repositoryName, 'repository name');

   
$url = $baseUrl . '/' . $repositoryName;
   
$lines = $command->get($host, $url);
   
$repositoriesAreAvailable = ((is_array($lines) && (count($lines) > 0)));

    if (
$repositoriesAreAvailable) {
        foreach (
$lines as $line) {
           
$data = json_decode($line);
            echo
'[number]' . PHP_EOL;
           
$uniqueNumbersAreAvailable = ((is_array($data) && (count($data) > 0)));
            if (
$uniqueNumbersAreAvailable) {
                foreach (
$data as $uniqueNumber) {
                    echo
$uniqueNumber->number . PHP_EOL;
                }
            } else {
                echo
'unexpected response: ' . var_export($lines, true);
            }
        }
    } else {
        echo
'no repository available' . PHP_EOL;
    }
},
$usage);