2017 © Pedro PelĂĄez
 

library ncip

Basic NCIP library

image

danmichaelo/ncip

Basic NCIP library

  • Wednesday, August 13, 2014
  • by danmichaelo
  • Repository
  • 4 Watchers
  • 1 Stars
  • 244 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 1 % Grown

The README.md

Basic NCIP php library and Laravel package

Build Status Coverage Status Latest Stable Version Total Downloads License, (*1)

php-ncip is a php package for parsing and formatting NCIP request and response messages. Development has been guided by a desire for a simple API rather than a complete one. Only a small subset of the NCIP specification is currently covered, but suggestions for additions are welcome., (*2)

Installation:

Composer

Add the package to the require list of your composer.json file., (*3)

{
    "require": {
        "danmichaelo/ncip": "dev-master"
    },
}

and run composer install to get the latest version of the package., (*4)

Additional steps for Laravel 4

The package comes with a Laravel 4 service provider that you can install if you like. It comes with a config file, so you can set settings there instead of passing them to the constructor., (*5)

To register the service provider, add 'Scriptotek\NcipServiceProvider', to the list of providers in app/config/app.php. Then run php artisan config:publish danmichaelo/ncip in your terminal to create the config file app/config/packages/danmichael/ncip/config.php., (*6)

Usage:

Construction

To construct a client, you need to specify the url to the NCIP srvice, a freely choosen user agent string for your client, and the agency id. The agency id identifies your institution, as specified in the NCIP protocol., (*7)

require_once('vendor/autoload.php');
use Scriptotek\Ncip\NcipConnector,
    Scriptotek\Ncip\NcipClient;

$ncipUrl = 'http://eksempel.com/NCIPResponder';
$userAgent = 'My NCIP client/0.1';
$agencyId = 'a';

$conn = new NcipConnector($ncipUrl, $userAgent, $agencyId);
$client = new NcipClient($conn);

To construct a server:, (*8)

require_once('vendor/autoload.php');
use Scriptotek\Ncip\NcipServer;

$server = new NcipServer;

If you have registered the Laravel 4 service provider, the classes can be constructed through the applications container instead:, (*9)

$client = App::make('ncip.client');
$server = App::make('ncip.server');

// Or if you have access to an instance of the application.
$client = $app['ncip.client'];
$server = $app['ncip.server'];

The settings are now pulled from app/config/packages/danmichael/ncip/config.php instead, and the NcipConnector is injected into the NcipClient automatically., (*10)

Client example:

$user = $client->lookupUser('abc123');
if ($user->exists) {
    echo 'Hello ' . $user->firstName . ' ' . $user->lastName;
} else {
    echo 'User not found';
}

Server example:

$postData = file_get_contents('php://input');
$request = $server->parseRequest($postData);

if ($request->is('LookupUser')) {

    $response = new UserResponse;
    $response->userId = $request->userId;
    $response->firstName = 'Meriadoc';
    $response->lastName = 'Brandybuck';
    echo $response->xml();

}

Events:

The client emits events on each request to the server. This can be useful to implement logging. The events are request.item, request.user, request.checkout, request.checkin and request.renew., (*11)

$client->on('request.checkout', function($userId, $itemId) {
    $log->addInfo('Requested checkout of item "' . $itemId . '" for user "' . $userId . '"');
}

For debugging, events message.send and message.recv are emitted on each message sent to and received from the server. The message xml body is given as the first argument., (*12)

$client->on('message.send', function($msg) {
    printf("[SEND] %s\n", $msg);
}
$client->on('message.recv', function($msg) {
    printf("[RECV] %s\n", $msg);
}

The Versions