Ejemplo n.º 1
0
function test($ns, $prompt, $param, $default = null, $validator = null, $sanitizer = null)
{
    static $cx = null;
    if (is_null($cx)) {
        $cx = new CX();
    }
    echo "<dt>Get {$param} with {$prompt}: </dt><dd>";
    try {
        print_r($cx->ns($ns)->getValue($param, $default, $validator, $sanitizer));
    } catch (Exception $e) {
        echo $e->getMessage();
    }
    echo '</dd>';
}
 public function testLocalVar()
 {
     $myvar = 'ok';
     // define a variable in local scope
     $v = CX::factory(get_defined_vars())->ns(CX::LOCAL)->getValue('myvar');
     $this->assertEquals($v, 'ok');
 }
 public function get()
 {
     $context = Context::factory();
     // get default parameters from config
     $config = $context->ns('geocodit');
     $defaultProfile = $config->getValue('trust', 'geocodit', $this->VALID_PROFILE());
     $defaultAddress = $config->getValue('defaultAddress', 'Via Montefiori 13, Esino Lario', FILTER_SANITIZE_STRING);
     $penality = $config->getValue('penality', 2, $this->VALID_PENALITY());
     // get input patrameters from URL quesry string
     $input = $context->ns(INPUT_GET);
     $query = $input->getValue('q', $defaultAddress, FILTER_SANITIZE_STRING);
     $profile = $input->getValue('trust', $defaultProfile, self::VALID_PROFILE());
     $geocoder = new \Geocoder\ProviderAggregator();
     $adapter = new \Ivory\HttpAdapter\CurlHttpAdapter();
     // chain all supported providers
     $chain = new \Geocoder\Provider\Chain(array());
     foreach ($this->PROFILES[$profile] as $providerName) {
         $provider = $this->geocoderFactory($adapter, trim($providerName));
         $chain->add($provider);
     }
     $geocoder->registerProvider($chain);
     // Call toponym resolution providers
     $address = $geocoder->limit(1)->geocode($query)->first();
     // apply penality (just to avoid abuse, set to 0 in config file to disable)
     if ($penality > 0) {
         usleep($penality * 1000000);
     }
     return $this->stateTransfer($address);
 }
 public function __construct($options = null)
 {
     if (!is_array($options)) {
         $options = array();
     }
     parent::__construct(array_merge($this->defaults, $options));
     $queryString = $this->ns(INPUT_GET);
     $options = $this->ns(self::LOCAL);
     $ps = $queryString->getValue($options->getValue('pslabel'), 0, FILTER_VALIDATE_INT);
     $p = $queryString->getValue($options->getValue('plabel'), -1, FILTER_VALIDATE_INT);
     $this->pagedRequested = $ps > 0 || $p >= 0;
     $this->pagenum = max(0, $p);
     $this->pagesize = $ps < 1 ? $options->getValue('pagesize') : $ps;
 }
 public function get()
 {
     $context = Context::factory();
     // get default parameters from config
     $config = $context->ns('geocodit');
     $defaultAddress = $config->getValue('defaultAddress', 'Via Montefiori 13, Esino Lario');
     $penality = $config->getValue('penality', 2);
     // get input patrameters from URL quesry string
     $input = $context->ns(INPUT_GET);
     $query = $input->getValue('q', $defaultAddress);
     $adapter = new \Ivory\HttpAdapter\CurlHttpAdapter();
     $benchmark = new Benchmark($query);
     foreach (explode('|', self::SUPPORTED_PROVIDERS) as $providerName) {
         $provider = $this->geocoderFactory($adapter, trim($providerName));
         $benchmark->compare($provider);
     }
     usleep($penality * 1000000);
     return $this->stateTransfer($benchmark);
 }
 public function geocoderFactory($adapter, $providerName)
 {
     $context = Context::factory();
     $input = $context->ns(INPUT_GET);
     $config = $context->ns('geocodit');
     $locale = 'it_IT';
     switch ($providerName) {
         case 'geocoditOSM':
             $endpoint = $config->getValue('endpoint', 'https://hub1.linkeddata.center/demo');
             $kbid = $config->getValue('kbid', 'demo');
             $secretKey = $config->getValue('secretKey', 'demo');
             $geocoder = new \Geocodit\Provider\GeocoditOSM($adapter, $kbid, $secretKey, $endpoint);
             break;
         case 'geocodit':
             $endpoint = $config->getValue('endpoint', 'https://hub1.linkeddata.center/demo');
             $kbid = $config->getValue('kbid', 'demo');
             $secretKey = $config->getValue('secretKey', 'demo');
             $geocoder = new \Geocodit\Provider\Geocodit($adapter, $kbid, $secretKey, $endpoint);
             break;
         case 'google_maps':
             $googleApiKey = $config->getValue('googleApiKey', V::NULL_AS_DEFAULT);
             $geocoder = new \Geocoder\Provider\GoogleMaps($adapter, $locale, 'Italy', true, $googleApiKey);
             break;
         case 'openstreetmap':
             $geocoder = new \Geocoder\Provider\OpenStreetMap($adapter, $locale);
             break;
         case 'bing_maps':
             $bingApiKey = $config->getValue('bingApiKey', 'here_your_bing_api_key');
             $geocoder = new \Geocoder\Provider\BingMaps($adapter, $bingApiKey, $locale);
             break;
         default:
             throw new \InvalidArgumentException(sprintf('Unknown geocoder provider name "%s".', $providerName));
             break;
     }
     return $geocoder;
 }
