Namespace, classes and aliases (uses)

In non-trivial applications, your'e probably using namespaces and wants to pass i.e. framework objects as models back and forth between your server and peers without convert them.

Add class path

For reflection to work with namespaces, these has to be added so that the service description object knows about them.

<?php

use UUP\WebService\Soap\SoapService;

/**
 * Setup class path.
 * @param SoapService $service The SOAP service.
 */
function setupClassPath1($service)
{
        $description = $service->getServiceDescription();

        $description->addClassPath('OpenExam\Models');
        $description->addClassPath('OpenExam\Library\Security');
        $description->addClassPath('OpenExam\Library\WebService\Soap\Types');
}

/**
 * Setup class path.
 * @param SoapService $service The SOAP service.
 */
function setupClassPath2($service)
{
        $description = $service->getServiceDescription();

        $description->setClassPath([
                'OpenExam\Models',
                'OpenExam\Library\Security',
                'OpenExam\Library\WebService\Soap\Types'
        ]);
}

Add class map

When alising classes in use statement and using for input parameters or return types in your handler its no longer possible for the service description object to resolve them to a known class, and class reflcetion will fail.

The solution is to add mapping between class alias and real class names. Combined the registration with addClassPath()/setClassPath() to add directories to resolve classes from:

<?php

use UUP\WebService\Soap\SoapService;

/**
 * Setup class map.
 * @param SoapService $service The SOAP service.
 */
function setupClassMap1($service)
{
        $description = $service->getServiceDescription();

        $description->addClassMap('MyExam', 'OpenExam\Models\Exam');
        $description->addClassMap('Caller', 'OpenExam\Library\Security\User');
}

/**
 * Setup class map.
 * @param SoapService $service The SOAP service.
 */
function setupClassMap2($service)
{
        $description = $service->getServiceDescription();

        $description->setClassPath([
                'MyExam' => 'OpenExam\Models\Exam',
                'Caller' => 'OpenExam\Library\Security\User'
        ]);
}

/**
 * Setup class map.
 * @param SoapService $service The SOAP service.
 */
function setupClassMap3($service)
{
        $description = $service->getServiceDescription();

        $description->addClassPath('OpenExam\Models');
        $description->addClassPath('OpenExam\Library\Security');

        $description->addClassMap('MyExam', 'Exam');
        $description->addClassMap('Caller', 'User');
}

The service description object should now have enouch information for loading classes for reflection even when using custom objects defined in namespaces or classes using aliases:

<?php

use OpenExam\Library\Security\User as Caller;
use OpenExam\Models\Exam as MyExam;

class MySoapHandler implements UUP\WebService\Soap\SoapHandler
{

        /**
         * Add moderator on exam.
         * @param Caller $user The calling user.
         * @param MyExam $exam The exam.
         */
        public function addModerator($user, $exam)
        {
                $exam->moderator = $user->getPrincipalName();
                $exam->save([
                        'throws'  => true,
                        'hydrate' => false
                ]);
        }

}