PHP Classes
elePHPant
Icontem

Package Base: Base classes for implementing a package generator

Recommend this page to a friend!
  Info   View files Documentation   View files View files (27)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-05-23 (5 days ago) RSS 2.0 feedNot enough user ratingsTotal: 118 This week: 14All time: 8,567 This week: 53Up
Version License PHP version Categories
packagebase 1.0.9MIT/X Consortium ...5.3.3PHP 5, Libraries, Data types
Description Author

This package contains base classes for implementing a package generator.

It defines several base classes and interfaces that are implemented and extended by the package generator package.

Currently it provides an AbstractSoapClientBase class, AbstractStructArrayBase class, AbstractStructBase class, and the interfaces SoapClientInterface, StructArrayInterface, StructInterface.

Recommendations

Call SOAP Web service
Problem to call Web service with authentication

SOAP client with Basic Authorization
Need a SOAP client that sends Basic Authorization header

  Performance   Level  
Name: WsdlToPhp <contact>
Classes: 8 packages by
Country: France France
Age: 33
All time rank: 96233 in France France
Week rank: 23 Up2 in France France Up
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

Details

Package Base

Latest Stable Version Total Downloads Build Status PHP 7 ready Scrutinizer Code Quality Code Coverage Dependency Status StyleCI SensioLabsInsight

Main features

This project contains base classes used as parent class by the generated classes from PackageGenerator: - Interfaces

- StructInterface
- StructArrayInterface
- SoapClientInterface

The interfaces

The defined interfaces must be used in order to be able to match the requirements for the PackageGenerator generated classes.

StructInterface

Description

This interface must be used to define a new StructType class.

What has to be implemented?

There is at least/most one method that must be implemented so it's pretty fast to implement it even if you don't see the goal of it: - __set_state: __set_state is useful when you want want to load an object that you stored as a string using var_export.

If you do not want to implement this method, you can too create your own class that inherits from our AbstractStructBase class.

StructArrayInterface

Description

This interface must be used to define a new ArrayType class. The goal is to provide utility methods around Array Structs defined by the Wsdl in order to ease the handling of its content. Therefore, this interface inherits from our StructInterface interface plus the native ArrayAccess, Iterator and Countable PHP interfaces

What has to be implemented?

The only method that must be implemented would be getAttributeName but be aware that it is implemented in every generated ArrayType class so no need to define it. It's just a reminder of what is available in ArrayType classes.

So, basically, you MUST at least override the methods declared by the PHP interfaces from which this interface inherits

If you do not want to implement all the methods, you can too create your own class that inherits from our AbstractStructArrayBase class.

SoapClientInterface

Description

This interface must be used to define a new SoapClient base class for any `ServiceType` class generated by PackageGenerator.

Options

