Skip to content

Commit 76c9fa3

Browse files
committed
Added docs to Utils classes
1 parent d21bee6 commit 76c9fa3

File tree

11 files changed

+388
-2
lines changed

11 files changed

+388
-2
lines changed

src-utils/Arrays.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44

55
use WeakMap;
66

7+
/**
8+
* Utility functions for working with arrays.
9+
*/
710
class Arrays
811
{
12+
/**
13+
* Merges two arrays, handling null values.
14+
* @param array|null $array1
15+
* @param array|null $array2
16+
* @return array|null
17+
*/
918
public static function mergeNull(?array $array1, ?array $array2): ?array {
1019
return match(true) {
1120
is_null($array1) && is_null($array2) => null,
@@ -15,6 +24,12 @@ public static function mergeNull(?array $array1, ?array $array2): ?array {
1524
};
1625
}
1726

27+
/**
28+
* Merges two arrays, handling null values.
29+
* @param array|null $array1
30+
* @param array|null $array2
31+
* @return array|null
32+
*/
1833
public static function unset(array $array, array|string $fields) : array {
1934
if (!is_array($fields)) {
2035
$fields = [$fields];
@@ -25,6 +40,11 @@ public static function unset(array $array, array|string $fields) : array {
2540
return $array;
2641
}
2742

43+
/**
44+
* Converts a value to an array.
45+
* @param mixed $value
46+
* @return array
47+
*/
2848
public static function asArray(mixed $value): array {
2949
if (is_array($value)) {
3050
return $value;
@@ -35,21 +55,45 @@ public static function asArray(mixed $value): array {
3555
return [$value];
3656
}
3757

58+
/**
59+
* Checks if an array is a subset of another array.
60+
* @param array $decodedKeys
61+
* @param array $propertyNames
62+
* @return bool
63+
*/
3864
static public function isSubset(array $decodedKeys, array $propertyNames) {
3965
return count(array_diff($decodedKeys, $propertyNames)) === 0;
4066
}
4167

68+
/**
69+
* Removes the last N elements from an array.
70+
* @param array $array
71+
* @param int $count
72+
* @return array
73+
*/
4274
static public function removeTail(array $array, int $count) : array {
4375
if ($count < 1) {
4476
return $array;
4577
}
4678
return array_slice($array, 0, -$count);
4779
}
4880

81+
/**
82+
* Flattens an array of arrays into a single string.
83+
* @param array $arrays
84+
* @param string $separator
85+
* @return string
86+
*/
4987
static public function flatten(array $arrays, string $separator = ''): string {
5088
return self::doFlatten($arrays, $separator);
5189
}
5290

91+
/**
92+
* Maps an array using a callback.
93+
* @param array $array
94+
* @param callable $callback
95+
* @return array
96+
*/
5397
static public function map(array $array, callable $callback): array {
5498
$target = [];
5599
foreach ($array as $key => $value) {
@@ -58,6 +102,12 @@ static public function map(array $array, callable $callback): array {
58102
return $target;
59103
}
60104

105+
/**
106+
* Filters an array using a callback.
107+
* @param array $array
108+
* @param callable $callback
109+
* @return array
110+
*/
61111
static public function fromAny(mixed $anyValue): array {
62112
$visited = new WeakMap();
63113
$toArray = function($x) use(&$toArray, &$visited) {
@@ -75,6 +125,12 @@ static public function fromAny(mixed $anyValue): array {
75125
return $toArray($anyValue);
76126
}
77127

128+
/**
129+
* Filters an array using a callback.
130+
* @param array $array
131+
* @param callable $callback
132+
* @return array
133+
*/
78134
static public function removeRecursively(array $array, array $keys): array {
79135
if (empty($array) || empty($keys)) {
80136
return $array;
@@ -92,12 +148,24 @@ static public function removeRecursively(array $array, array $keys): array {
92148
return $remove($array, $keys);
93149
}
94150

151+
/**
152+
* Filters an array using a callback.
153+
* @param array $array
154+
* @param callable $callback
155+
* @return array
156+
*/
95157
static public function toBullets(array $array): string {
96158
return implode("\n", array_map(fn($c) => " - {$c}\n", $array));
97159
}
98160

99161
// INTERNAL ///////////////////////////////////////////////////////
100162

163+
/**
164+
* Flattens an array of arrays into a single string.
165+
* @param array $arrays
166+
* @param string $separator
167+
* @return string
168+
*/
101169
private static function doFlatten(array $arrays, string $separator): string {
102170
$flat = '';
103171
foreach ($arrays as $item) {

src-utils/DataMap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use JsonSerializable;
99

1010
/**
11+
* DataMap provides a simple way to work with nested data structures.
1112
* @template TKey of array-key
1213
* @template TValue
1314
*/

src-utils/Files.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use RecursiveIteratorIterator;
1010
use RuntimeException;
1111

12+
/**
13+
* Utility class for working with files and directories.
14+
*/
1215
class Files
1316
{
1417
/**

src-utils/RawChain.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
*
3838
* return $chain->process(5);
3939
*/
40-
4140
class RawChain {
4241
/**
4342
* @var callable[] The processors that the payload will be passed through.

src-utils/ResultChain.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,43 @@ public function __construct(
7171

7272
// CREATION ///////////////////////////////////////////////////////////////////////////////
7373

74+
/**
75+
* Create a new ResultChain instance.
76+
*
77+
* @return ResultChain
78+
*/
7479
static public function make() : static {
7580
return new ResultChain();
7681
}
7782

83+
/**
84+
* Create a new ResultChain instance with an initial value.
85+
*
86+
* @param mixed $value
87+
* @return ResultChain
88+
*/
7889
static public function for(mixed $value): static {
7990
return new ResultChain(value: Result::with($value));
8091
}
8192

93+
/**
94+
* Create a new ResultChain instance with a source of values.
95+
*
96+
* @param callable $source
97+
* @return ResultChain
98+
*/
8299
static public function from(callable $source): static {
83100
return new ResultChain(source: $source);
84101
}
85102

86103
// DEFINITION /////////////////////////////////////////////////////////////////////////////
87104

105+
/**
106+
* Set the initial value of the chain.
107+
*
108+
* @param mixed $value
109+
* @return ResultChain
110+
*/
88111
public function withContext(mixed $context): static {
89112
$this->context = $context;
90113
return $this;
@@ -248,6 +271,13 @@ private function getCarry() : mixed {
248271
};
249272
}
250273

274+
/**
275+
* Process the value with the processor.
276+
*
277+
* @param callable $processor
278+
* @param mixed $value
279+
* @return mixed
280+
*/
251281
private function processValue(callable $processor, mixed $value): mixed {
252282
return match(true) {
253283
$value instanceof Result => $processor($value->unwrap()),
@@ -256,6 +286,13 @@ private function processValue(callable $processor, mixed $value): mixed {
256286
};
257287
}
258288

289+
/**
290+
* Wrap the value into a Result object or unwrap it if it is already a Result.
291+
*
292+
* @param mixed $value
293+
* @param string $onNull
294+
* @return Result|null
295+
*/
259296
private function asResult(mixed $value, string $onNull = self::FAIL_ON_NULL) : ?Result {
260297
return match(true) {
261298
$value instanceof Result => $value,
@@ -268,13 +305,25 @@ private function asResult(mixed $value, string $onNull = self::FAIL_ON_NULL) : ?
268305
};
269306
}
270307

308+
/**
309+
* Wrap or unwrap the value based on the flag.
310+
* @param mixed $value
311+
* @param bool $wrapped
312+
* @return mixed
313+
*/
271314
private function wrapOrUnwrap(mixed $value, bool $wrapped = true): mixed {
272315
return match(true) {
273316
$wrapped => $this->asResult($value, self::CONTINUE_ON_NULL),
274317
default => $this->unwrapIfResult($value),
275318
};
276319
}
277320

321+
/**
322+
* Unwrap the value if it is a Result object.
323+
*
324+
* @param mixed $value
325+
* @return mixed
326+
*/
278327
private function unwrapIfResult(mixed $value): mixed {
279328
return match(true) {
280329
$value instanceof Success => $value->unwrap(),

src-utils/Settings.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public static function set(string $group, string $key, mixed $value) : void {
105105
self::$settings[$group] = self::$settings[$group]->set($key, $value);
106106
}
107107

108+
/**
109+
* Unsets a settings group.
110+
*
111+
* @param string $group The settings group.
112+
*/
108113
public static function unset(string $group) : void {
109114
if (self::isGroupLoaded($group)) {
110115
self::$settings[$group] = [];

0 commit comments

Comments
 (0)