Skip to content

Commit

Permalink
FEATURE Added MultipleOf and Modulus methods to ViewableData - useful…
Browse files Browse the repository at this point in the history
… for templating work (from r94063)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@95584 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information
Sean Harvey committed Dec 16, 2009
1 parent 7377010 commit d6e9931
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
16 changes: 16 additions & 0 deletions core/ViewableData.php
Expand Up @@ -620,7 +620,23 @@ public function Pos($startIndex = 1) {
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 -------------------------------------------------------------------------------------------------

/**
Expand Down
24 changes: 23 additions & 1 deletion tests/DataObjectSetTest.php
Expand Up @@ -69,7 +69,29 @@ function testIterator() {
$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()}
*/
Expand Down

0 comments on commit d6e9931

Please sign in to comment.