Skip to content

Commit 383cc32

Browse files
authored
Merge pull request #5 from farzai/fix-number-value
Fix Array helper can't access numeric
2 parents fc11fcd + a2b91f2 commit 383cc32

4 files changed

Lines changed: 286 additions & 15 deletions

File tree

src/Arr.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ class Arr
1414
*/
1515
public static function get($array, $key, $default = null)
1616
{
17-
if (! static::accessible($array)) {
18-
return $default;
19-
}
20-
2117
if (is_null($key)) {
2218
return $array;
2319
}
2420

21+
if (! static::accessible($array)) {
22+
return $default;
23+
}
24+
2525
foreach (explode('.', $key) as $segment) {
26-
if (static::accessible($array) && static::exists($array, $segment)) {
26+
if (! static::exists($array, $segment)) {
27+
return $default;
28+
}
29+
30+
if (static::accessible($array[$segment])) {
2731
$array = $array[$segment];
2832
} else {
29-
return $default;
33+
return $array[$segment];
3034
}
3135
}
3236

@@ -39,7 +43,7 @@ public static function get($array, $key, $default = null)
3943
public static function exists($array, $key): bool
4044
{
4145
foreach (explode('.', $key) as $segment) {
42-
if (static::accessible($array) && ($array[$segment] ?? false)) {
46+
if (isset($array[$segment])) {
4347
$array = $array[$segment];
4448
} else {
4549
return false;

src/Str.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,178 @@ public static function replace($search, $replace, $subject)
3737
{
3838
return str_replace($search, $replace, $subject);
3939
}
40+
41+
public static function startsWith($haystack, $needles)
42+
{
43+
foreach ((array) $needles as $needle) {
44+
if ($needle !== '' && strncmp($haystack, $needle, strlen($needle)) === 0) {
45+
return true;
46+
}
47+
}
48+
49+
return false;
50+
}
51+
52+
public static function endsWith($haystack, $needles)
53+
{
54+
foreach ((array) $needles as $needle) {
55+
if ((string) $needle === static::substr($haystack, -static::length($needle))) {
56+
return true;
57+
}
58+
}
59+
60+
return false;
61+
}
62+
63+
public static function length($value)
64+
{
65+
return mb_strlen($value);
66+
}
67+
68+
public static function substr($string, $start, $length = null)
69+
{
70+
return mb_substr($string, $start, $length, 'UTF-8');
71+
}
72+
73+
public static function contains($haystack, $needles)
74+
{
75+
foreach ((array) $needles as $needle) {
76+
if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
77+
return true;
78+
}
79+
}
80+
81+
return false;
82+
}
83+
84+
public static function isSnakeCase($value)
85+
{
86+
return $value === static::snake($value);
87+
}
88+
89+
public static function isCamelCase($value)
90+
{
91+
return $value === static::camel($value);
92+
}
93+
94+
public static function isStudlyCase($value)
95+
{
96+
return $value === static::studly($value);
97+
}
98+
99+
/**
100+
* Generate a more truly "random" alpha-numeric string.
101+
*
102+
* @param int $length
103+
* @return string
104+
*
105+
* @throws \Exception
106+
*/
107+
public static function random($length = 16)
108+
{
109+
$string = '';
110+
111+
while (($len = static::length($string)) < $length) {
112+
$size = $length - $len;
113+
114+
$bytes = random_bytes($size);
115+
116+
$string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
117+
}
118+
119+
return $string;
120+
}
121+
122+
/**
123+
* Generate a more truly "random" alpha-numeric string of ASCII characters.
124+
*
125+
* @param int $length
126+
* @return string
127+
*
128+
* @throws \Exception
129+
*/
130+
public static function randomAscii($length = 16)
131+
{
132+
return static::substr(str_replace(['/', '+', '='], '', base64_encode(random_bytes($length))), 0, $length);
133+
}
134+
135+
/**
136+
* Generate a more truly "random" numeric string.
137+
*
138+
* @param int $length
139+
* @return string
140+
*
141+
* @throws \Exception
142+
*/
143+
public static function randomNumeric($length = 16)
144+
{
145+
$string = '';
146+
147+
while (($len = static::length($string)) < $length) {
148+
$size = $length - $len;
149+
150+
$bytes = random_bytes($size);
151+
152+
$string .= preg_replace('/[^0-9]/', '', base64_encode($bytes));
153+
}
154+
155+
return static::substr($string, 0, $length);
156+
}
157+
158+
/**
159+
* Generate a more truly "random" alpha-numeric string.
160+
*
161+
* @param int $length
162+
* @return string
163+
*
164+
* @throws \Exception
165+
*/
166+
public static function randomAlphanumeric($length = 16)
167+
{
168+
$string = '';
169+
170+
while (($len = static::length($string)) < $length) {
171+
$size = $length - $len;
172+
173+
$bytes = random_bytes($size);
174+
175+
$string .= preg_replace('/[^A-Za-z0-9]/', '', base64_encode($bytes));
176+
}
177+
178+
return static::substr($string, 0, $length);
179+
}
180+
181+
/**
182+
* Generate a more truly "random" string.
183+
*
184+
* @param int $length
185+
* @param string|null $characters
186+
* @return string
187+
*
188+
* @throws \Exception
189+
*/
190+
public static function randomString($length = 16, $characters = null)
191+
{
192+
$string = '';
193+
194+
$characters = $characters ?: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
195+
196+
$max = static::length($characters) - 1;
197+
198+
while (($len = static::length($string)) < $length) {
199+
$string .= $characters[random_int(0, $max)];
200+
}
201+
202+
return $string;
203+
}
204+
205+
public static function randomStringWithNumeric($length = 16)
206+
{
207+
return static::randomString($length, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
208+
}
209+
210+
public static function randomStringWithSpecialCharacter($length = 16)
211+
{
212+
return static::randomString($length, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=[]{};:,.<>/?');
213+
}
40214
}

tests/ArrTest.php

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

33
use Farzai\Support\Arr;
44

5+
it('should return default value if array is not accessible', function () {
6+
$this->assertNull(Arr::get(null, 'foo'));
7+
});
8+
9+
it('should return array if key is null', function () {
10+
$array = [
11+
'foo' => 'bar',
12+
];
13+
14+
$this->assertEquals($array, Arr::get($array, null));
15+
});
16+
17+
it('should return default value if key does not exist', function () {
18+
$array = [
19+
'foo' => 'bar',
20+
];
21+
22+
$this->assertEquals('baz', Arr::get($array, 'qux', 'baz'));
23+
});
24+
525
it('can check if key exists in array', function () {
626
$array = [
727
'foo' => 'bar',
@@ -63,3 +83,13 @@
6383

6484
$this->assertNull(Arr::get($array, 'foo.qux'));
6585
});
86+
87+
it('can get value number from array using dot notation with default value', function () {
88+
$array = [
89+
'foo' => [
90+
'bar' => 0,
91+
],
92+
];
93+
94+
$this->assertEquals(0, Arr::get($array, 'foo.bar', 'qux'));
95+
});

tests/StringTest.php

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,87 @@
33
use Farzai\Support\Str;
44

55
it('can convert string to camel case', function () {
6-
$this->assertEquals('fooBar', Str::camel('FooBar'));
7-
$this->assertEquals('fooBar', Str::camel('foo_bar'));
6+
expect(Str::camel('foo_bar_baz'))->toBe('fooBarBaz');
87
});
98

109
it('can convert string to studly case', function () {
11-
$this->assertEquals('FooBar', Str::studly('fooBar'));
12-
$this->assertEquals('FooBar', Str::studly('foo_bar'));
10+
expect(Str::studly('foo_bar_baz'))->toBe('FooBarBaz');
1311
});
1412

1513
it('can convert string to snake case', function () {
16-
$this->assertEquals('foo_bar', Str::snake('fooBar'));
17-
$this->assertEquals('foo_bar', Str::snake('fooBar', '_'));
14+
expect(Str::snake('fooBarBaz'))->toBe('foo_bar_baz');
1815
});
1916

2017
it('can convert string to lower case', function () {
21-
$this->assertEquals('foo bar baz', Str::lower('FOO BAR BAZ'));
18+
expect(Str::lower('FooBarBaz'))->toBe('foobarbaz');
2219
});
2320

2421
it('can replace string', function () {
25-
$this->assertEquals('bar bar baz', Str::replace('foo', 'bar', 'foo bar baz'));
22+
expect(Str::replace('foo', 'bar', 'foo bar baz'))->toBe('bar bar baz');
23+
});
24+
25+
it('can check if string starts with given value', function () {
26+
expect(Str::startsWith('foo bar baz', 'foo'))->toBeTrue();
27+
expect(Str::startsWith('foo bar baz', 'bar'))->toBeFalse();
28+
});
29+
30+
it('can check if string ends with given value', function () {
31+
expect(Str::endsWith('foo bar baz', 'baz'))->toBeTrue();
32+
expect(Str::endsWith('foo bar baz', 'bar'))->toBeFalse();
33+
});
34+
35+
it('can check if string is in snake case', function () {
36+
expect(Str::isSnakeCase('foo_bar_baz'))->toBeTrue();
37+
expect(Str::isSnakeCase('fooBarBaz'))->toBeFalse();
38+
});
39+
40+
it('can check if string is in camel case', function () {
41+
expect(Str::isCamelCase('fooBarBaz'))->toBeTrue();
42+
expect(Str::isCamelCase('foo_bar_baz'))->toBeFalse();
43+
});
44+
45+
it('can check if string is in studly case', function () {
46+
expect(Str::isStudlyCase('FooBarBaz'))->toBeTrue();
47+
expect(Str::isStudlyCase('foo_bar_baz'))->toBeFalse();
48+
});
49+
50+
it('can randomize string', function () {
51+
expect(Str::random(10))->toHaveLength(10);
52+
});
53+
54+
it('can random ascii', function () {
55+
expect(Str::randomAscii(10))->toHaveLength(10);
56+
});
57+
58+
it('can random numeric', function () {
59+
expect(Str::randomNumeric(10))->toHaveLength(10);
60+
});
61+
62+
it('can get string length', function () {
63+
expect(Str::length('foo bar baz'))->toBe(11);
64+
});
65+
66+
it('can get string substring', function () {
67+
expect(Str::substr('foo bar baz', 0, 3))->toBe('foo');
68+
});
69+
70+
it('can check if string contains given value', function () {
71+
expect(Str::contains('foo bar baz', 'bar'))->toBeTrue();
72+
expect(Str::contains('foo bar baz', 'qux'))->toBeFalse();
73+
});
74+
75+
it('can random alphanumeric', function () {
76+
expect(Str::randomAlphanumeric(10))->toHaveLength(10);
77+
});
78+
79+
it('can random string', function () {
80+
expect(Str::randomString(10))->toHaveLength(10);
81+
});
82+
83+
it('can random string with numeric', function () {
84+
expect(Str::randomStringWithNumeric(10))->toHaveLength(10);
85+
});
86+
87+
it('can random string with special characters', function () {
88+
expect(Str::randomStringWithSpecialCharacter(10))->toHaveLength(10);
2689
});

0 commit comments

Comments
 (0)