Ejemplo n.º 7
0
 * limitations under the License.
 * 
 */
require '../../vendor/autoload.php';
use BOTK\Core\EndPointFactory, BOTK\Core\ErrorManager;
// Control errors
use Geocodit\View\GoogleAnalyticsEnabledRenderer;
// for CSS
use BOTK\Context\Context;
// get config vars and other inputs
use BOTK\Context\ContextNameSpace as CX;
// search configs files in  in config and /etc/geocodit directories
if (!isset($_ENV['BOTK_CONFIGDIR'])) {
    if (file_exists(__DIR__ . '/../../config/geocodit.ini')) {
        $_ENV['BOTK_CONFIGDIR'] = realpath(__DIR__ . '/../../config');
    } elseif (is_dir('/etc/geocodit')) {
        $_ENV['BOTK_CONFIGDIR'] = '/etc/geocodit';
    }
}
// Enable Universal Analytics code
$UA = Context::factory()->ns('geocodit')->getValue('UA', CX::NULL_AS_DEFAULT);
GoogleAnalyticsEnabledRenderer::$UniversalAnalyticsId = $UA;
// Enable the catching of PHP errors
$errorManager = ErrorManager::getInstance()->registerErrorHandler();
try {
    $endpoint = EndPointFactory::make('Geocodit\\ApiEndpoint');
    $result = $endpoint->run();
} catch (Exception $e) {
    $result = ErrorManager::getInstance()->render($e);
}
echo $result;
Ejemplo n.º 8
0
<?php

require '../vendor/autoload.php';
DEFINE('DEMO_ENDPOINT', 'https://hub1.linkeddata.center/demo');
// search configs files in  in config and /etc/geocodit directories
if (!isset($_ENV['BOTK_CONFIGDIR'])) {
    if (file_exists(__DIR__ . '/../config/geocodit.ini')) {
        $_ENV['BOTK_CONFIGDIR'] = realpath(__DIR__ . '/../config');
    } elseif (is_dir('/etc/geocodit')) {
        $_ENV['BOTK_CONFIGDIR'] = '/etc/geocodit';
    }
}
$config = \BOTK\Context\Context::factory()->ns('geocodit');
$penality = $config->getValue('penality', 2);
$endpoint = $config->getValue('endpoint', DEMO_ENDPOINT);
$defaultAddress = $config->getValue('defaultAddress', 'Via Montefiori 13, Esino Lario');
// test google maps and bing maps credentials
$googleApiKeyNotSetWarning = $config->getValue('googleApiKey', '') ? '' : "<li style='color: red'>WARNING: google maps api key not available (continue at your risk)</li>";
$bingApiKeyNotSetWarning = $config->getValue('bingApiKey', '') ? '' : "<li style='color: red' >WARNING: bing maps api key not available, bing maps will return errors</li>";
// Enable Universal Analytics code
$UASnippet = \Geocodit\View\GoogleAnalyticsEnabledRenderer::GoogleAnalyticsSnippet($config->getValue('UA', ''));
$passwordHint = $endpoint == DEMO_ENDPOINT ? ' (demo/demo)' : '';
?>
<!DOCTYPE html>
<html>
    <head>
		<meta charset='utf-8'>
		<link rel='stylesheet' type='text/css' href='http://linkeddata.center/resources/v4/css/doc.css'/>
		<style type='text/css'>
		    main {
		        font-family: monospace;
Ejemplo n.º 9
0
 public function testNoNamespace()
 {
     $v = CX::factory()->ns('nosample')->getValue('novar', 1);
     $this->assertEquals($v, 1);
 }