Skip to content

Commit

Permalink
Merge pull request #7 from tine20/pu/appointmentReplyTime
Browse files Browse the repository at this point in the history
add boolean type, support appointmentReplyTime
  • Loading branch information
pschuele committed Jan 24, 2019
2 parents 3bc0d55 + dfb2553 commit 1809f2b
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 149 deletions.
155 changes: 83 additions & 72 deletions lib/Syncroton/Model/AXMLEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package Syncroton
* @subpackage Model
* @license http://www.tine20.org/licenses/lgpl.html LGPL Version 3
* @copyright Copyright (c) 2012-2012 Metaways Infosystems GmbH (http://www.metaways.de)
* @copyright Copyright (c) 2012-2019 Metaways Infosystems GmbH (http://www.metaways.de)
* @author Lars Kneschke <l.kneschke@metaways.de>
*/

Expand Down Expand Up @@ -55,9 +55,9 @@ public function appendXML(DOMElement $domParrent, Syncroton_Model_IDevice $devic

list ($nameSpace, $elementProperties) = $this->_getElementProperties($elementName);

if ($nameSpace == 'Internal') {
continue;
}
if ($nameSpace === 'Internal') {
continue;
}

$elementVersion = isset($elementProperties['supportedSince']) ? $elementProperties['supportedSince'] : '12.0';

Expand All @@ -75,18 +75,25 @@ public function appendXML(DOMElement $domParrent, Syncroton_Model_IDevice $devic
$element->appendChild($subElement);
}
$domParrent->appendChild($element);
} else if ($elementProperties['type'] == 'container' && !empty($elementProperties['multiple'])) {
} elseif ($elementProperties['type'] === 'container' && !empty($elementProperties['multiple'])) {
foreach ($value as $element) {
$container = $domParrent->ownerDocument->createElementNS($nameSpace, ucfirst($elementName));
$element->appendXML($container, $device);
$domParrent->appendChild($container);
}
} else if ($elementProperties['type'] == 'none') {
} elseif ($elementProperties['type'] === 'none') {
if ($value) {
$element = $domParrent->ownerDocument->createElementNS($nameSpace, ucfirst($elementName));
$domParrent->appendChild($element);
}
} else {
if ($elementProperties['type'] === 'boolean') {
if ($value) {
$value = '1';
} else {
$value = '0';
}
}
$element = $domParrent->ownerDocument->createElementNS($nameSpace, ucfirst($elementName));
$this->_appendXMLElement($device, $element, $elementProperties, $value);
$domParrent->appendChild($element);
Expand All @@ -106,67 +113,67 @@ public function getProperties($selectedNamespace = null)
if ($selectedNamespace !== null && $namespace != $selectedNamespace) {
continue;
}
$properties = array_merge($properties, array_keys($namespaceProperties));
$properties = array_merge($properties, array_keys($namespaceProperties));
}

return $properties;

}

/**
* set properties from SimpleXMLElement object
*
* @param SimpleXMLElement $xmlCollection
* @throws InvalidArgumentException
*/
public function setFromSimpleXMLElement(SimpleXMLElement $properties)
{
/**
* set properties from SimpleXMLElement object
*
* @param SimpleXMLElement $xmlCollection
* @throws InvalidArgumentException
*/
public function setFromSimpleXMLElement(SimpleXMLElement $properties)
{
if (!in_array($properties->getName(), (array) $this->_xmlBaseElement)) {
throw new InvalidArgumentException('Unexpected element name: ' . $properties->getName());
}

throw new InvalidArgumentException('Unexpected element name: ' . $properties->getName());
}

foreach (array_keys($this->_properties) as $namespace) {
if ($namespace == 'Internal') {
continue;
}

$this->_parseNamespace($namespace, $properties);
}

return;
$this->_parseNamespace($namespace, $properties);
}

return;
}

/**
* add needed xml namespaces to DomDocument
*
* @param unknown_type $domParrent
*/
protected function _addXMLNamespaces(DOMElement $domParrent)
protected function _addXMLNamespaces(DOMElement $domParrent)
{
foreach($this->_properties as $namespace => $namespaceProperties) {
// don't add default namespace again
if($domParrent->ownerDocument->documentElement->namespaceURI != 'uri:'.$namespace) {
$domParrent->ownerDocument->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:'.$namespace, 'uri:'.$namespace);
}
}
}
}
}

