Skip to content

Commit b5df3c7

Browse files
committed
v2.0.1: add new String and Array Helpers
1 parent f8c559e commit b5df3c7

23 files changed

+587
-131
lines changed

CHANGELOG.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
6+
7+
## v2.0.1
8+
9+
### Added
10+
11+
- Add `CHANGELOG.md`
12+
- Add new Array helpers:
13+
+ `get` Get an item from an array using "dot" notation.
14+
- Add new String helpers:
15+
+ `toSnake` Converts a string to `snake_case`
16+
+ `toScreamingSnake` Converts a string to `SCREAMING_SNAKE_CASE`
17+
+ `toKebab` Converts a string to `kebab-case`
18+
+ `toCamel` Converts a string to `CamelCase`
19+
+ `toLowerCamel` Converts a string to `lowerCamelCase`
20+
+ `removeMultiSpace` Converts all multi-spaced characters to once
21+
22+
## v2.0.0
23+
24+
### Reformat
25+
26+
[keepachangelog]:https://keepachangelog.com/en/1.0.0/
27+
[semver]:https://semver.org/spec/v2.0.0.html

candidates/Helpers/Arr.php

-55
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Php\Support\Candidates\Helpers;
66

7-
use ArrayAccess;
8-
97
/**
108
* Class Arr
119
*
@@ -43,59 +41,6 @@ private static function itemReplaceByTemplate(&$item, array $replace): void
4341
}
4442
}
4543

46-
/**
47-
* Remove elements from array by value
48-
*
49-
* @param array $array
50-
* @param mixed $val
51-
*/
52-
public static function removeByValue(array &$array, $val): void
53-
{
54-
if (($key = array_search($val, $array, false)) !== false) {
55-
unset($array[$key]);
56-
}
57-
}
58-
59-
60-
/**
61-
* Get an item from an array using "dot" notation.
62-
*
63-
* @param ArrayAccess|array $array
64-
* @param null|string $key
65-
* @param mixed $default
66-
*
67-
* @return mixed
68-
*/
69-
public static function get($array, ?string $key, $default = null)
70-
{
71-
if (!static::accessible($array)) {
72-
return value($default);
73-
}
74-
75-
if ($key === null) {
76-
return $array;
77-
}
78-
79-
if (static::exists($array, $key)) {
80-
return $array[$key];
81-
}
82-
83-
if (strpos($key, '.') === false) {
84-
return $array[$key] ?? value($default);
85-
}
86-
87-
foreach (explode('.', $key) as $segment) {
88-
if (static::accessible($array) && static::exists($array, $segment)) {
89-
$array = $array[$segment];
90-
} else {
91-
return value($default);
92-
}
93-
}
94-
95-
return $array;
96-
}
97-
98-
9944
/**
10045
* @param string|null $s
10146
* @param int $start

candidates/Helpers/Str.php

-61
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,13 @@
44

55
namespace Php\Support\Candidates\Helpers;
66

7-
use function mb_strtolower;
8-
use function preg_replace;
9-
107
/**
118
* Class Str
129
*
1310
* @package Php\Support\Helpers
1411
*/
1512
class Str
1613
{
17-
/**
18-
* The cache of studly-cased words.
19-
*
20-
* @var array
21-
*/
22-
protected static $studlyCache = [];
23-
/**
24-
* The cache of snake-cased words.
25-
*
26-
* @var array
27-
*/
28-
protected static $snakeCache = [];
2914

3015
/**
3116
* Replace templates into string
@@ -41,50 +26,4 @@ public static function stringReplaceByTemplate(string $str, array $replace)
4126
{
4227
return str_replace(array_keys($replace), array_values($replace), $str);
4328
}
44-
45-
/**
46-
* Convert a value to studly caps case.
47-
*
48-
* @param string $value
49-
*
50-
* @return string
51-
*/
52-
public static function studly($value): string
53-
{
54-
$key = $value;
55-
56-
if (isset(static::$studlyCache[$key])) {
57-
return static::$studlyCache[$key];
58-
}
59-
60-
$value = ucwords(str_replace(['-', '_'], ' ', $value));
61-
62-
return static::$studlyCache[$key] = str_replace(' ', '', $value);
63-
}
64-
65-
/**
66-
* Convert a string to snake case.
67-
*
68-
* @param string $value
69-
* @param string $delimiter
70-
*
71-
* @return string
72-
*/
73-
public static function snake($value, $delimiter = '_'): string
74-
{
75-
$key = $value;
76-
77-
if (isset(static::$snakeCache[$key][$delimiter])) {
78-
return static::$snakeCache[$key][$delimiter];
79-
}
80-
81-
if (!ctype_lower($value)) {
82-
$value = preg_replace('/\s+/u', '', ucwords($value));
83-
84-
$value = preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, (string)$value);
85-
$value = mb_strtolower((string)$value, 'UTF-8');
86-
}
87-
88-
return static::$snakeCache[$key][$delimiter] = $value;
89-
}
9029
}

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
![](https://img.shields.io/badge/php->=7.2-blue.svg)
33
[![Latest Stable Version](https://poser.pugx.org/efureev/support/v/stable?format=flat)](https://packagist.org/packages/efureev/support)
44

5-
[![Build Status](https://travis-ci.org/efureev/php-support.svg?branch=master)](https://travis-ci.org/efureev/php-support)
5+
[![Build Status](https://travis-ci.org/efureev/php-support.svg?branch=v2)](https://travis-ci.org/efureev/php-support/tree/v2)
66
[![Maintainability](https://api.codeclimate.com/v1/badges/a7cf8708bf58fa7e5096/maintainability)](https://codeclimate.com/github/efureev/php-support/maintainability)
77
[![Test Coverage](https://api.codeclimate.com/v1/badges/a7cf8708bf58fa7e5096/test_coverage)](https://codeclimate.com/github/efureev/php-support/test_coverage)
88
[![codecov](https://codecov.io/gh/efureev/php-support/branch/master/graph/badge.svg)](https://codecov.io/gh/efureev/php-support)

src/Exceptions/ConfigException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/Exception.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/InvalidArgumentException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/InvalidCallException.php

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace Php\Support\Exceptions;
45

src/Exceptions/InvalidConfigException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/InvalidParamException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/InvalidValueException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/JsonException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/MethodNotAllowedException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/MissingClassException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/MissingConfigException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/MissingPropertyException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/NotSupportedException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/UnknownMethodException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Exceptions/UnknownPropertyException.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace Php\Support\Exceptions;

src/Helpers/Arr.php

+38
Original file line numberDiff line numberDiff line change
@@ -204,4 +204,42 @@ public static function toIndexedArray(array $array): array
204204

205205
return $array;
206206
}
207+
208+
/**
209+
* Get an item from an array using "dot" notation.
210+
*
211+
* @param mixed $array
212+
* @param null|string $key
213+
* @param mixed $default
214+
*
215+
* @return mixed
216+
*/
217+
public static function get($array, ?string $key, $default = null)
218+
{
219+
if (!static::accessible($array)) {
220+
return value($default);
221+
}
222+
223+
if ($key === null) {
224+
return $array;
225+
}
226+
227+
if (static::exists($array, $key)) {
228+
return $array[$key];
229+
}
230+
231+
if (strpos($key, '.') === false) {
232+
return $array[$key] ?? value($default);
233+
}
234+
235+
foreach (explode('.', $key) as $segment) {
236+
if (static::accessible($array) && static::exists($array, $segment)) {
237+
$array = $array[$segment];
238+
} else {
239+
return value($default);
240+
}
241+
}
242+
243+
return $array;
244+
}
207245
}

0 commit comments

Comments
 (0)