Skip to content

Commit b3f4d52

Browse files
0.3.0
1 parent 05af970 commit b3f4d52

File tree

9 files changed

+311
-23
lines changed

9 files changed

+311
-23
lines changed

.gitattributes

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
.git export-ignore
55
.gitattributes export-ignore
66
.gitignore export-ignore
7-
/vendor export-ignore
8-
/docs export-ignore
7+
.github export-ignore
8+
phpunit.xml export-ignore
9+
CHANGELOG.md export-ignore
910

11+
vendor export-ignore
12+
docs export-ignore
13+
tests export-ignore

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@
22

33
Helpers for Arris µ-Framework
44

5+
6+
---
7+
Other helpers:
8+
9+
https://github.com/AgelxNash/functions
10+

sources/Helpers/Arr.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ public static function map_to_integer($input): array
4949
*/
5050
public static function map($input, callable $callback):array
5151
{
52-
if (!is_array($input) || empty($input)) return [];
53-
if (!is_callable($callback)) $callback = static function() {};
52+
if (!is_array($input) || empty($input)) {
53+
return [];
54+
}
55+
56+
if (!is_callable($callback)) {
57+
$callback = static function() {
58+
};
59+
}
5460

5561
return array_map($callback, $input);
5662
}
@@ -80,6 +86,8 @@ function fill_like_list(array &$target_array, array $indexes, array $source_arra
8086
* Sort array in given order by key
8187
* Returns array
8288
*
89+
* Alias of array_sort_in_given_order()
90+
*
8391
* @param $array - array for sort [ [ id, data...], [ id, data...], ... ]
8492
* @param $order - order (as array of sortkey values) [ id1, id2, id3...]
8593
* @param $sort_key - sorting key (id)
@@ -95,6 +103,40 @@ public static function sort_with_order(array $array, array $order, $sort_key):ar
95103
return $array;
96104
}
97105

106+
public static function filter(array $input, $callback = null, $flag = 0)
107+
{
108+
return array_filter($input, $callback, $flag);
109+
}
110+
111+
/**
112+
*
113+
*
114+
* @param array $input
115+
* @param callable $callback
116+
* @return array|mixed|null
117+
*/
118+
public static function searchCallback(array $input, callable $callback)
119+
{
120+
if (!is_array($input) || empty($input)) {
121+
return [];
122+
}
123+
124+
if (!is_callable($callback)) {
125+
$callback = static function() { };
126+
}
127+
128+
foreach ($input as $item) {
129+
$v = \call_user_func($callback, $item);
130+
if ( $v === true ) {
131+
return $item;
132+
}
133+
}
134+
return null;
135+
136+
}
137+
138+
139+
98140
}
99141

100142
# -eof-

sources/Helpers/Debug.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
4+
namespace Arris\Helpers;
5+
6+
7+
class Debug
8+
{
9+
10+
/**
11+
* Печать массива в том виде, в каком его понимает php
12+
*
13+
* @param $array
14+
* @param int $level
15+
* @return string
16+
*/
17+
public static function array_code($array, $level = 1)
18+
{
19+
$php = $tabs = $breaks = '';
20+
if (is_array($arr)) {
21+
for ($n = 0; $n < $level; $n++) {
22+
$tabs .= "\t";
23+
if ($n > 0) {
24+
$breaks .= "\t";
25+
}
26+
}
27+
$vals = [];
28+
foreach ($arr as $key => $val) {
29+
$vals[] = is_array($val) ?
30+
"'" . $key . "'=>" . self::array_code($val, $level + 1) :
31+
"'" . $key . "'=>'" . $val . "'";
32+
}
33+
$php = "array(\r" . $tabs . implode(",\r" . $tabs, $vals) . "\r" . $breaks . ")";
34+
}
35+
36+
return $php;
37+
}
38+
39+
/**
40+
* Возможность вызвать любой метод (даже приватный)
41+
*
42+
* @param $object
43+
* @param $method
44+
* @param array $args
45+
* @return mixed
46+
* @throws \ReflectionException
47+
*/
48+
public static function callMethod($object, $method, $args = [])
49+
{
50+
$classReflection = new \ReflectionClass(get_class($object));
51+
$methodReflection = $classReflection->getMethod($method);
52+
$methodReflection->setAccessible(true);
53+
$result = $methodReflection->invokeArgs($object, $args);
54+
$methodReflection->setAccessible(false);
55+
56+
return $result;
57+
}
58+
59+
}