protected function _appendXMLElement(Syncroton_Model_IDevice $device, DOMElement $element, $elementProperties, $value)
{
if ($value instanceof Syncroton_Model_IEntry) {
$value->appendXML($element, $device);
} else {
if ($value instanceof DateTime) {
if ($value instanceof Syncroton_Model_IEntry) {
$value->appendXML($element, $device);
} else {
if ($value instanceof DateTime) {
$value = $value->format($this->_dateTimeFormat);

} elseif (isset($elementProperties['encoding']) && $elementProperties['encoding'] == 'base64') {
if (is_resource($value)) {

} elseif (isset($elementProperties['encoding']) && $elementProperties['encoding'] == 'base64') {
if (is_resource($value)) {
rewind($value);
$value = stream_get_contents($value);
}
$value = base64_encode($value);
}

$value = stream_get_contents($value);
}
$value = base64_encode($value);
}

if ($elementProperties['type'] == 'byteArray') {
$element->setAttributeNS('uri:Syncroton', 'Syncroton:encoding', 'opaque');
// encode to base64; the wbxml encoder will base64_decode it again
Expand All @@ -179,8 +186,8 @@ protected function _appendXMLElement(Syncroton_Model_IDevice $device, DOMElement
}

$element->appendChild($element->ownerDocument->createTextNode($this->_enforceUTF8($value)));
}
}
}
}
}

/**
Expand Down Expand Up @@ -228,31 +235,31 @@ protected function _enforceUTF8($dirty)
* @throws InvalidArgumentException
* @return multitype:unknown
*/
protected function _getElementProperties($element)
protected function _getElementProperties($element)
{
foreach($this->_properties as $namespace => $namespaceProperties) {
if (array_key_exists($element, $namespaceProperties)) {
return array($namespace, $namespaceProperties[$element]);
}
}

throw new InvalidArgumentException("$element is no valid property of " . get_class($this));
if (array_key_exists($element, $namespaceProperties)) {
return array($namespace, $namespaceProperties[$element]);
}
}

throw new InvalidArgumentException("$element is no valid property of " . get_class($this));
}

protected function _parseNamespace($nameSpace, SimpleXMLElement $properties)
{
// fetch data from Contacts namespace
$children = $properties->children("uri:$nameSpace");

// fetch data from Contacts namespace
$children = $properties->children("uri:$nameSpace");

foreach ($children as $elementName => $xmlElement) {
$elementName = lcfirst($elementName);


if (!isset($this->_properties[$nameSpace][$elementName])) {
continue;
}

list (, $elementProperties) = $this->_getElementProperties($elementName);


switch ($elementProperties['type']) {
case 'container':
if (!empty($elementProperties['multiple'])) {
Expand All @@ -276,9 +283,9 @@ protected function _parseNamespace($nameSpace, SimpleXMLElement $properties)
}
}
} else {
$subClassName = isset($elementProperties['class']) ? $elementProperties['class'] : get_class($this) . ucfirst($elementName);

$property = new $subClassName($xmlElement);
$subClassName = isset($elementProperties['class']) ? $elementProperties['class'] : get_class($this) . ucfirst($elementName);

$property = new $subClassName($xmlElement);
}

break;
Expand All @@ -287,6 +294,10 @@ protected function _parseNamespace($nameSpace, SimpleXMLElement $properties)
$property = new DateTime((string) $xmlElement, new DateTimeZone('UTC'));
break;

case 'boolean':
$property = (bool) ((int) $xmlElement);
break;

case 'number':
$property = (int) $xmlElement;
break;
Expand All @@ -301,28 +312,28 @@ protected function _parseNamespace($nameSpace, SimpleXMLElement $properties)
}

$this->$elementName = $property;
}
}
}

public function &__get($name)
{
$this->_getElementProperties($name);

return $this->_elements[$name];
}

public function &__get($name)
{
$this->_getElementProperties($name);

return $this->_elements[$name];
}

public function __set($name, $value)
{
list ($nameSpace, $properties) = $this->_getElementProperties($name);

public function __set($name, $value)
{
list ($nameSpace, $properties) = $this->_getElementProperties($name);

if ($properties['type'] == 'datetime' && !$value instanceof DateTime) {
throw new InvalidArgumentException("value for $name must be an instance of DateTime");
}
if ($properties['type'] == 'datetime' && !$value instanceof DateTime) {
throw new InvalidArgumentException("value for $name must be an instance of DateTime");
}

if (!array_key_exists($name, $this->_elements) || $this->_elements[$name] != $value) {
if (!array_key_exists($name, $this->_elements) || $this->_elements[$name] != $value) {
$this->_elements[$name] = $value;

$this->_isDirty = true;
}
}
}
}
}
Loading

0 comments on commit 1809f2b

Please sign in to comment.