PHP Beer Server – Part Two

Part One

Today, we’ll be fleshing out the method bodies of our PHP Beer Service.

First, I’m going to fix the getMethods function.

Old:
[php]
/*
* getMethods returns a Vector of methods accessable to a client
*/
function getMethods( $params ) {
$methods = array(
‘getPrice(String beer)’,
‘setPrice(String beer, Double price)’,
‘getBeers()’,
‘getCheapest()’,
‘getCostliest()’
);
return new xmlrpcresp( new xmlrpcval($methods), "vector");
}
[/php]

New:
[php]
/*
* getMethods returns an Array of methods accessable to a client
*/
function getMethods( $params ) {
$methods = array(
new xmlrpcval(‘getPrice(String beer)’, ‘string’),
new xmlrpcval(‘setPrice(String beer, Double price)’, ‘boolean’),
new xmlrpcval(‘getBeers()’, ‘array’),
new xmlrpcval(‘getCheapest()’, ‘string’),
new xmlrpcval(‘getCostliest()’, ‘string’)
);
return new xmlrpcresp( new xmlrpcval($methods), "array" );
}
[/php]
Here, I’ve updated the getMethods function a bit. I found out that to correctly return the array, it needs to be an array of xmlrpcval objects. So, easy fix.

I made an associative array to hold the beer data. Keeping it simple.
[php]
$beersArray = array(
‘Bud’ => ‘6.99’,
‘Coors’ => ‘8.49’,
‘Corona’ => ‘13.99’,
‘Genesee’ => ‘4.99’,
‘Guinness Draught’ => ‘12.99’,
‘Labatt’ => ‘7.99’,
‘Sam Adams’ => ‘12.49’
);
[/php]

Now that we have our array, we can start on the getPrice function. Thanks to PHP’s enormous amount of well-built methods, it’s as simple as using Array Search.

[php]
/*
* getPrice returns a Double price for a beer
*/
function getPrice( $beer ){
return new xmlrpcval(array_search( $beer, $beersArray ), ‘double’);
}
[/php]

Now, I’m going to move on to function getBeers. I have a feeling I might run into an issue here, since PHP doesn’t support Vectors like Java – I’m passing back an array. We’ll see how this goes in testing.
[php]
/*
* getBeers accepts no parameters, returns a Vector of beers and their prices
*/
function getBeers() {
return new xmlrpcval($beersArray, ‘array’);
}
[/php]

For our next function, getCheapest, it was a little harder to think of a solution, since we have to look through the array, find the minimum value, and then find its corresponding key. Thankfully, a quick Google search led me to StackOverflow, which had the exact solution.

[php]
/*
* getCheapest accepts no parameters, returns a String of the cheapest beer on tap
*/
function getCheapest(){
return new xmlrpcval(array_keys($beersArray, min($beersArray)), ‘string’);
}
[/php]

GetCostliest was ultra easy, since it’s just one function change, from min to max.
[php]
/*
* getCostliest accepts no parameters, returns a String of the costliest beer on tap
*/
function getCostliest(){
return new xmlrpcval(array_keys($beersArray, max($beersArray)), ‘string’);
}
[/php]

SetPrice function wasn’t too difficult, either. Here’s what I came up with.