Here are the constants/options defined by this interface and their utility: - DEFAULT_SOAP_CLIENT_CLASS = '\SoapClient': this is the default SoapClient class that is used to send the request. Feel free to override it if you want to use another SoapClient class - OPTION_PREFIX: this is the prefix used for any constant's option name - WSDL_URL: option index used to pass the WSDL url - WSDL_CLASSMAP: the classmap's array - WSDL_LOGIN: the basic authentication's login - WSDL_PASSWORD: the basic authentication's password - WSDL_TRACE: tracing of request so faults can be backtraced. This defaults to `true` - WSDL_EXCEPTIONS: boolean value defining whether soap errors throw exceptions of type SoapFault - WSDL_CACHE_WSDL: option is one of `WSDL_CACHE_NONE`, `WSDL_CACHE_DISK`, `WSDL_CACHE_MEMORY` or `WSDL_CACHE_BOTH` - WSDL_STREAM_CONTEXT: a resource for context - WSDL_SOAP_VERSION: one of either `SOAP_1_1` or `SOAP_1_2` to select SOAP 1.1 or 1.2, respectively. If omitted, SOAP 1.1 is used - WSDL_COMPRESSION: allows to use compression of HTTP SOAP requests and responses - WSDL_ENCODING: internal character encoding. This option does not change the encoding of SOAP requests (it is always utf-8), but converts strings into it - WSDL_CONNECTION_TIMEOUT: defines a timeout in seconds for the connection to the SOAP service. This option does not define a timeout for services with slow responses. To limit the time to wait for calls to finish the default_socket_timeout setting is available - WSDL_TYPEMAP: array of type mappings. Type mapping is an array with keys type_name, type_ns (namespace URI), from_xml (callback accepting one string parameter) and to_xml (callback accepting one object parameter) - WSDL_USER_AGENT: specifies string to use in User-Agent header - WSDL_FEATURES: a bitmask of `SOAP_SINGLE_ELEMENT_ARRAYS`, `SOAP_USE_XSI_ARRAY_TYPE`, `SOAP_WAIT_ONE_WAY_CALLS` - WSDL_KEEP_ALIVE: a boolean value defining whether to send the Connection: Keep-Alive header or Connection: close - WSDL_PROXY_HOST: your pxoxy hostname - WSDL_PROXY_PORT: your proxy port - WSDL_PROXY_LOGIN: your proxy login - WSDL_PROXY_PASSWORD: your proxy password - WSDL_LOCAL_CERT: your local certificate content (as a string) - WSDL_PASSPHRASE: your local passphrase content (as a string) - WSDL_AUTHENTICATION: authentication method may be either `SOAP_AUTHENTICATION_BASIC` (default) or `SOAP_AUTHENTICATION_DIGEST` - WSDL_SSL_METHOD: one of `SOAP_SSL_METHOD_TLS`, `SOAP_SSL_METHOD_SSLv2`, `SOAP_SSL_METHOD_SSLv3` or `SOAP_SSL_METHOD_SSLv23`

What has to be implemented?

Here are the methods that must be implemented and why: - __construct(array $wsdlOptions = array(), $resetSoapClient = true): the constructor must be able to handl one of the listed constants above - getSoapClient(): must return the SoapClient object that is responsible fo sending the requests. - setSoapHeader($nameSpace, $name, $data, $mustUnderstand = false, $actor = null): look to AbstractSoapClientBase part that details this method. Basically, it allows to define SoapHeaders for the request - getLastError(): must return the last error, its format is up to you - saveLastError($methodName, \SoapFault $soapFault): look to AbstractSoapClientBase part that details this method. Basically, it must allow to store a catched Soapfault object when a request has failed - getResult(): should return the Soap Web Service response, it's up to you - setResult($result): must accept any parameter type as it should received the Soap Web Service response

If you do not want to implement all these methods, you can too create your own class that inherits from our AbstractSoapClientBase class.

The abstract classes

AbstractStructBase

Description

This class is the base class for any `StructType` class generated by PackageGenerator. It implements our StructInterface interface. It defines three methods: - __set_state($array): Useful when you load the string representation of an object that you stored using `var_export`. It also allows you to ease the instanciation of an object that contains many properties which would be hard to instanciate using the `__construct` method. You can see `__set_state` as an hydratation method. - _set($name, $value): As magic method `__set` but used by the `__set_state` method. Plus, defining `__set` method on used class by the classmap option for the SoapClient breaks the correct hydratation of your received objects. - _get($name): As magic method `__get`. Used by our AbstractStructArrayBase class

Usage

$item = \Api\StructType\Item::__set_state(array(
    'id' => 1,
    'name' => 'Entity #1',
    'label' => 'Entity #1',
    '_href' => 'http://www.entity.com',
));
// $item is now an \Api\StructType\Item object

AbstractStructArrayBase

Description

This class is the base class for any `ArrayType` class generated by PackageGenerator. It implements our StructArrayInterface interface.

Usage

As soon as you have an element that is an array of items such as:

