Skip to content

Commit

Permalink
Merge branch 'support-language-segment-anniversary-filter' into token…
Browse files Browse the repository at this point in the history
…-helper-replace
  • Loading branch information
kuzmany committed Feb 11, 2019
2 parents d923a4d + b2536bb commit 3ce452d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
18 changes: 10 additions & 8 deletions app/bundles/CoreBundle/Helper/DateRelativeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ public function __construct(array $dictionary, $timeframe, $prefixes = [])
{
$this->dictionary = $dictionary;
$this->timeframe = trim(str_replace($prefixes, '', $timeframe));
$this->prefix = trim(str_replace([$this->getRelativeString(), $this->getRelativeDate()], '', $timeframe));
$this->prefix = trim(str_replace([$this->getStringPart(), $this->getTimeframePart()], '', $timeframe));
}

/**
* Relative date from dictionary or start with +/-.
*
* @return bool
*/
public function hasRelativeDate()
Expand All @@ -52,31 +54,31 @@ public function hasRelativeDate()
*/
public function hasDateFromDictionary()
{
return in_array($this->getRelativeString(), $this->getDictionaryVariants());
return in_array($this->getStringPart(), $this->getDictionaryVariants());
}

/**
* Return timeframe.
* Return string part of time frame, for exampele: anniversary.
*
* @return string
*/
private function getRelativeString()
private function getStringPart()
{
return trim(str_replace($this->getRelativeDate(), '', $this->timeframe));
return trim(str_replace($this->getTimeframePart(), '', $this->timeframe));
}

/**
* Return all after /birthday string, for example -1 day.
* Return timeframe part, for example +1 day.
*
* @return string
*/
public function getRelativeDate()
public function getTimeframePart()
{
return trim(str_replace($this->getDictionaryVariants(), '', $this->timeframe));
}

/**
* Return all possible variants for dates.
* Return all possible variants with translations.
*
* @return array
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public function testRelativeDate()
$dateRelativeParser = new DateRelativeParser($this->dictionary, 'date +1 day', ['datetime', 'date']);
$this->assertTrue($dateRelativeParser->hasRelativeDate());
$dateRelativeParser = new DateRelativeParser($this->dictionary, 'date +1 day', ['datetime', 'date']);
$this->assertEquals('+1 day', $dateRelativeParser->getRelativeDate());
$this->assertEquals('+1 day', $dateRelativeParser->getTimeframePart());
$dateRelativeParser = new DateRelativeParser($this->dictionary, 'date +1 day', ['datetime', 'date']);
$this->assertEquals('date', $dateRelativeParser->getPrefix());
$dateRelativeParser = new DateRelativeParser($this->dictionary, 'date anniversary +1 day', ['datetime', 'date']);
$this->assertEquals('+1 day', $dateRelativeParser->getRelativeDate());
$this->assertEquals('+1 day', $dateRelativeParser->getTimeframePart());
$dateRelativeParser = new DateRelativeParser($this->dictionary, 'date anniversary +1 day', ['datetime', 'date']);
$this->assertEquals('date', $dateRelativeParser->getPrefix());
$this->assertEquals(date('Y').'-07-07', $dateRelativeParser->getAnniversaryDate('2018-07-07'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getParameterHolder(ContactSegmentFilterCrate $contactSegmentFilt
public function getParameterValue(ContactSegmentFilterCrate $contactSegmentFilterCrate)
{
$filter = $contactSegmentFilterCrate->getFilter();
$relativeFilter = $this->dateDecorator->dateRelativeParser($filter)->getRelativeDate();
$relativeFilter = $this->dateDecorator->dateRelativeParser($filter)->getTimeframePart();
$dateTimeHelper = $this->dateDecorator->getDefaultDate($relativeFilter);

return $dateTimeHelper->toUtcString('%-m-d');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Mautic\LeadBundle\Tests\Segment\Decorator\Date;

use Mautic\CoreBundle\Helper\DateRelativeParser;
use Mautic\LeadBundle\Segment\ContactSegmentFilterCrate;
use Mautic\LeadBundle\Segment\Decorator\Date\DateOptionFactory;
use Mautic\LeadBundle\Segment\Decorator\Date\Day\DateDayToday;
Expand Down Expand Up @@ -265,6 +266,10 @@ private function getFilterDecorator($filterName)
$dateDecorator = $this->createMock(DateDecorator::class);
$relativeDate = $this->createMock(RelativeDate::class);

$dateDecorator->method('dateRelativeParser')->willReturn(
(new DateRelativeParser(['anniversary'=>'anniversary'], ''))
);

$relativeDate->method('getRelativeDateStrings')
->willReturn(
[
Expand Down

0 comments on commit 3ce452d

Please sign in to comment.