From f185bbf898ddeaadbe092d4709a8e963fd2550fc Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Mon, 30 Nov 2009 22:00:23 +0000 Subject: [PATCH] FEATURE Added MultipleOf and Modulus methods to ViewableData - useful for templating work git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@94063 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/ViewableData.php | 16 ++++++++++++++++ tests/DataObjectSetTest.php | 24 +++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/core/ViewableData.php b/core/ViewableData.php index 472861d21..d02b81426 100755 --- a/core/ViewableData.php +++ b/core/ViewableData.php @@ -620,7 +620,23 @@ class ViewableData extends Object implements IteratorAggregate { public function TotalItems() { return $this->iteratorTotalItems; } + + /** + * Returns the modulus of the numerical position of the item in the data set. + * The count starts from $startIndex, which defaults to 1. + * @param int $Mod The number to perform Mod operation to. + * @param int $startIndex Number to start count from. + * @return int + */ + public function Modulus($mod, $startIndex = 1) { + return ($this->iteratorPos + $startIndex) % $mod; + } + public function MultipleOf($factor, $offset = 1) { + return ($this->Modulus($factor, $offset) == 0); + } + + // UTILITY METHODS ------------------------------------------------------------------------------------------------- /** diff --git a/tests/DataObjectSetTest.php b/tests/DataObjectSetTest.php index 0dd5f8cf3..c94483c2f 100644 --- a/tests/DataObjectSetTest.php +++ b/tests/DataObjectSetTest.php @@ -69,7 +69,29 @@ class DataObjectSetTest extends SapphireTest { $this->assertTrue($three->Odd()); $this->assertFalse($four->Odd()); } - + + public function testMultipleOf() { + $comments = DataObject::get('PageComment', '', "\"ID\" ASC"); + $commArr = $comments->toArray(); + $multiplesOf3 = 0; + + foreach($comments as $comment) { + if($comment->MultipleOf(3)) { + $comment->IsMultipleOf3 = true; + $multiplesOf3++; + } else { + $comment->IsMultipleOf3 = false; + } + } + + $this->assertEquals(2, $multiplesOf3); + + $this->assertFalse($commArr[0]->IsMultipleOf3); + $this->assertFalse($commArr[1]->IsMultipleOf3); + $this->assertTrue($commArr[2]->IsMultipleOf3); + $this->assertTrue($commArr[5]->IsMultipleOf3); + } + /** * Test {@link DataObjectSet->Count()} */