$items = \Api\ArrayType\Items::__set_state(array(
    'items' => array(
        \Api\StructType\Item::__set_state(array(
            'id' => 1,
            'name' => 'Entity #1',
            'label' => 'Entity #1',
            '_href' => 'http://www.entity-1.com',
        ),
        \Api\StructType\Item::__set_state(array(
            'id' => 2,
            'name' => 'Entity #2',
            'label' => 'Entity #2',
            '_href' => 'http://www.entity-2.com',
        ),
        \Api\StructType\Item::__set_state(array(
            'id' => 3,
            'name' => 'Entity #3',
            'label' => 'Entity #3',
            '_href' => 'http://www.entity-3.com',
        ),
    )
));
// 'items' is the unique property of the object
// Its name is returned by the getAttributeName method
// defined in the generated \Api\ArrayType\Items class
  • You MUST call first `initInternArray` method on your ArrayType object otherwise you'll get nothing working for the implemented methods: `php $items->initInternArray(); `
  • then you can call `count`, `length` methods: gives you the number of items contained by your object
  • you can iterate through the items: `php foreach ($items as $item) { // $items->current() and $item is an \Api\StructType\Item object // $items->key() is the current index } `
  • you can get the first item: `php $items->first(); `
  • you can get the last item: `php $items->last(); `
  • you can get any item: `php $items->item($index); `
  • you can add a new item: `php $items->add(\Api\StructType\Item::__set_state(array( 'id' => 4, 'name' => 'Entity #4', 'label' => 'Entity #4', '_href' => 'http://www.entity-4.com', ))); `
  • you can even reset the items: `php $items->initInternArray(array( \Api\StructType\Item::__set_state(array( 'id' => 0, 'name' => 'Entity #0', 'label' => 'Entity #0', '_href' => 'http://www.entity-0.com', ), \Api\StructType\Item::__set_state(array( 'id' => 1, 'name' => 'Entity #1', 'label' => 'Entity #1', '_href' => 'http://www.entity-1.com', ), \Api\StructType\Item::__set_state(array( 'id' => 2, 'name' => 'Entity #2', 'label' => 'Entity #2', '_href' => 'http://www.entity-2.com', ), )); `

AbstractSoapClientBase

Description

This class is the base class for any `ServiceType` class generated by PackageGenerator. Its goal is to provide utility/handful methods by implementing our SoapClientInterface interface. It's basically a decorator design pattern as the class has the SoapClient object as a static property in order to be able to apply methods on it. It is a static property in order to have a singleton between multiple calls (allowing to send cookies automatically between calls). It can be reset by passing true as the second parameter.

Usage

Let's say you have this type of generate `ServiceType` class:

namespace Api\ServiceType;
use \WsdlToPhp\PackageBase\AbstractSoapClientBase;
class ApiUpdate extends AbstractSoapClientBase
{
    public function UpdateBulkOrder(\Api\StructType\ApiUpdateBulkOrder $parameters)
    {
        try {
            $this->setResult(self::getSoapClient()->UpdateBulkOrder($parameters));
            return $this->getResult();
        } catch (\SoapFault $soapFault) {
            $this->saveLastError(__METHOD__, $soapFault);
            return false;
        }
    }
}

You can do:

use \WsdlToPhp\PackageBase\AbstractSoapClientBase;
$options = array(
    AbstractSoapClientBase::WSDL_URL => '__WSDL_URL__',
    AbstractSoapClientBase::WSDL_CLASSMAP => \Api\ApiClassMap::classMap(),
);
// sets the first instance of SoapClient within  AbstractSoapClientBase
$update = new \Api\ServiceType\ApiUpdate($options);
// resets the SoapClient instance
$update = new \Api\ServiceType\ApiUpdate($options, true);

Then call any of these base methods: - getResult: return the actual response as an object. The object's class should be a generated class - getLastRequest($asDomDocument = false): returns either the XML string version or the `DOMDocument` version of the request - getLastResponse($asDomDocument = false): returns either the XML string version or the `DOMDocument` version of the response - getLastRequestHeaders($asArray = false): returns either the HTTP request's headers as a string or as an array (each HTTP header is parsed) - getLastResponseHeaders($asArray = false): returns either the HTTP response's headers as a string or as an array - getLastError: automatically populated with an error when `$this->saveLastError(__METHOD__, $soapFault)` is called - getLastErrorForMethod($methodName) : returns the error associated to the called method. It should return a `SoapFault` object

$result = $update->UpdateBulkOrder(new \Api\StructType\ApiUpdateBulkOrder())
if ($result !== false) {
    echo "\nThis is the result as an object:" . print_r($update->getResult(), true);
    // Actually $result is the same data than $update->getResult()
} else {
    echo "\nThis is the XML request:" . $update->getLastRequest(false);
    echo "\nThese are the request's headers:" . $update->getLastRequestHeaders(false);
    echo "\nThis is the XML response:" . $update->getLastResponse(false);
    echo "\nThese are the response's headers:" . $update->getLastResponseHeaders(false);
    echo "\nThese are the last errors:" . print_r($update->getLastError(), true);
    echo "\nThis is the current error:" . print_r($update->getLastErrorForMethod('\Api\ServiceType\ApiUpdate::UpdateBulkOrder'), true);
}

You have additional methods such as: - setSoapHeader($nameSpace, $name, $data, $mustUnderstand = false, $actor = null): it provides a way to redefine SoapHeaders

// A sample of its usage in the generated ServiceType class
public function setSoapHeaderCSPCHD(\Api\StructType\ApiCSPCHD $cSPCHD, $nameSpace = 'http://tempuri.org', $mustUnderstand = false, $actor = null)
{
    return $this->setSoapHeader($nameSpace, 'CSPCHD', $cSPCHD, $mustUnderstand, $actor);
}
  • setHttpHeader($headerName, $headerValue): an easy way to define your proper HTTP headers that must be sent
  • setLocation($location): Sets the location of the Web service to use
  • getStreamContext(): Returns the created stream context used by the SoapClient class
  • getStreamContextOptions(): Returns the created stream context's options used by the SoapClient class

Need improvements for these classes?

Feel free to make some pull requests. We'll study them and let you know when it can be integrated.

Unit tests

You can run the unit tests with the following command:

    $ cd /path/to/src/WsdlToPhp/PackageBase/
    $ composer install
    $ phpunit
  Files folder image Files  
File Role Description
Files folder imagesrc (7 files)
Files folder imagetests (9 files, 1 directory)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .php_cs Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file AbstractSoapClientBase.php Class Class source
  Plain text file AbstractStructArrayBase.php Class Class source
  Plain text file AbstractStructBase.php Class Class source
  Plain text file SoapClientInterface.php Class Class source
  Plain text file StructArrayInterface.php Class Class source
  Plain text file StructInterface.php Class Class source
  Plain text file Utils.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageresources (3 files)
  Accessible without login Plain text file Client.php Test Unit test script
  Accessible without login Plain text file SoapClient.php Test Unit test script
  Accessible without login Plain text file SoapClientTest.php Test Unit test script
  Accessible without login Plain text file StructArrayObject.php Test Unit test script
  Accessible without login Plain text file StructArrayTest.php Test Unit test script
  Accessible without login Plain text file StructBaseTest.php Test Unit test script
  Accessible without login Plain text file StructObject.php Test Unit test script
  Accessible without login Plain text file TestCase.php Test Unit test script
  Accessible without login Plain text file UtilsTest.php Test Unit test script

  Files folder image Files  /  tests  /  resources  
File Role Description
  Accessible without login Plain text file bingsearch.wsdl Data Auxiliary data
  Accessible without login Plain text file formated.xml Data Auxiliary data
  Accessible without login Plain text file oneline.xml Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:118
This week:14
All time:8,567
This week:53Up