[php]
/*
* setPrice returns true if set correctly; sets the price for a beer given
* String $beer
* Double $price
*/
function setPrice( $beer, $price ){
if($beersArray[$beer]){ //if the beer exists in the array
$beersArray[$beer] = $price; //set its value to $price
return new xmlrpcval(true, ‘boolean’);
}
else{ //we couldn’t find that beer
return new xmlrpcval(false, ‘boolean’); //so don’t update its price
}
[/php]

Great! So, looks like I have all my methods filled in now. Time to test!
Full PHP server file:
[php]
<?php
include "lib/xmlrpc.inc";
include "lib/xmlrpcs.inc";
include "lib/xmlrpc_wrappers.inc";

$beersArray = array(
‘Bud’ => 6.99,
‘Coors’ => 8.49,
‘Corona’ => 13.99,
‘Genesee’ => 4.99,
‘Guinness Draught’ => 12.99,
‘Labatt’ => 7.99,
‘Sam Adams’ => 12.49
);

/*
* getMethods returns a Vector of methods accessable to a client
*/
function getMethods( $params ) {
$methods = array(
new xmlrpcval(‘getPrice(String beer)’, ‘string’),
new xmlrpcval(‘setPrice(String beer, Double price)’, ‘boolean’),
new xmlrpcval(‘getBeers()’, ‘array’),
new xmlrpcval(‘getCheapest()’, ‘string’),
new xmlrpcval(‘getCostliest()’, ‘string’)
);
return new xmlrpcresp( new xmlrpcval($methods), "array" );
}

/*
* getPrice returns a Double price for a beer
*/
function getPrice( $beer ){
return new xmlrpcval(array_search( $beer, $beersArray ), ‘double’);
}

/*
* setPrice returns true if set correctly; sets the price for a beer given
* String $beer
* Double $price
*/
function setPrice( $beer, $price ){
if($beersArray[$beer]){ //if the beer exists in the array
$beersArray[$beer] = $price; //set its value to $price
return new xmlrpcval(true, ‘boolean’);
}
else{ //we couldn’t find that beer
return new xmlrpcval(false, ‘boolean’); //so don’t update its price
}
}

/*
* getBeers accepts no parameters, returns a Vector of beers and their prices
*/
function getBeers() {
return new xmlrpcval($beersArray, ‘array’);
}

/*
* getCheapest accepts no parameters, returns a String of the cheapest beer on tap
*/
function getCheapest(){
return new xmlrpcval(array_keys($beersArray, min($beersArray)), ‘string’);
}

/*
* getCostliest accepts no parameters, returns a String of the costliest beer on tap
*/
function getCostliest(){
return new xmlrpcval(array_keys($beersArray, max($beersArray)), ‘string’);
}

//declare signature, provide documentation
//the php server supports remote introspection
$getMethods_sig = array(array($xmlrpcArray));
$getMethods_doc = "Returns a Vector of methods accessable to a client.";

$getPrice_sig = array(array($xmlrpcArray, $xmlrpcDouble));
$getPrice_doc = "Returns a Double price for a given beer.";

$setPrice_sig = array(array($xmlrpcArray, $xmlrpcString, $xmlrpcDouble));
$setPrice_doc = "Returns true if set correctly; sets the price for a beer given.";

$getBeers_sig = array(array($xmlrpcArray));
$getBeers_doc = "Returns a Vector of the beers on tap";

$getCheapest_sig = array(array($xmlrpcArray));
$getCheapest_doc = "Returns a String of the cheapest beer on tap";

$getCostliest_sig = array(array($xmlrpcArray));
$getCostliest_doc = "Returns a String of the costliest beer on tap";

new xmlrpc_server(array(‘beer.getMethods’ =>
array(
‘function’ => ‘getMethods’,
‘signature’ => $getMethods_sig,
‘docstring’ => $getMethods_doc),
‘beer.getPrice’ =>
array(
‘function’ => ‘getPrice’,
‘signature’ => $getPrice_sig,
‘docstring’ => $getPrice_doc),
‘beer.setPrice’ =>
array(
‘function’ => ‘setPrice’,
‘signature’ => $setPrice_sig,
‘docstring’ => $setPrice_doc),
‘beer.getBeers’ =>
array(
‘function’ => ‘getBeers’,
‘signature’ => $getBeers_sig,
‘docstring’ => $getBeers_doc),
‘beer.getCheapest’ =>
array(
‘function’ => ‘getCheapest’,
‘signature’ => $getCheapest_sig,
‘docstring’ => $getCheapest_doc),
‘beer.getCostliest’ =>
array(
‘function’ => ‘getCostliest’,
‘signature’ => $getCostliest_sig,
‘docstring’ => $getCostliest_doc)
));

?>
[/php]

Author: Sam

Tinkerer. I like making things.

Leave a Reply

Your email address will not be published. Required fields are marked *