sources/Helpers/FS.php

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,78 @@ class FS
1414
*/
1515
public static function rmdir($directory): bool
1616
{
17-
if (!is_dir($directory)) {
17+
if (!is_dir( $directory )) {
1818
return false;
1919
}
2020

21-
$files = array_diff(scandir($directory), ['.', '..']);
21+
$files = array_diff( scandir( $directory ), [ '.', '..' ] );
2222

2323
foreach ($files as $file) {
2424
$target = "{$directory}/{$file}";
25-
(is_dir($target))
26-
? self::rmdir($target)
27-
: unlink($target);
25+
(is_dir( $target ))
26+
? self::rmdir( $target )
27+
: unlink( $target );
2828
}
29-
return rmdir($directory);
29+
return rmdir( $directory );
3030
}
31-
31+
32+
/**
33+
* Максимальный размер закачиваемого файла
34+
*
35+
* @return mixed
36+
*/
37+
public static function getMaxUploadSize()
38+
{
39+
$max_upload = (int)(ini_get( 'upload_max_filesize' ));
40+
$max_post = (int)(ini_get( 'post_max_size' ));
41+
$memory_limit = (int)(ini_get( 'memory_limit' ));
42+
return min( $max_upload, $max_post, $memory_limit );
43+
}
44+
45+
/**
46+
* Finds path, relative to the given root folder, of all files and directories in the given directory and its sub-directories non recursively.
47+
* Will return an array of the form
48+
* array(
49+
* 'files' => [],
50+
* 'dirs' => [],
51+
* )
52+
*
53+
* @param string $root
54+
* @return array[]
55+
*/
56+
public static function read_all_files($root = '.')
57+
{
58+
$directory_content = [ 'files' => [], 'dirs' => [] ];
59+
$directories = array();
60+
$last_letter = $root[ strlen( $root ) - 1 ];
61+
$root
62+
= ($last_letter === '\\' || $last_letter === '/')
63+
? $root
64+
: $root.DIRECTORY_SEPARATOR;
65+
66+
$directories[] = $root;
67+
68+
while (count( $directories )) {
69+
$dir = array_pop( $directories );
70+
if ($handle = opendir( $dir )) {
71+
while (false !== ($file = readdir( $handle ))) {
72+
if ($file === '.' || $file === '..') {
73+
continue;
74+
}
75+
$file = $dir.$file;
76+
if (is_dir( $file )) {
77+
$directory_path = $file . DIRECTORY_SEPARATOR;
78+
$directories[] = $directory_path;
79+
$directory_content[ 'dirs' ][] = $directory_path;
80+
} elseif (is_file( $file )) {
81+
$directory_content[ 'files' ][] = $file;
82+
}
83+
}
84+
closedir( $handle );
85+
}
86+
}
87+
88+
return $directory_content;
89+
}
90+
3291
}

sources/Helpers/HTTP.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,7 @@
44

55
class HTTP
66
{
7-
/**
8-
* Проверяет, является ли переданная строка корректным URL (http/https/ftp), включая IDN
9-
*
10-
* @param $url
11-
* @return false|int
12-
*/
13-
public static function filter_validate_url($url)
14-
{
15-
return preg_match('#((https?|ftp)://(\S*?\.\S*?))([\s)\[\]{},;"\':<]|\.\s|$)#i', $url);
16-
}
17-
18-
public function getIP()
7+
public static function getIP()
198
{
209
if (php_sapi_name() === 'cli') return '127.0.0.1';
2110

@@ -34,4 +23,24 @@ public function getIP()
3423
return filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP) ? $_SERVER['REMOTE_ADDR'] : NULL;
3524
}
3625

26+
/**
27+
*
28+
* @param $file_post
29+
* @return array
30+
*/
31+
public static function reArrangeFilesPOST($file_post)
32+
{
33+
$rearranged_files = [];
34+
$file_count = count($file_post['name']);
35+
$file_keys = array_keys($file_post);
36+
37+
for ($i = 0; $i < $file_count; $i++) {
38+
foreach ($file_keys as $key) {
39+
$rearranged_files[$i][$key] = $file_post[$key][$i];
40+
}
41+
}
42+
43+
return $rearranged_files;
44+
}
45+
3746
}

sources/Helpers/Misc.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
4+
namespace Arris\Helpers;
5+
6+
/**
7+
* Class Misc
8+
*
9+
* Методы, не вошедшие (пока) в другие классы
10+
*
11+
* @package Arris\Helpers
12+
*/
13+
class Misc
14+
{
15+
/**
16+
* Generate GUID
17+
*
18+
* @return string
19+
*/
20+
public static function GUID()
21+
{
22+
if (function_exists('\com_create_guid') === true) {
23+
return trim(com_create_guid(), '{}');
24+
}
25+
26+
if (function_exists('openssl_random_pseudo_bytes') === true) {
27+
$data = openssl_random_pseudo_bytes(16);
28+
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
29+
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
30+
return strtoupper( vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)) );
31+
}
32+
33+
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
34+
}
35+
36+
37+
38+
}

sources/Helpers/Str.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Str
66
{
77
/**
88
* trims text to a space then adds ellipses if desired
9+
*
910
* @param string $input text to trim
1011
* @param int $length in characters to trim to
1112
* @param bool $ellipses if ellipses (...) are to be added
@@ -119,5 +120,35 @@ function($m) use ($map) { return $m[1].'%'.($map[$m[2]] + 1).'$'; },
119120
return vsprintf($new_str, $args);
120121
}
121122

123+
/**
124+
* pluralForm - форма числительного
125+
*
126+
* @param $number
127+
* @param mixed $forms (array or string with glues, x|y|z or [x,y,z]
128+
* @param string $glue
129+
* @return string
130+
*/
131+
public static function pluralForm($number, $forms, string $glue = '|'):string
132+
{
133+
if (is_string($forms)) {
134+
$forms = explode($forms, $glue);
135+
} elseif (!is_array($forms)) {
136+
return '';
137+
}
138+
139+
if (3 != count($forms)) {
140+
return '';
141+
}
142+
143+
return
144+
($number % 10 == 1 && $number % 100 != 11)
145+
? $forms[0]
146+
: (
147+
($number % 10 >= 2 && $number % 10 <= 4 && ($number % 100 < 10 || $number % 100 >= 20))
148+
? $forms[1]
149+
: $forms[2]
150+
);
151+
}
152+
122153

123154
}

0 commit comments

Comments
 (0)