<?php

define('DDS_USERNAME', 'your dds username');
define('DDS_PASSWORD', 'your dds password');

define('DDS_API', 'https://%s:%s@secure.dds.nl/api');

require_once('nusoap/nusoap.php');

class dds_client
{
    private $client;

    private $error_code;
    private $error_msg;

    public function __construct()
    {
        $this->client = new nusoap_client(sprintf(DDS_API, urlencode(DDS_USERNAME), urlencode(DDS_PASSWORD)));
    }

    public function error()
    {
        return $this->error_code;
    }

    public function error_msg()
    {
        return $this->error_msg;
    }

    public function _process_error($e)
    {
        if ($e) {
            $p = explode(':', $e, 2);

            if (is_numeric($p[0])) {
                $this->error_code = $p[0];
                $this->error_msg  = trim(array_pop($p));
            } else {
                $this->error_code = -1;
                $this->error_msg  = $e;
            }
        } else {
            $this->error_code = 0;
            $this->error_msg  = '';
        }
    }

    public function _process_args(&$arg)
    {
        $arg = new soapval(false, false, $arg);
    }

    public function __call($method, $args)
    {
        array_walk($args, array($this, '_process_args'));

        $res = $this->client->call($method, $args);

        $this->_process_error($this->client->getError());

        return $res;
    }
}
