Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
popy-dev committed Mar 6, 2018
1 parent 6662e50 commit 989048b
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 89 deletions.
4 changes: 2 additions & 2 deletions src/Converter/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getTo()
*
* @param DateRepresentationInterface $to the to
*
* @return self|null
* @return self
*/
public function setTo(DateRepresentationInterface $to)
{
Expand All @@ -92,7 +92,7 @@ public function setTo(DateRepresentationInterface $to)
*/
public function getUnixTime()
{
return $this->unixTime;
return (int)$this->unixTime;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Converter/UnixTimeConverter/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ class Date implements UnixTimeConverterInterface
*/
public function fromUnixTime(Conversion $conversion)
{
if (null === $res = $conversion->getTo()) {
return;
}

$from = $conversion->getFrom();

$res = $res
$res = $conversion->getTo()
->withUnixTime($from->getUnixTime())
->withUnixMicroTime($from->getUnixMicroTime())
->withTimezone($from->getTimezone())
Expand Down
9 changes: 4 additions & 5 deletions src/Converter/UnixTimeConverter/DateSolar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Popy\Calendar\Converter\UnixTimeConverter;

use BCMathExtended\BC;
use Popy\Calendar\Converter\Conversion;
use Popy\Calendar\Converter\UnixTimeConverterInterface;
use Popy\Calendar\Converter\CompleteLeapYearCalculatorInterface;
Expand Down Expand Up @@ -68,7 +67,7 @@ public function fromUnixTime(Conversion $conversion)
// TODO : remove code repetition by using a separate abstraction
// handling basic math operations.

if (is_integer($this->dayLengthInSeconds) || !class_exists(BC::class)) {
if (is_integer($this->dayLengthInSeconds)) {
// bc not needed/not available

// Relative time from era start.
Expand All @@ -95,12 +94,12 @@ public function fromUnixTime(Conversion $conversion)

// Calculating global day index. Floor is used to properly handle
// negative values
$eraDayIndex = BC::floor(
$eraDayIndex = floor(
bcdiv($relativeTime, $this->dayLengthInSeconds)
);

// TODO : handle the loss as microseconds ?
$time = BC::ceil(bcsub(
$time = ceil(bcsub(
$relativeTime,
bcmul($eraDayIndex, $this->dayLengthInSeconds)
));
Expand Down Expand Up @@ -131,7 +130,7 @@ public function toUnixTime(Conversion $conversion)
return;
}

$year = $input->getYear();
$year = (int)$input->getYear();
$eraDayIndex = $input->getDayIndex()
+ $this->calculator->getYearEraDayIndex($year)
;
Expand Down
2 changes: 1 addition & 1 deletion src/Converter/UnixTimeConverter/Iso8601Weeks.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function fromUnixTime(Conversion $conversion)
return;
}

$year = $input->getYear();
$year = (int)$input->getYear();
// Assuming the era starting year is 1970, it starts a Thursday.
$dayOfWeek = ($input->getEraDayIndex() + $this->firstYearDayIndex) % 7;

Expand Down
8 changes: 3 additions & 5 deletions src/Converter/UnixTimeConverter/TimeOffset.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ public function fromUnixTime(Conversion $conversion)
$conversion->getUnixTime() + $offset->getValue()
);

if (null !== $conversion->getTo()) {
$conversion->setTo(
$conversion->getTo()->withOffset($offset)
);
}
$conversion->setTo(
$conversion->getTo()->withOffset($offset)
);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Factory/ConfigurableFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class ConfigurableFactory
/**
* Available values for option "additional_symbol_parser"
*
* @var array<string>
* @var array<string|boolean>
*/
protected $additional_symbol_parser = [
'none' => false,
Expand Down Expand Up @@ -135,8 +135,8 @@ public function buildConverter(array $options = array())
/**
* Generic service getter.
*
* @param string $service Service name.
* @param array &$options Option array
* @param string $service Service name.
* @param array $options Option array
*
* @return mixed
*/
Expand Down
4 changes: 1 addition & 3 deletions src/Formatter/AgnosticFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ public function format(DateTimeInterface $input, $format)
{
$date = Date::buildFromDateTimeInterface($input);

if (null === $date = $this->converter->to($date)) {
return;
}
$date = $this->converter->to($date);

return $this->formatDateRepresentation($date, $format);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Formatter/NumberConverter/RFC2550.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function from($input)
*
* @param string $input
*
* @return string
* @return string|null
*/
public function convertFromPositive($input)
{
Expand All @@ -117,7 +117,7 @@ public function convertFromPositive($input)
$matches
)
) {
return;
return null;
}

if ($matches['alpha'] === '') {
Expand Down Expand Up @@ -148,7 +148,7 @@ public function convertFromPositive($input)
*
* @param string $input
*
* @return string
* @return string|null
*/
public function convertFromNegative($input)
{
Expand Down
20 changes: 15 additions & 5 deletions src/Formatter/SymbolFormatter/StandardDateFragmented.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ public function formatSymbol(DateRepresentationInterface $input, FormatToken $to

if ($token->is('F')) {
// F A full textual representation of a month
return (string)$this->locale->getMonthName($input->getDateParts()->get(0));
return (string)$this->locale->getMonthName(
$input->getDateParts()->get(0)
);
}

if ($token->is('M')) {
// M A short textual representation of a month, three letters
return (string)$this->locale->getMonthShortName($input->getDateParts()->get(0));
return (string)$this->locale->getMonthShortName(
$input->getDateParts()->get(0)
);
}

if ($token->is('m')) {
Expand Down Expand Up @@ -79,17 +83,23 @@ public function formatSymbol(DateRepresentationInterface $input, FormatToken $to

if ($token->is('S')) {
// S English ordinal suffix for the day of the month, 2 characters
return (string)$this->locale->getNumberOrdinalSuffix($input->getDateParts()->get(1));
return $this->locale->getNumberOrdinalSuffix(
(int)$input->getDateParts()->get(1)
);
}

if ($token->is('l')) {
// l (lowercase 'L') A full textual representation of the day of the week
return (string)$this->locale->getDayName($input->getDateParts()->getTransversal(2));
return (string)$this->locale->getDayName(
$input->getDateParts()->getTransversal(2)
);
}

if ($token->is('D')) {
// D A textual representation of a day, three letters
return (string)$this->locale->getDayShortName($input->getDateParts()->getTransversal(2));
return (string)$this->locale->getDayShortName(
$input->getDateParts()->getTransversal(2)
);
}

if ($token->is('w')) {
Expand Down
2 changes: 1 addition & 1 deletion src/Formatter/SymbolFormatter/StandardDateSolar.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function formatSymbol(DateRepresentationInterface $input, FormatToken $to
}
if ($token->is('y')) {
// y A two digit representation of a year
return $this->converter->to($input->getYear());
return $this->converter->to($input->getYear() ?: 0);
}

if ($token->is('Y')) {
Expand Down
10 changes: 4 additions & 6 deletions src/Parser/AgnosticParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,18 @@ public function parse($input, $format, DateTimeZone $timezone = null)
*/
public function parseToDateRepresentation($input, $format, DateTimeZone $timezone = null)
{
if (null === $lexer = $this->parser->parseFormat($format)) {
return;
}
$lexer = $this->parser->parseFormat($format);

if (null === $parts = $lexer->tokenizeDate($input)) {
return;
return null;
}

$date = Date::buildFromTimezone($timezone);

if (null === $date = $this->mapper->map($parts, $date)) {
return;
return null;
}

return $date;
return $this->mapper->map($parts, $date);
}
}
37 changes: 21 additions & 16 deletions src/Parser/DateLexer/PregChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,30 @@ public function hydrateResult(DateLexerResult $result, $match, $offset = 1)
return $offset;
}

if (
null === $this->symbol // nothing to map
|| $match[$offset][1] === -1 // didn't match
) {
return $offset + 1;
}

// Did match
if ($match[$offset][1] !== -1) {
$found = array_search($match[$offset][0], $this->choices);

// DO THE MAGIC
if ($found === false) {
$res = preg_grep(
'/^' . preg_quote($match[$offset][0], '/') . '$/i',
$this->choices
);

if (count($res)) {
reset($res);
$found = key($res);
}
$found = array_search($match[$offset][0], $this->choices);

// DO THE MAGIC
if ($found === false) {
$res = preg_grep(
'/^' . preg_quote($match[$offset][0], '/') . '$/i',
$this->choices
);

if (count($res)) {
reset($res);
$found = key($res);
}

$result->set($this->symbol, $found);
}

$result->set($this->symbol, $found);

return $offset + 1;
}
Expand Down
12 changes: 5 additions & 7 deletions src/Parser/FormatParser/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
namespace Popy\Calendar\Parser\FormatParser;

use Popy\Calendar\Parser\FormatLexerInterface;
use Popy\Calendar\Parser\FormatLexer\MbString;
use Popy\Calendar\Parser\SymbolParserInterface;
use Popy\Calendar\Parser\FormatParserInterface;
use Popy\Calendar\Parser\DateLexer\Collection;
use Popy\Calendar\Parser\SymbolParser\PregNative;
use Popy\Calendar\Parser\DateLexer\NonSymbolMatcher;

/**
Expand Down Expand Up @@ -37,13 +35,13 @@ class Basic implements FormatParserInterface
/**
* Class constructor.
*
* @param FormatLexerInterface|null $lexer Format lexer.
* @param SymbolParserInterface|null $symbolParser Symbol Parser
* @param FormatLexerInterface $lexer Format lexer.
* @param SymbolParserInterface $symbolParser Symbol Parser
*/
public function __construct(FormatLexerInterface $lexer = null, SymbolParserInterface $symbolParser = null)
public function __construct(FormatLexerInterface $lexer, SymbolParserInterface $symbolParser)
{
$this->lexer = $lexer ?: new MbString();
$this->symbolParser = $symbolParser ?: new PregNative();
$this->lexer = $lexer;
$this->symbolParser = $symbolParser;
}


Expand Down
12 changes: 5 additions & 7 deletions src/Parser/FormatParser/PregExtendedNative.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use Popy\Calendar\Parser\FormatToken;
use Popy\Calendar\Parser\FormatLexerInterface;
use Popy\Calendar\Parser\FormatLexer\MbString;
use Popy\Calendar\Parser\FormatParserInterface;
use Popy\Calendar\Parser\SymbolParserInterface;
use Popy\Calendar\Parser\DateLexer\PregSimple;
use Popy\Calendar\Parser\DateLexer\PregCollection;
use Popy\Calendar\Parser\SymbolParser\PregNative;

/**
* Preg based implementation of the native DateTimeInterface format, with an
Expand Down Expand Up @@ -39,13 +37,13 @@ class PregExtendedNative implements FormatParserInterface
/**
* Class constructor.
*
* @param FormatLexerInterface|null $lexer Format lexer.
* @param SymbolParserInterface|null $symbolParser Symbol Parser
* @param FormatLexerInterface $lexer Format lexer.
* @param SymbolParserInterface $symbolParser Symbol Parser
*/
public function __construct(FormatLexerInterface $lexer = null, SymbolParserInterface $symbolParser = null)
public function __construct(FormatLexerInterface $lexer, SymbolParserInterface $symbolParser)
{
$this->lexer = $lexer ?: new MbString();
$this->symbolParser = $symbolParser ?: new PregNative();
$this->lexer = $lexer;
$this->symbolParser = $symbolParser;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Parser/FormatToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function __construct($value, $type)
/**
* Gets the token value.
*
* @return string
* @return string|null
*/
public function getValue()
{
Expand All @@ -58,7 +58,7 @@ public function getValue()
/**
* Gets the token name (possibly aliased).
*
* @return string
* @return string|null
*/
public function getName()
{
Expand Down Expand Up @@ -97,7 +97,7 @@ public function is($symbol)
* or one of the symbol contained in the first argument if it is an array
*
* @param array|string $symbols
* @param string ...$symbol
* @param string ...$symbols
*
* @return boolean
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Parser/ResultMapper/StandardDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ protected function determineOffset(DateLexerResult $parts, TimeOffset $offset)
* Determine date's timezone. If an offset has been found, Timezone has
* no effect on the date parsing, but will have on the date display.
*
* @param DateLexerResult $parts Date lexer results.
* @param integer|null $offset Date offset if it has been found.
* @param DateTimeZone $inputTz Default timezone if any.
* @param DateLexerResult $parts Date lexer results.
* @param TimeOffset $offset Date offset.
* @param DateTimeZone|null $inputTz Default timezone if any.
*
* @return DateTimeZone
*/
protected function determineTimezone(DateLexerResult $parts, TimeOffset $offset, DateTimeZone $inputTz)
protected function determineTimezone(DateLexerResult $parts, TimeOffset $offset, DateTimeZone $inputTz = null)
{
// e Timezone identifier (added in PHP 5.1.0) Examples: UTC, GMT, Atlantic/Azores
// O Difference to Greenwich time (GMT) in hours Example: +0200
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/ResultMapper/StandardDateFragmented.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected function determineDayOfWeek(DateLexerResult $parts)
protected function determineWeekIndex(DateLexerResult $parts)
{
if (null === $w = $parts->get('W')) {
return;
return null;
}

return intval($w) - 1;
Expand Down
Loading

0 comments on commit 989048b

Please sign in to comment.