Skip to content

Commit

Permalink
Update copyrights, fix DateTime helper
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-wdmg committed Jul 7, 2023
1 parent 210f89a commit 36d15cd
Show file tree
Hide file tree
Showing 13 changed files with 74 additions and 48 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Changelog
=========

## 1.5.0 (2023-07-07)
* Update copyrights, fix DateTime helper

## 1.4.8 (2021-06-20)
* Fixed syntax (backward compatibility for PHP 5.6)

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2019 - 2020 W.D.M.Group, Ukraine
Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 2 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,7 @@ To install the helpers, run the following command in the console:
`$ composer require "wdmg/yii2-helpers"`

# Status and version [ready to use]
* v.1.5.0 - Update copyrights, fix DateTime helper
* v.1.4.8 - Backward compatibility for PHP 5.6
* v.1.4.7 - Added `StringHelper::formatBytes()` method
* v.1.4.6 - Added `ExchangeRates` helper
* v.1.4.5 - Added methods for working with timezones `DateAndTime::getTimezones(...)`
* v.1.4.4 - Fixed `StringHelper::stripTags()`
* v.1.4.3 - Added `keyFirst()` and `keyLast()` methods to `ArrayHelper`
* v.1.4.2 - Added `genUUID()` and `flattenTree()` methods
* v.1.4.1 - Added string detection methods to `StringHelper`
* v.1.4.0 - Added `KeyboardLayoutHelper` and `DbSchemaTrait` for migrations
* v.1.3.6 - Fixed CIDR methods. Added IPv6 methods in `IpAddressHelper`
* v.1.3.5 - IpAddressHelper fixed
* v.1.3.4 - Added IpAddressHelper
* v.1.4.6 - Added `ExchangeRates` helper
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["yii", "yii2", "extension", "helper", "array-helper", "daterime-helper", "ip-helper", "string-helper", "text-analyzer", "utilities", "wdmg"],
"type": "yii2-extension",
"license": "MIT",
"version": "1.4.8",
"version": "1.5.0",
"homepage": "https://github.com/wdmg/yii2-helpers",
"support": {
"source": "https://github.com/wdmg/yii2-helpers",
Expand Down
26 changes: 24 additions & 2 deletions src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Yii2 Custom array helper
*
* @category Helpers
* @version 1.4.8
* @version 1.5.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-helpers
* @copyright Copyright (c) 2019 - 2021 W.D.M.Group, Ukraine
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
Expand Down Expand Up @@ -250,6 +250,28 @@ public static function diff($array1, $array2) {
return $array;
}


/**
* Add or remove (if already exists) a key=>value pair to an array
*
* @param $array1
* @param $array2
* @return array|null
*/
public static function update($array, $findKey, $findValue) {

foreach($array as $key => $value) {

if ($key == $findKey && $value == $findValue) {
unset($array[$key]);
return $array;
}
}

$array[$findKey] = $findValue;
return $array;
}

/**
* Merges multidimensional arrays.
*
Expand Down
45 changes: 27 additions & 18 deletions src/DateAndTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Yii2 Date and Time helper
*
* @category Helpers
* @version 1.4.8
* @version 1.5.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-helpers
* @copyright Copyright (c) 2019 - 2021 W.D.M.Group, Ukraine
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
Expand Down Expand Up @@ -286,33 +286,42 @@ public static function diff($datetime1 = null, $datetime2 = null, $options = [])
//@TODO: https://www.yiiframework.com/doc/api/2.0/yii-i18n-formatter#asRelativeTime()-detail

static::initI18N('app/helpers');
$default = ['layout' => '<span class="{class}">{datetime}</span>', 'inpastClass' => 'field-inpast-datetime', 'futureClass' => 'field-future-datetime'
$default = [
'layout' => '<span class="{class}">{datetime}</span>',
'inpastClass' => 'field-inpast-datetime',
'futureClass' => 'field-future-datetime'
];
$options = array_merge($default, $options);

if (!$datetime1)
return;

$date = new \DateTime();

if ($datetime2)
$datenow = new \DateTime($datetime2);
else
$datenow = new \DateTime("now");
$datenow = $date->createFromFormat('Y-m-d H:i:s', $datetime2);

$dateend = new \DateTime($datetime1);
$interval = $datenow->diff($dateend);
$content = $datetime1;
if (!is_null($datenow)) {
$dateend = new \DateTime($datetime1);
$interval = $datenow->diff($dateend);

$content = Yii::t('app/helpers', '{y, plural, =0{} =1{# year} one{# year} few{# years} many{# years} other{# years}}{y, plural, =0{} =1{, } other{, }}{m, plural, =0{} =1{# month} one{# month} few{# months} many{# months} other{# months}}{m, plural, =0{} =1{, } other{, }}{d, plural, =0{} =1{# day} one{# day} few{# days} many{# days} other{# days}}{d, plural, =0{} =1{, } other{, }}{h, plural, =0{} =1{# hour} one{# hour} few{# hours} many{# hours} other{# hours}}{h, plural, =0{} =1{, } other{, }}{i, plural, =0{} =1{# minute} one{# minute} few{# minutes} many{# minutes} other{# minutes}}{i, plural, =0{} =1{, } other{, }}{s, plural, =0{} =1{# second} one{# second} few{# seconds} many{# seconds} other{# seconds}}{invert, plural, =0{ left} =1{ ago} other{}}',
$interval
);
$content = Yii::t('app/helpers', '{y, plural, =0{} =1{# year} one{# year} few{# years} many{# years} other{# years}}{y, plural, =0{} =1{, } other{, }}{m, plural, =0{} =1{# month} one{# month} few{# months} many{# months} other{# months}}{m, plural, =0{} =1{, } other{, }}{d, plural, =0{} =1{# day} one{# day} few{# days} many{# days} other{# days}}{d, plural, =0{} =1{, } other{, }}{h, plural, =0{} =1{# hour} one{# hour} few{# hours} many{# hours} other{# hours}}{h, plural, =0{} =1{, } other{, }}{i, plural, =0{} =1{# minute} one{# minute} few{# minutes} many{# minutes} other{# minutes}}{i, plural, =0{} =1{, } other{, }}{s, plural, =0{} =1{# second} one{# second} few{# seconds} many{# seconds} other{# seconds}}{invert, plural, =0{ left} =1{ ago} other{}}',
$interval
);
}

$layout = $options['layout'];
if ($interval->invert == 1)
$layout = str_replace('{class}', $options['inpastClass'], $layout);
else
$layout = str_replace('{class}', $options['futureClass'], $layout);

return str_replace('{datetime}', $content, $layout);

if (!empty($layout)) {
if ($interval->invert == 1)
$layout = str_replace('{class}', $options['inpastClass'], $layout);
else
$layout = str_replace('{class}', $options['futureClass'], $layout);

return str_replace('{datetime}', $content, $layout);
} else {
return $content;
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/DbSchemaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Yii2 Extend DB schema
*
* @category Helpers
* @version 1.4.8
* @version 1.5.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-helpers
* @copyright Copyright (c) 2019 - 2021 W.D.M.Group, Ukraine
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
Expand Down
4 changes: 2 additions & 2 deletions src/ExchangeRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Yii2 Exchange Rates
*
* @category Helpers
* @version 1.4.8
* @version 1.5.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-helpers
* @copyright Copyright (c) 2019 - 2021 W.D.M.Group, Ukraine
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
Expand Down
4 changes: 2 additions & 2 deletions src/FileHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Yii2 Custom file helper
*
* @category Helpers
* @version 1.4.8
* @version 1.5.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-helpers
* @copyright Copyright (c) 2019 - 2021 W.D.M.Group, Ukraine
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
Expand Down
6 changes: 3 additions & 3 deletions src/IpAddressHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* Yii2 IP address helper
*
* @category Helpers
* @version 1.4.8
* @version 1.5.0
* @author Alexsander Vyshnyvetskyy <[email protected]>, Jonavon Wilcox <[email protected]>, Manuel Kasper <[email protected]>
* @see https://gist.github.com/stibiumz/5e6a92a195c50c875649, https://gist.github.com/jonavon/2028872, http://m0n0.ch/wall, https://www.experts-exchange.com/questions/23903322/Need-PHP-code-for-calculating-subnets.html
* @link https://github.com/wdmg/yii2-helpers
* @copyright Copyright (c) 2019-2020 W.D.M.Group, Ukraine
* @copyright Copyright (c) 2019-2023 W.D.M.Group, Ukraine
* @copyright Copyright (c) 2003-2004 Manuel Kasper <[email protected]>
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
Expand Down Expand Up @@ -2536,7 +2536,7 @@ public static function getReservedSubnets() {
public static function whois($host, $flags = null, $asHtml = true) {

$zone = array_slice(explode(".", trim($host)), -1)[0];
if (isset(self::WHOIS_SERVERS[$zone])) {
if (!is_null(self::WHOIS_SERVERS[$zone])) {
$servers = self::WHOIS_SERVERS[$zone];
foreach ($servers as $server) {
if ($info = self::whoisQuery($server, trim($host))) {
Expand Down
6 changes: 3 additions & 3 deletions src/KeyboardLayoutHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Yii2 keyboard layout helper
*
* @category Helpers
* @version 1.4.8
* @version 1.5.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-helpers
* @copyright Copyright (c) 2019 - 2021 W.D.M.Group, Ukraine
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
Expand Down Expand Up @@ -79,7 +79,7 @@ public static function getKeyboardLayout($locale = null, $format = false) {
$chars = preg_split('!!u', self::$layouts[$locale], null, PREG_SPLIT_NO_EMPTY);
if ($format == 1) {
return $chars;
} elseif ($format == 2 && is_countable($chars)) {
} elseif ($format == 2 && is_array($chars)) {
$keycodes = [];
foreach ($chars as $char) {
$keycodes[] = \ord($char);
Expand Down
4 changes: 2 additions & 2 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Yii2 String Helper
*
* @category Helpers
* @version 1.4.8
* @version 1.5.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-helpers
* @copyright Copyright (c) 2019 - 2021 W.D.M.Group, Ukraine
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
Expand Down
4 changes: 2 additions & 2 deletions src/TextAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* Yii2 Text Analyzer
*
* @category Helpers
* @version 1.4.8
* @version 1.5.0
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @link https://github.com/wdmg/yii2-helpers
* @copyright Copyright (c) 2019 - 2021 W.D.M.Group, Ukraine
* @copyright Copyright (c) 2019 - 2023 W.D.M.Group, Ukraine
* @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License
*
*/
Expand Down

0 comments on commit 36d15cd

Please sign in to comment.