Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added index property to Recurrence #110

Merged
merged 4 commits into from
Dec 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Recurr/Recurrence.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class Recurrence
/** @var \DateTime */
protected $end;

public function __construct(\DateTime $start = null, \DateTime $end = null)
/** @var int */
protected $index;

public function __construct(\DateTime $start = null, \DateTime $end = null, $index = 0)
{
if ($start instanceof \DateTime) {
$this->setStart($start);
Expand All @@ -33,6 +36,8 @@ public function __construct(\DateTime $start = null, \DateTime $end = null)
if ($end instanceof \DateTime) {
$this->setEnd($end);
}

$this->index = $index;
}

/**
Expand Down Expand Up @@ -66,4 +71,20 @@ public function setEnd($end)
{
$this->end = $end;
}

/**
* @return int
*/
public function getIndex()
{
return $this->index;
}

/**
* @param int $index
*/
public function setIndex($index)
{
$this->index = $index;
}
}
8 changes: 4 additions & 4 deletions src/Recurr/Transformer/ArrayTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ public function transform(Rule $rule, ConstraintInterface $constraint = null, $c
}
}
} else {
$dates[] = $dtTmp;
$dates[$total] = $dtTmp;
}

if (null !== $count) {
Expand Down Expand Up @@ -588,7 +588,7 @@ public function transform(Rule $rule, ConstraintInterface $constraint = null, $c
}
}
} else {
$dates[] = clone $dtTmp;
$dates[$total] = clone $dtTmp;
}

if (null !== $count) {
Expand Down Expand Up @@ -685,11 +685,11 @@ public function transform(Rule $rule, ConstraintInterface $constraint = null, $c

/** @var Recurrence[] $recurrences */
$recurrences = array();
foreach ($dates as $start) {
foreach ($dates as $key => $start) {
/** @var \DateTime $end */
$end = clone $start;

$recurrences[] = new Recurrence($start, $end->add($durationInterval));
$recurrences[] = new Recurrence($start, $end->add($durationInterval), $key);
}

$recurrences = $this->handleInclusions($rule->getRDates(), $recurrences);
Expand Down
14 changes: 14 additions & 0 deletions tests/Recurr/Test/Transformer/ArrayTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,18 @@ public function testRfc2445Example()
$this->assertEquals(new \DateTime('2003-01-12 08:30:00'), $computed[28]->getStart());
$this->assertEquals(new \DateTime('2003-01-12 09:30:00'), $computed[29]->getStart());
}

public function testIndex()
{
$rule = new Rule(
'FREQ=YEARLY;COUNT=10',
new \DateTime('2000-01-01 09:00:00')
);

$computed = $this->transformer->transform($rule);

$this->assertCount(10, $computed);
$this->assertEquals(1, $computed[0]->getIndex());
$this->assertEquals(10, $computed[9]->getIndex());
}
}