Created
February 5, 2017 19:25
-
-
Save ekinhbayar/0c38f7929fc19ed63dea3942e5ac22ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
/** | |
* Set of regular expressions utilized to match/replace/validate parts of a given input. | |
* | |
* PHP version 7+ | |
* | |
* @category DateTimeParser | |
* @author Ekin H. Bayar <ekin@coproductivity.com> | |
* @version 0.2.0 | |
*/ | |
namespace DateTimeParser; | |
class Pattern | |
{ | |
/** | |
* Don't forget to close parentheses when using Pattern::DEFINE | |
*/ | |
const DEFINE = "/(?(DEFINE)"; | |
# Definitions of sub patterns for a valid interval | |
const INTEGER = <<<'REGEX' | |
(?<integer> | |
(?:\G|(?!\n)) | |
(\s*\b)? | |
\d{1,5} | |
\s* | |
) | |
REGEX; | |
# Starts with integer followed by time specified | |
const TIME_PART = <<<'REGEX' | |
(?<timepart> | |
(?&integer) | |
( s(ec(ond)?s?)? | |
| m(on(ths?)?|in(ute)?s?)? | |
| h(rs?|ours?)? | |
| d(ays?)? | |
| w(eeks?)? | |
) | |
) | |
REGEX; | |
# Regex to match a valid interval, holds the value in $matches['interval'] | |
const INTERVAL_ONLY = "^(?<interval>(?&timepart)++)$/uix"; | |
# Leading separator | |
const LEADING_SEPARATOR = "(?<leadingSeparator>\s?(?:in)\s?)"; | |
# Regex to match a valid interval and any trailing string, holds the interval in $matches['interval'], the rest in $matches['trailing'] | |
const INTERVAL_WITH_TRAILING_DATA = "^(?<interval>(?&timepart)++)(?<trailing>.+)*?$/uix"; | |
# Regex to handle an input that may have multiple intervals along with leading and/or trailing data | |
const MULTIPLE_INTERVALS = <<<'REGEX' | |
^(?<leading>.*?)? | |
(?<sep>(?&leadingSeparator))? | |
(?<interval>(?&timepart)++) | |
(?<trailing>.*) | |
/uix | |
REGEX; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
namespace DateTimeParser; | |
class Separator | |
{ | |
# Separation types | |
const COMMA = 0b00000000; | |
const SYMBOL = 0b00000001; | |
const STRING = 0b00000010; | |
# Default to comma separation | |
const DEFAULT_STRING = ','; | |
const DEFAULT_EXPRESSION = <<<'REGEX' | |
/ | |
(?<first>[^,]*) | |
\s?,\s? | |
(?<next>.*)$ | |
/ui"; | |
REGEX; | |
# Default leading separator <in|at|on> with capturing groups | |
const LEADING_GROUP_SEPARATOR = <<<'REGEX' | |
/ | |
(?<leading>.*) | |
\s+(?<separator>(?:o|i)n|at)\s? | |
(?<trailing>.*)? | |
/uix | |
REGEX; | |
const STRING_SEPARATOR = <<<'REGEX' | |
/ | |
^(?<first>.*?)\s?,\s?(?<next>.*)$ | |
/uix | |
REGEX; | |
/** | |
* @var string | |
*/ | |
private $string; | |
/** | |
* @var int | |
*/ | |
private $type; | |
/** | |
* @var bool | |
*/ | |
private $keepSeparator; | |
/** | |
* @var string | |
*/ | |
private $expression; | |
/** | |
* Separator constructor. | |
* | |
* @param string $string String to separate input, the separator | |
* @param int $type Type of separation | |
* @param bool $keepSeparator Keep or omit separator string | |
* @param string $expression Optional regex | |
*/ | |
public function __construct( | |
string $string = self::DEFAULT_STRING, | |
int $type = self::COMMA, | |
bool $keepSeparator = false, | |
string $expression = self::DEFAULT_EXPRESSION | |
) { | |
$this->string = $string; | |
$this->type = $type; | |
$this->keepSeparator = $keepSeparator; | |
$this->expression = $expression; | |
} | |
public function getString(): string | |
{ | |
return $this->string; | |
} | |
public function getType(): int | |
{ | |
return $this->type; | |
} | |
public function keepSeparator(): bool | |
{ | |
return $this->keepSeparator; | |
} | |
public function getExpression(): string | |
{ | |
return $this->expression; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types = 1); | |
namespace DateTimeParser; | |
class Separators | |
{ | |
private $separators; | |
public function __construct(array $separators = []) | |
{ | |
$this->separators = $separators; | |
} | |
public function getSeparators(): array | |
{ | |
return $this->separators ?? []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment