Skip to content

Commit

Permalink
Add SK namedays
Browse files Browse the repository at this point in the history
  • Loading branch information
dfridrich committed Jan 10, 2017
1 parent 149d324 commit d815e42
Show file tree
Hide file tree
Showing 5 changed files with 512 additions and 56 deletions.
57 changes: 2 additions & 55 deletions src/NameDays.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
* @package Defr
* @author Dennis Fridrich <fridrich.dennis@gmail.com>
*/
class NameDays
class NameDays extends NameDaysAbstract
{
/**
* @var array
*/
private static $data = [
protected static $data = [
1 => [
1 => '!Nový rok',
'Karina',
Expand Down Expand Up @@ -404,57 +404,4 @@ class NameDays
'Silvestr',
],
];

/**
* @param \DateTime $date
*
* @return string
*/
public static function getNameDay(\DateTime $date = null)
{
if (!$date) {
$date = new \DateTime();
}
$name = self::$data[$date->format('n')][$date->format('j')];
if ($name[0] != '!') {
$return = $name;
} else {
$return = substr($name, 1);
}

return $return;
}

/**
* @param $name
* @return \DateTime|null
*/
public static function getNameDate($name)
{
$matches = [];
for ($i = 1; $i <= 12; $i++) {
foreach (self::$data[$i] as $day => $value) {
$check = mb_stripos($value, $name);
if (false !== $check) {
$matches[] = [$i, $day, $value, levenshtein($name, $value)];
}
}
}

usort(
$matches,
function ($a, $b) {
return $a[3] > $b[3];
}
);

if (count($matches) == 0) {
return null;
}

$date = new \DateTime();
$date->setDate(date("Y"), $matches[0][0], $matches[0][1]);

return $date;
}
}
70 changes: 70 additions & 0 deletions src/NameDaysAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Defr;

/**
* Class NameDays
* @package Defr
* @author Dennis Fridrich <fridrich.dennis@gmail.com>
*/
abstract class NameDaysAbstract
{
/**
* @var array
*/
private static $data = [];

/**
* @param \DateTime $date
*
* @return string
*/
public static function getNameDay(\DateTime $date = null)
{
if (!$date) {
$date = new \DateTime();
}
$name = static::$data[$date->format('n')][$date->format('j')];
if ($name[0] != '!') {
$return = $name;
} else {
$return = substr($name, 1);
}

return $return;
}

/**
* @param $name
*
* @return \DateTime|null
*/
public static function getNameDate($name)
{
$matches = [];
for ($i = 1; $i <= 12; $i++) {
foreach (static::$data[$i] as $day => $value) {
$check = mb_stripos($value, $name);
if (false !== $check) {
$matches[] = [$i, $day, $value, levenshtein($name, $value)];
}
}
}

usort(
$matches,
function ($a, $b) {
return $a[3] > $b[3];
}
);

if (count($matches) == 0) {
return null;
}

$date = new \DateTime();
$date->setDate(date("Y"), $matches[0][0], $matches[0][1]);

return $date;
}
}
12 changes: 12 additions & 0 deletions src/NameDaysCz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Defr;

/**
* Class NameDaysCz
* @package Defr
* @author Dennis Fridrich <fridrich.dennis@gmail.com>
*/
class NameDaysCz extends NameDays
{
}
Loading

0 comments on commit d815e42

Please sign in to comment.