From f7b6df5441a3e3004caa9fd05b7833b67c4fd7c9 Mon Sep 17 00:00:00 2001 From: alex-wdmg Date: Sun, 27 Sep 2020 23:55:24 +0300 Subject: [PATCH] Added `DbSchemaTrait` for migrations, added `ArrayHelper::diff()` method --- CHANGELOG.md | 4 ++++ src/ArrayHelper.php | 36 ++++++++++++++++++++++++++++++++++++ src/DbSchemaTrait.php | 38 ++++++++++++++++++++++++++++++++++++++ src/FileHelper.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 src/DbSchemaTrait.php create mode 100644 src/FileHelper.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 51d00ad..f933f00 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ Changelog ========= +## 1.3.* + * Added `DbSchemaTrait` for migrations + * Added `ArrayHelper::diff()` method + ## 1.3.6 (2020-09-18) * Fixed CIDR methods * Added IPv6 methods in `IpAddressHelper` diff --git a/src/ArrayHelper.php b/src/ArrayHelper.php index 98a0169..31cd79a 100644 --- a/src/ArrayHelper.php +++ b/src/ArrayHelper.php @@ -168,6 +168,42 @@ public static function unique($array, $columns = null) { } } + public static function diff($array1, $array2) { + $array = []; + + if (!is_array($array1)) { + throw new InvalidArgumentException('The `$array1` argument must be array.'); + return null; + } + + if (!is_array($array2)) { + throw new InvalidArgumentException('The `$array2` argument must be array.'); + return null; + } + + foreach ($array1 as $key => $value) { + if (array_key_exists($key, $array2)) { + if (is_array($value)) { + + $diff = self::diff($value, $array2[$key]); + + if (count($diff)) + $array[$key] = $diff; + + } else { + + if ($value != $array2[$key]) + $array[$key] = $value; + + } + } else { + $array[$key] = $value; + } + } + + return $array; + } + public static function crossMerging($array1, $array2, $count1 = null, $count2 = null) { $i = 0; $j = 0; diff --git a/src/DbSchemaTrait.php b/src/DbSchemaTrait.php new file mode 100644 index 0000000..e02a9f7 --- /dev/null +++ b/src/DbSchemaTrait.php @@ -0,0 +1,38 @@ +getDb()->getSchema()->createColumnSchemaBuilder('mediumtext'); + } + + /** + * Creates a long text column. + * @return ColumnSchemaBuilder the column instance which can be further customized. + */ + public function longText() + { + return $this->getDb()->getSchema()->createColumnSchemaBuilder('longtext'); + } + + /** + * Creates a tiny text column. + * @return ColumnSchemaBuilder the column instance which can be further customized. + */ + public function tinyText() + { + return $this->getDb()->getSchema()->createColumnSchemaBuilder('tinytext'); + } +} \ No newline at end of file diff --git a/src/FileHelper.php b/src/FileHelper.php new file mode 100644 index 0000000..07ca3b4 --- /dev/null +++ b/src/FileHelper.php @@ -0,0 +1,43 @@ + + * @link https://github.com/wdmg/yii2-helpers + * @copyright Copyright (c) 2019 - 2020 W.D.M.Group, Ukraine + * @license https://opensource.org/licenses/MIT Massachusetts Institute of Technology (MIT) License + * + */ + +use Yii; +use yii\helpers\BaseFileHelper; +use yii\base\InvalidArgumentException; + +class FileHelper extends BaseFileHelper +{ + + /** + * Safety a file/directory path. + * + * @param string $path the file/directory path to be normalized + * @param null|string $root the base root path. If set as NULL use path of `@app` alias by default. + * @param string $ds the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`. + * @return string the safety file/directory path + */ + public static function safetyPath($path, $root = null, $ds = DIRECTORY_SEPARATOR) { + + $path = parent::normalizePath($path, $ds); + + if (!$root) + $root = Yii::getAlias('@app'); + + $pattern = str_replace('_SEPARATOR_', $ds, "/\_SEPARATOR_[A-Za-z0-9]{0,4}/is"); + $safe = preg_replace($pattern, "$ds****", $root); + return str_replace($root, $safe, $path); + } +} \ No newline at end of file