[Pkg-owncloud-commits] [php-sabre-vobject] 292/341: Lots of tweaks to work with the new system. WIP
David Prévot
taffit at moszumanska.debian.org
Tue Aug 11 13:35:58 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository php-sabre-vobject.
commit 9b30193fda189a89bc29dee4b9c0a582e13bf521
Author: Evert Pot <me at evertpot.com>
Date: Fri Jul 3 20:23:48 2015 -0400
Lots of tweaks to work with the new system. WIP
---
lib/Component.php | 183 +++++++++++++-----------
lib/Component/VCalendar.php | 109 +++++++++-----
lib/Component/VCard.php | 4 +-
tests/VObject/Component/VAvailabilityTest.php | 5 +
tests/VObject/ITip/BrokerProcessMessageTest.php | 8 +-
tests/VObject/ITip/BrokerTester.php | 17 +--
tests/VObject/TestCase.php | 8 +-
7 files changed, 192 insertions(+), 142 deletions(-)
diff --git a/lib/Component.php b/lib/Component.php
index 7d5b8bd..d36358c 100644
--- a/lib/Component.php
+++ b/lib/Component.php
@@ -114,17 +114,12 @@ class Component extends Node {
throw new \InvalidArgumentException('The second argument must not be specified, when passing a VObject Node');
}
$a1->parent = $this;
- $this->children[] = $a1;
-
- return $a1;
+ $newNode = $a1;
} elseif (is_string($a1)) {
- $item = $this->root->create($a1, $a2, $a3);
- $item->parent = $this;
- $this->children[] = $item;
-
- return $item;
+ $newNode = $this->root->create($a1, $a2, $a3);
+ $newNode->parent = $this;
} else {
@@ -132,6 +127,14 @@ class Component extends Node {
}
+ $name = $newNode->name;
+ if (isset($this->children[$name])) {
+ $this->children[$name][] = $newNode;
+ } else {
+ $this->children[$name] = [$newNode];
+ }
+ return $newNode;
+
}
/**
@@ -142,26 +145,32 @@ class Component extends Node {
* pass an instance of a property or component, in which case only that
* exact item will be removed.
*
- * The removed item will be returned. In case there were more than 1 items
- * removed, only the last one will be returned.
- *
- * @param mixed $item
- *
+ * @param string|Property|Component $item
* @return void
*/
function remove($item) {
if (is_string($item)) {
- $children = $this->select($item);
- foreach ($children as $k => $child) {
- unset($this->children[$k]);
+
+ // If there's no dot in the name, it's an exact property name and
+ // we can just wipe out all those properties.
+ //
+ if (strpos($item, '.')===false) {
+ unset($this->children[strtoupper($item)]);
+ return;
+ }
+ // If there was a dot, we need to ask select() to help us out and
+ // then we just call remove recursively.
+ foreach ($this->select($item) as $child) {
+
+ $this->remove($child);
+
}
- return $child;
} else {
- foreach ($this->children as $k => $child) {
+ foreach ($this->select($item->name) as $k => $child) {
if ($child === $item) {
- unset($this->children[$k]);
- return $child;
+ unset($this->children[$item->name][$k]);
+ return;
}
}
@@ -179,7 +188,11 @@ class Component extends Node {
*/
function children() {
- return $this->children;
+ $result = [];
+ foreach ($this->children as $childGroup) {
+ $result = array_merge($result, $childGroup);
+ }
+ return $result;
}
@@ -192,12 +205,14 @@ class Component extends Node {
function getComponents() {
$result = [];
- foreach ($this->children as $child) {
- if ($child instanceof self) {
- $result[] = $child;
+
+ foreach ($this->children as $childGroup) {
+ foreach ($childGroup as $child) {
+ if ($child instanceof self) {
+ $result[] = $child;
+ }
}
}
-
return $result;
}
@@ -211,12 +226,8 @@ class Component extends Node {
* search for a property in a specific group, you can select on the entire
* string ("HOME.EMAIL"). If you want to search on a specific property that
* has not been assigned a group, specify ".EMAIL".
- *
- * Keys are retained from the 'children' array, which may be confusing in
- * certain cases.
- *
+
* @param string $name
- *
* @return array
*/
function select($name) {
@@ -226,27 +237,43 @@ class Component extends Node {
if (strpos($name, '.') !== false) {
list($group, $name) = explode('.', $name, 2);
}
+ if ($name==='') $name = null;
- $result = [];
- foreach ($this->children as $key => $child) {
+ if (!is_null($name)) {
+
+ $result = isset($this->children[$name]) ? $this->children[$name] : [];
- if (
- (
- strtoupper($child->name) === $name
- && (is_null($group) || ($child instanceof Property && strtoupper($child->group) === $group))
- )
- ||
- (
- $name === '' && $child instanceof Property && strtoupper($child->group) === $group
- )
- ) {
+ if (is_null($group)) {
+ return $result;
+ } else {
+ // If we have a group filter as well, we need to narrow it down
+ // more.
+ return array_filter(
+ $result,
+ function($child) use ($group) {
- $result[$key] = $child;
+ return $child instanceof Property && strtoupper($child->group) === $group;
+ }
+ );
}
+
}
- reset($result);
+ // If we got to this point, it means there was no 'name' specified for
+ // searching, implying that this is a group-only search.
+ $result = [];
+ foreach ($this->children as $childGroup) {
+
+ foreach ($childGroup as $child) {
+
+ if ($child instanceof Property && strtoupper($child->group) === $group) {
+ $result[] = $child;
+ }
+
+ }
+
+ }
return $result;
}
@@ -305,9 +332,10 @@ class Component extends Node {
};
- $tmp = $this->children;
+ $children = $this->children();
+ $tmp = $children;
uksort(
- $this->children,
+ $children,
function($a, $b) use ($sortScore, $tmp) {
$sA = $sortScore($a, $tmp);
@@ -318,7 +346,7 @@ class Component extends Node {
}
);
- foreach ($this->children as $child) $str .= $child->serialize();
+ foreach ($children as $child) $str .= $child->serialize();
$str .= "END:" . $this->name . "\r\n";
return $str;
@@ -336,11 +364,13 @@ class Component extends Node {
$components = [];
$properties = [];
- foreach ($this->children as $child) {
- if ($child instanceof self) {
- $components[] = $child->jsonSerialize();
- } else {
- $properties[] = $child->jsonSerialize();
+ foreach ($this->children as $childGroup) {
+ foreach ($childGroup as $child) {
+ if ($child instanceof self) {
+ $components[] = $child->jsonSerialize();
+ } else {
+ $properties[] = $child->jsonSerialize();
+ }
}
}
@@ -365,11 +395,13 @@ class Component extends Node {
$components = [];
$properties = [];
- foreach ($this->children as $child) {
- if ($child instanceof self) {
- $components[] = $child;
- } else {
- $properties[] = $child;
+ foreach ($this->children as $childGroup) {
+ foreach ($childGroup as $child) {
+ if ($child instanceof self) {
+ $components[] = $child;
+ } else {
+ $properties[] = $child;
+ }
}
}
@@ -479,24 +511,12 @@ class Component extends Node {
*/
function __set($name, $value) {
- $matches = $this->select($name);
- $overWrite = count($matches) ? key($matches) : null;
-
+ $name = strtoupper($name);
+ $this->remove($name);
if ($value instanceof self || $value instanceof Property) {
- $value->parent = $this;
- if (!is_null($overWrite)) {
- $this->children[$overWrite] = $value;
- } else {
- $this->children[] = $value;
- }
+ $this->add($value);
} else {
- $property = $this->root->create($name, $value);
- $property->parent = $this;
- if (!is_null($overWrite)) {
- $this->children[$overWrite] = $property;
- } else {
- $this->children[] = $property;
- }
+ $this->add($name, $value);
}
}
@@ -510,13 +530,7 @@ class Component extends Node {
*/
function __unset($name) {
- $matches = $this->select($name);
- foreach ($matches as $k => $child) {
-
- unset($this->children[$k]);
- $child->parent = null;
-
- }
+ $this->remove($name);
}
@@ -530,9 +544,10 @@ class Component extends Node {
*/
function __clone() {
- foreach ($this->children as $key => $child) {
- $this->children[$key] = clone $child;
- $this->children[$key]->parent = $this;
+ foreach ($this->children as $childName => $childGroup) {
+ foreach ($childGroup as $key => $child) {
+ $this->children[$childName][$key] = clone $child;
+ }
}
}
@@ -597,7 +612,7 @@ class Component extends Node {
$messages = [];
- foreach ($this->children as $child) {
+ foreach ($this->children() as $child) {
$name = strtoupper($child->name);
if (!isset($propertyCounters[$name])) {
$propertyCounters[$name] = 1;
diff --git a/lib/Component/VCalendar.php b/lib/Component/VCalendar.php
index efdd316..0e6e171 100644
--- a/lib/Component/VCalendar.php
+++ b/lib/Component/VCalendar.php
@@ -174,25 +174,46 @@ class VCalendar extends VObject\Document {
*/
function getBaseComponents($componentName = null) {
- $components = [];
- foreach ($this->children as $component) {
+ $isBaseComponent = function($component) {
- if (!$component instanceof VObject\Component)
- continue;
+ if (!$component instanceof VObject\Component) {
+ return false;
+ }
+ if ($component->name === 'VTIMEZONE') {
+ return false;
+ }
+ if (isset($component->{'RECURRENCE-ID'})) {
+ return false;
+ }
+ return true;
- if (isset($component->{'RECURRENCE-ID'}))
- continue;
+ };
- if ($componentName && $component->name !== strtoupper($componentName))
- continue;
+ if ($componentName) {
+ // Early exit
+ return array_filter(
+ $this->select($componentName),
+ $isBaseComponent
+ );
+ }
- if ($component->name === 'VTIMEZONE')
- continue;
+ $components = [];
+ foreach($this->children as $childGroup) {
- $components[] = $component;
+ foreach($childGroup as $child) {
- }
+ if (!$child instanceof Component) {
+ // If one child is not a component, they all are so we skip
+ // the entire group.
+ continue 2;
+ }
+ if ($isBaseComponent($child)) {
+ $components[] = $child;
+ }
+
+ }
+ }
return $components;
}
@@ -209,23 +230,40 @@ class VCalendar extends VObject\Document {
*/
function getBaseComponent($componentName = null) {
- foreach ($this->children as $component) {
+ $isBaseComponent = function($component) {
- if (!$component instanceof VObject\Component)
- continue;
-
- if (isset($component->{'RECURRENCE-ID'}))
- continue;
+ if (!$component instanceof VObject\Component) {
+ return false;
+ }
+ if ($component->name === 'VTIMEZONE') {
+ return false;
+ }
+ if (isset($component->{'RECURRENCE-ID'})) {
+ return false;
+ }
+ return true;
- if ($componentName && $component->name !== strtoupper($componentName))
- continue;
+ };
- if ($component->name === 'VTIMEZONE')
- continue;
+ if ($componentName) {
+ foreach ($this->select($componentName) as $child) {
+ if ($isBaseComponent($child)) {
+ return $child;
+ }
+ return null;
+ }
+ }
- return $component;
+ // Searching all components
+ foreach ($this->children as $childGroup) {
+ foreach($childGroup as $child) {
+ if ($isBaseComponent($child)) {
+ return $child;
+ }
+ }
}
+ return null;
}
@@ -322,14 +360,16 @@ class VCalendar extends VObject\Document {
// Setting all properties to UTC time.
foreach ($newEvents as $newEvent) {
- foreach ($newEvent->children as $child) {
- if ($child instanceof VObject\Property\ICalendar\DateTime && $child->hasTime()) {
- $dt = $child->getDateTimes($timeZone);
- // We only need to update the first timezone, because
- // setDateTimes will match all other timezones to the
- // first.
- $dt[0] = $dt[0]->setTimeZone(new DateTimeZone('UTC'));
- $child->setDateTimes($dt);
+ foreach ($newEvent->children as $childGroup) {
+ foreach($childGroup as $child) {
+ if ($child instanceof VObject\Property\ICalendar\DateTime && $child->hasTime()) {
+ $dt = $child->getDateTimes($timeZone);
+ // We only need to update the first timezone, because
+ // setDateTimes will match all other timezones to the
+ // first.
+ $dt[0] = $dt[0]->setTimeZone(new DateTimeZone('UTC'));
+ $child->setDateTimes($dt);
+ }
}
}
$this->add($newEvent);
@@ -426,7 +466,7 @@ class VCalendar extends VObject\Document {
$componentsFound = 0;
$componentTypes = [];
- foreach ($this->children as $child) {
+ foreach ($this->children() as $child) {
if ($child instanceof Component) {
$componentsFound++;
@@ -508,11 +548,8 @@ class VCalendar extends VObject\Document {
*/
function getByUID($uid) {
- return array_filter($this->children, function($item) use ($uid) {
+ return array_filter($this->getComponents(), function($item) use ($uid) {
- if (!$item instanceof Component) {
- return false;
- }
if (!$itemUid = $item->select('UID')) {
return false;
}
diff --git a/lib/Component/VCard.php b/lib/Component/VCard.php
index 4f6a262..87baaa3 100644
--- a/lib/Component/VCard.php
+++ b/lib/Component/VCard.php
@@ -426,7 +426,7 @@ class VCard extends VObject\Document {
// method to remove that array element.
$properties = [];
- foreach ($this->children as $child) {
+ foreach ($this->children() as $child) {
$properties[] = $child->jsonSerialize();
}
@@ -449,7 +449,7 @@ class VCard extends VObject\Document {
$propertiesByGroup = [];
- foreach ($this->children as $property) {
+ foreach ($this->children() as $property) {
$group = $property->group;
diff --git a/tests/VObject/Component/VAvailabilityTest.php b/tests/VObject/Component/VAvailabilityTest.php
index e94e451..2cc3386 100644
--- a/tests/VObject/Component/VAvailabilityTest.php
+++ b/tests/VObject/Component/VAvailabilityTest.php
@@ -319,6 +319,11 @@ VCAL
protected function assertIsValid(VObject\Document $document) {
+ $validationResult = $document->validate();
+ if ($validationResult) {
+ $messages = array_map(function($item) { return $item['message']; }, $validationResult);
+ $this->fail('Failed to assert that the supplied document is a valid document. Validation messages: ' . implode(', ', $messages) );
+ }
$this->assertEmpty($document->validate());
}
diff --git a/tests/VObject/ITip/BrokerProcessMessageTest.php b/tests/VObject/ITip/BrokerProcessMessageTest.php
index 649df98..691574a 100644
--- a/tests/VObject/ITip/BrokerProcessMessageTest.php
+++ b/tests/VObject/ITip/BrokerProcessMessageTest.php
@@ -19,7 +19,7 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
-%foo%
+VERSION:2.0
BEGIN:VEVENT
SEQUENCE:1
UID:foobar
@@ -46,7 +46,6 @@ ICS;
$old = <<<ICS
BEGIN:VCALENDAR
-%foo%
BEGIN:VEVENT
SEQUENCE:1
UID:foobar
@@ -56,7 +55,6 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
-%foo%
BEGIN:VEVENT
SEQUENCE:2
UID:foobar
@@ -83,7 +81,6 @@ ICS;
$old = <<<ICS
BEGIN:VCALENDAR
-%foo%
BEGIN:VEVENT
SEQUENCE:1
UID:foobar
@@ -93,11 +90,10 @@ ICS;
$expected = <<<ICS
BEGIN:VCALENDAR
-%foo%
BEGIN:VEVENT
-SEQUENCE:2
UID:foobar
STATUS:CANCELLED
+SEQUENCE:2
END:VEVENT
END:VCALENDAR
ICS;
diff --git a/tests/VObject/ITip/BrokerTester.php b/tests/VObject/ITip/BrokerTester.php
index 395e184..2cea0b9 100644
--- a/tests/VObject/ITip/BrokerTester.php
+++ b/tests/VObject/ITip/BrokerTester.php
@@ -80,21 +80,12 @@ abstract class BrokerTester extends \Sabre\VObject\TestCase {
$result = $broker->processMessage($message, $existingObject);
- if (is_string($expected)) {
- $expected = str_replace(
- '%foo%',
- "VERSION:2.0\nPRODID:-//Sabre//Sabre VObject $version//EN\nCALSCALE:GREGORIAN",
- $expected
- );
- $expected = str_replace("\n", "\r\n", $expected);
-
- }
- if ($result instanceof \Sabre\VObject\Component\VCalendar) {
- $result = $result->serialize();
- $result = rtrim($result, "\r\n");
+ if (is_null($expected)) {
+ $this->assertTrue(!$result);
+ return;
}
- $this->assertEquals(
+ $this->assertVObjEquals(
$expected,
$result
);
diff --git a/tests/VObject/TestCase.php b/tests/VObject/TestCase.php
index d81559d..16b2833 100644
--- a/tests/VObject/TestCase.php
+++ b/tests/VObject/TestCase.php
@@ -11,7 +11,10 @@ class TestCase extends \PHPUnit_Framework_TestCase {
* It supports objects being supplied as strings, streams or
* Sabre\VObject\Component instances.
*
- * PRODID is removed from both objects as this is often variable.
+ * PRODID is removed from both objects as this is often changes and would
+ * just get in the way.
+ *
+ * CALSCALE will automatically get removed if it's set to GREGORIAN.
*
* @param resource|string|Component $expected
* @param resource|string|Component $actual
@@ -32,6 +35,9 @@ class TestCase extends \PHPUnit_Framework_TestCase {
$this->fail('Input must be a string, stream or VObject component');
}
unset($input->PRODID);
+ if ($input instanceof Component\VCalendar && (string)$input->CALSCALE === 'GREGORIAN') {
+ unset($input->CALSCALE);
+ }
return $input;
};
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabre-vobject.git
More information about the Pkg-owncloud-commits
mailing list