Here is some PHP sample code we have been working on for a customer. I believe it can be used to get an idea on how to address conditional queries in PHP.
Yaron.
<?php
class Base
{
public function __construct()
{
}
}
class Condition extends Base
{
public function __construct()
{
parent::__construct();
}
}
class Expression extends Base
{
public function __construct()
{
parent::__construct();
}
}
class FieldExpression extends Expression
{
/*
<xs:element minOccurs="0" name="FieldName" nillable="true" type="xs:string"/>
*/
//string field name
public $FieldName;
public function __construct($fieldName)
{
parent::__construct();
$this->FieldName = (string)$fieldName;
}
}
class ConstantExpression extends Expression
{
/*
<xs:element minOccurs="0" name="Value" nillable="true" type="xs:anyType"/>
*/
//any type field value
public $Value;
public function __construct($value)
{
parent::__construct();
$this->Value = new SoapVar($value, XSD_STRING, 'string', " http://www.w3.org/2001/XMLSchema");;
}
}
class LeftExpression extends Expression
{
}
class RightExpression extends Expression
{
}
class Operator extends Base
{
/*
<xs:restriction base="xs:string">
<xs:enumeration value="Equal"/>
<xs:enumeration value="NotEqual"/>
<xs:enumeration value="LessThan"/>
<xs:enumeration value="GreaterThan"/>
<xs:enumeration value="LessThanOrEqual"/>
<xs:enumeration value="GreaterThanOrEqual"/>
<xs:enumeration value="BeginsWith"/>
<xs:enumeration value="EndsWith"/>
<xs:enumeration value="Contains"/>
<xs:enumeration value="DoesNotContain"/>
<xs:enumeration value="In">
In is only supported in compare operations where the left side is a FieldExpression which represents a "reference to entity" field and the right side is a QueryExpression
*/
//string Operator type
public $Value;
public function __construct($operatorType)
{
parent::__construct();
$this->Value = (string)$operatorType;
}
}
class Compare extends Condition
{
/*
<xs:element minOccurs="0" name="LeftExpression" nillable="true" type="tns:Expression"/>
<xs:element name="Operator" type="tns:Operator"/>
<xs:element minOccurs="0" name="RightExpression" nillable="true" type="tns:Expression"/>
*/
//instance of Expression
public $LeftExpression;
//instance of Expression
public $RightExpression;
//instance of Operator
public $Operator;
public function __construct()
{
parent::__construct();
}
}
CLASS EntityQuery extends Base
{
/*
<xs:element xmlns:q11=" [http://clarizen.com/api](http://clarizen.com/api)" minOccurs="0" name="Fields" nillable="true" type="q11:stringList"/>
<xs:element minOccurs="0" name="Orders" nillable="true" type="tns:ArrayOfOrderBy"/>
<xs:element minOccurs="0" name="TypeName" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="Where" nillable="true" type="tns:Condition"/>
*/
//array of strings of fieldsName
public $Fields;
//instance of ArrayOfOrderBy
//public $Orders;
//instance of Condition
public $Where;
//string query type
public $TypeName;
public function __construct(array $fieldsName, $typeName, $cond)
{
parent::__construct();
$this->Fields = $fieldsName;
$this->TypeName = (string)$typeName;
$this->Where = $cond;
}
}
class EntityQueryManager
{
public static function getProjectEntityQuery()
{
$queryType = 'Project';
$fieldName = 'Name';
$compare = new Compare();
$compare->LeftExpression = new SoapVar(new FieldExpression($fieldName), SOAP_ENC_OBJECT, 'FieldExpression', " http://clarizen.com/api/queries");
$compare->RightExpression = new SoapVar(new ConstantExpression('Commercial System'), SOAP_ENC_OBJECT, 'ConstantExpression', " http://clarizen.com/api/queries");
$compare->Operator = 'Equal';
$compareSoapVar = new SoapVar($compare, SOAP_ENC_OBJECT, 'Compare', " http://clarizen.com/api/queries");
return new EntityQuery(array($fieldName), $queryType, $compareSoapVar);
}
}
//LOGIN PROCCESS
$soapUrl = ' https://api.clarizen.com/v1.0/Clarizen.svc?WSDL';
$soapApiUrl = ' http://clarizen.com/api';
$soapConfig = array('trace' => 1, 'exceptions' => 1);
$request = array();
$params = array(
'userName' => 'chilik.hochberg1',
'password' => 'clarizen1234'
);
try{
$client = new SoapClient($soapUrl, $soapConfig);
$response = $client->Login($params);
$sessionId = $response->LoginResult->SessionId;
$header = new SoapHeader($soapApiUrl, 'Session', array("ID"=>$sessionId));
$client->__setSoapHeaders($header);
$request[] = new SoapVar(EntityQueryManager::getProjectEntityQuery(), SOAP_ENC_OBJECT, 'EntityQuery', ' http://clarizen.com/api/queries');
//print_r($request);die;
$result = $client->Execute(array("request"=>$request));
print_r($result);exit;
}catch(SoapFault $e){
var_dump($e->getMessage());
//header ("Content-Type:text/xml");
//echo $client->__getLastRequest();
//echo $client->__getLastResponse();
}