Skip to content

Commit

Permalink
Added DbSchemaTrait for migrations, added ArrayHelper::diff() method
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-wdmg committed Sep 27, 2020
1 parent 06a64ee commit f7b6df5
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
36 changes: 36 additions & 0 deletions src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions src/DbSchemaTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace wdmg\helpers;

trait DbSchemaTrait
{
/**
* @return \yii\db\Connection the database connection to be used for schema building.
*/
protected abstract function getDb();

/**
* Creates a medium text column.
* @return ColumnSchemaBuilder the column instance which can be further customized.
*/
public function mediumText()
{
return $this->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');
}
}
43 changes: 43 additions & 0 deletions src/FileHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace wdmg\helpers;

/**
* Yii2 Custom file helper
*
* @category Helpers
* @version 1.3.6
* @author Alexsander Vyshnyvetskyy <[email protected]>
* @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);
}
}

0 comments on commit f7b6df5

Please sign in to comment.