Skip to content

Commit 601ee3f

Browse files
Merge pull request #2 from larapulse/feature/tests
Add unit tests
2 parents 56096bd + 36ad48e commit 601ee3f

30 files changed

+1943
-136
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
],
2323
"require": {
2424
"php": "~7.0",
25+
"ext-mbstring": "*",
2526
"illuminate/support": "^5.5"
2627
},
2728
"require-dev": {

docs/CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,33 @@ All notable changes to `larapulse/support` will be documented in this file.
44

55
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
66

7+
## 2017-11-22
8+
9+
### Added
10+
- `Larapulse\Support\Handlers\RegEx` to work with Regular Expressions
11+
- Init `Larapulse\Support\Helpers\RegEx` to generate Regular Expressions
12+
- Unit tests for `Larapulse\Support\Handlers\Str` class
13+
- Unit tests for `Larapulse\Support\Handlers\Regex` class
14+
- Unit tests for `Larapulse\Support\Helpers\Regex` class
15+
- Unit tests for array and string functions
16+
17+
### Changed
18+
- `Larapulse\Support\Handlers\Str::pop()` add `encoding` attribute
19+
- `Larapulse\Support\Handlers\Str::shift()` add `encoding` attribute
20+
- `Larapulse\Support\Handlers\Str::cutStart()` add `repeat` and `caseSensitive` attributes
21+
- `Larapulse\Support\Handlers\Str::cutEnd()` add `repeat` and `caseSensitive` attributes
22+
23+
### Removed
24+
- Array functions: `array_only`, `array_head`, `array_last`
25+
26+
## 2017-11-21
27+
28+
### Added
29+
- Unit tests for `Larapulse\Support\Handlers\Arr` class
30+
31+
### Changed
32+
- `array_flatten`, `array_flatten_assoc` function not flatten array, if wrong depth defined
33+
734
## 2017-11-20
835

936
### Added
@@ -12,3 +39,4 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
1239
- `Larapulse\Support\Helpers\DataTypes` class
1340
- Array functions: `array_flatten`, `array_flatten_assoc`, `array_depth`, `is_array_of_type`, `is_array_of_types`, `is_array_of_instance`, `array_is_assoc`, `array_only`, `array_head`, `array_last`
1441
- String functions: `str_pop`, `str_shift`, `str_cut_start`, `str_cut_end`, `str_lower`, `str_upper`, `str_title`, `str_length`, `str_words`, `str_substr`, `str_ucfirst`, `str_starts_with`, `str_ends_with`
42+
- Unit tests for `Larapulse\Support\Handlers\Arr` class

phpunit.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Larapulse/Support Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>

src/Handlers/Arr.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ class Arr
1515
*/
1616
public static function flatten(array $array, $depth = INF) : array
1717
{
18-
$depth = is_int($depth) ? max($depth, 1) : INF;
18+
$depth = is_int($depth) ? max($depth, 0) : INF;
1919

2020
return array_reduce($array, function ($result, $item) use ($depth) {
21-
if (!is_array($item)) {
21+
if (!is_array($item) || $depth === 0) {
2222
return array_merge($result, [$item]);
2323
} elseif ($depth === 1) {
2424
return array_merge($result, array_values($item));
@@ -39,10 +39,10 @@ public static function flatten(array $array, $depth = INF) : array
3939
public static function flattenAssoc(array $array, $depth = INF) : array
4040
{
4141
$result = [];
42-
$depth = is_int($depth) ? max($depth, 1) : INF;
42+
$depth = is_int($depth) ? max($depth, 0) : INF;
4343

4444
foreach ($array as $key => $value) {
45-
if (is_array($value) && $depth > 1) {
45+
if (is_array($value) && $depth >= 1) {
4646
$result = self::flattenAssoc($value, $depth - 1) + $result;
4747
} else {
4848
$result[$key] = $value;

src/Handlers/RegEx.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Larapulse\Support\Handlers;
4+
5+
class RegEx
6+
{
7+
const REGEX_CHARS_REPLACEMENT = [
8+
" " => '\s',
9+
"\t" => '\t',
10+
"\n" => '\n',
11+
"\f" => '\f',
12+
"\r" => '\r',
13+
"\v" => '\v',
14+
// "\h" => '\h',
15+
// "\R" => '\R',
16+
];
17+
18+
/**
19+
* Validate if RegEx statement is valid
20+
*
21+
* @param string $regexStatement
22+
*
23+
* @return bool
24+
*/
25+
public static function isValid(string $regexStatement) : bool
26+
{
27+
return @preg_match($regexStatement, null) !== false;
28+
}
29+
30+
/**
31+
* Prepare string to be safety used in regex
32+
*
33+
* @param string $string
34+
* @param string $quotes
35+
*
36+
* @return string
37+
*/
38+
public static function prepare(string $string, $quotes = '/') : string
39+
{
40+
$string = $quotes ? preg_quote($string, $quotes) : $string;
41+
42+
return str_replace(
43+
array_keys(self::REGEX_CHARS_REPLACEMENT),
44+
array_values(self::REGEX_CHARS_REPLACEMENT),
45+
$string
46+
);
47+
}
48+
}

src/Handlers/Str.php

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@
22

33
namespace Larapulse\Support\Handlers;
44

5+
use Larapulse\Support\Helpers\Regex as RegexHelper;
6+
57
class Str
68
{
79
/**
810
* Pop the character off the end of string
911
*
1012
* @param string $str
13+
* @param string $encoding
1114
*
1215
* @return bool|string
1316
*/
14-
public static function pop(string &$str)
17+
public static function pop(string &$str, string $encoding = null)
1518
{
16-
$last = substr($str, -1);
17-
$str = substr($str, 0, -1);
19+
$encoding = $encoding ?: mb_internal_encoding();
20+
21+
$last = mb_substr($str, -1, null, $encoding);
22+
$str = mb_substr($str, 0, -1, $encoding);
1823

1924
return $last;
2025
}
@@ -23,13 +28,16 @@ public static function pop(string &$str)
2328
* Shift a character off the beginning of string
2429
*
2530
* @param string $str
31+
* @param string $encoding
2632
*
2733
* @return bool|string
2834
*/
29-
public static function shift(string &$str)
35+
public static function shift(string &$str, string $encoding = null)
3036
{
31-
$first = substr($str, 0, 1);
32-
$str = substr($str, 1);
37+
$encoding = $encoding ?: mb_internal_encoding();
38+
39+
$first = mb_substr($str, 0, 1, $encoding);
40+
$str = mb_substr($str, 1, null, $encoding);
3341

3442
return $first;
3543
}
@@ -39,24 +47,56 @@ public static function shift(string &$str)
3947
*
4048
* @param string $str
4149
* @param string $subString
50+
* @param bool $repeat
51+
* @param bool $caseSensitive Not working with multi-byte characters
4252
*
4353
* @return string
4454
*/
45-
public static function cutStart(string $str, string $subString = ' ') : string
46-
{
47-
return preg_replace('/^'. preg_quote($subString, '/') . '/', '', $str);
55+
public static function cutStart(
56+
string $str,
57+
string $subString = ' ',
58+
bool $repeat = false,
59+
bool $caseSensitive = true
60+
) : string {
61+
$prepared = RegEx::prepare($subString, '/');
62+
$regex = sprintf(
63+
'/^%s/%s',
64+
($subString
65+
? ($repeat ? RegexHelper::quantifyGroup($prepared, 0) : $prepared)
66+
: ''
67+
),
68+
(!$caseSensitive ? 'i' : '')
69+
);
70+
71+
return preg_replace($regex, '', $str);
4872
}
4973

5074
/**
5175
* Cut substring from the end of string
5276
*
5377
* @param string $str
5478
* @param string $subString
79+
* @param bool $repeat
80+
* @param bool $caseSensitive Not working with multi-byte characters
5581
*
5682
* @return string
5783
*/
58-
public static function cutEnd(string $str, string $subString = ' ') : string
59-
{
60-
return preg_replace('/'. preg_quote($subString, '/') . '$/', '', $str);
84+
public static function cutEnd(
85+
string $str,
86+
string $subString = ' ',
87+
bool $repeat = false,
88+
bool $caseSensitive = true
89+
) : string {
90+
$prepared = RegEx::prepare($subString, '/');
91+
$regex = sprintf(
92+
'/%s$/%s',
93+
($subString
94+
? ($repeat ? RegexHelper::quantifyGroup($prepared, 0) : $prepared)
95+
: ''
96+
),
97+
(!$caseSensitive ? 'i' : '')
98+
);
99+
100+
return preg_replace($regex, '', $str);
61101
}
62102
}

src/Helpers/Regex.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Larapulse\Support\Helpers;
4+
5+
class Regex
6+
{
7+
public static function quantifyGroup(string $str, $min = null, $max = INF, string $tag = null) : string
8+
{
9+
$tag = $tag ? "?<{$tag}>" : '';
10+
11+
return "({$tag}{$str})".self::fetchQuantifier($min, $max);
12+
}
13+
14+
/**
15+
* Build quantifier from parameters
16+
*
17+
* @param int|null $min
18+
* @param float|int $max
19+
* @param bool $lazyLoad
20+
*
21+
* @return string
22+
*/
23+
public static function fetchQuantifier($min = null, $max = INF, bool $lazyLoad = false) : string
24+
{
25+
$lazy = $lazyLoad ? '?' : '';
26+
27+
switch (true) {
28+
case ($min === 0 && $max === 1):
29+
return '?'.$lazy;
30+
case ($min === 0 && $max === INF):
31+
return '*'.$lazy;
32+
case ($min === 1 && $max === INF):
33+
return '+'.$lazy;
34+
case (is_int($min) && $min >= 1 && $max === null):
35+
return '{'.$min.'}'.$lazy;
36+
case (is_int($min) && (is_int($max) || $max === INF)):
37+
$max = is_int($max) ? $max : '';
38+
return '{'.$min.','.$max.'}'.$lazy;
39+
default:
40+
return '';
41+
}
42+
}
43+
}

src/array_functions.php

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ function array_flatten($array, $depth = INF) : array
2222
* Flatten a multi-dimensional array into a single level with saving keys
2323
*
2424
* @param array $array
25+
* @param int $depth
2526
* @return array
2627
*/
27-
function array_flatten_assoc(array $array) : array
28+
function array_flatten_assoc(array $array, $depth = INF) : array
2829
{
29-
return Arr::flattenAssoc($array);
30+
return Arr::flattenAssoc($array, $depth);
3031
}
3132
}
3233

@@ -102,43 +103,3 @@ function array_is_assoc(array $array) : bool
102103
return IlluminateArr::isAssoc($array);
103104
}
104105
}
105-
106-
if (!function_exists('array_only')) {
107-
/**
108-
* Get a subset of the items from the given array
109-
*
110-
* @param array $array
111-
* @param array|string $keys
112-
* @return array
113-
*/
114-
function array_only($array, $keys) : array
115-
{
116-
return IlluminateArr::only($array, $keys);
117-
}
118-
}
119-
120-
if (!function_exists('array_head')) {
121-
/**
122-
* Get the first element of an array. Useful for method chaining
123-
*
124-
* @param array $array
125-
* @return mixed
126-
*/
127-
function array_head($array)
128-
{
129-
return reset($array);
130-
}
131-
}
132-
133-
if (!function_exists('array_last')) {
134-
/**
135-
* Get the last element from an array
136-
*
137-
* @param array $array
138-
* @return mixed
139-
*/
140-
function array_last($array)
141-
{
142-
return end($array);
143-
}
144-
}

0 commit comments

Comments
 (0)