Skip to content

Commit ab8fa0f

Browse files
authored
Use type-utils for type casting and add more methods (#3)
Use type-utils for type casting and add more methods Added methods: * [type]OrDefault * [type]OrNull * [types]Filtered * [types]Forced
1 parent ebdb2d5 commit ab8fa0f

File tree

8 files changed

+744
-46
lines changed

8 files changed

+744
-46
lines changed

README.md

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,112 @@
11
# Gowork Safe - Type Safety Tools
22

3-
Mainly for Symfony
3+
Safe accessors wraps unsafe or uncertain associative data structures and provides methods of safe type casting.
4+
Mainly for Symfony.
5+
6+
## SafeAccessorTrait methods
7+
8+
#### `bool(string $key, bool $default = false): bool`
9+
* casts value to `bool` (true, false, 0, 1) if possible
10+
* or throws `InvalidArgumentException` when value set but cannot be casted
11+
* or returns default when value not set
12+
13+
#### `boolOrDefault(string $key, bool $default): bool`
14+
* casts value to `bool` (true, false, 0, 1)
15+
* or returns default
16+
17+
#### `string(string $key, string $default = ''): string`
18+
* casts value to `string` if possible
19+
* or throws `InvalidArgumentException` when value set but cannot be casted
20+
* or returns default when value not set
21+
22+
#### `stringNullable(string $key, ?string $default = null): ?string`
23+
* casts value to `string` if possible
24+
* or returns default when value not set or is `null`
25+
* or throws `InvalidArgumentException` when value not `null` but cannot be casted
26+
27+
#### `stringOrNull(string $key): ?string`
28+
* casts value to `string` if possible
29+
* or returns `null`
30+
31+
#### `stringOrDefault(string $key, string $default): string`
32+
* casts value to `string` if possible
33+
* or returns default
34+
35+
#### `int(string $key, int $default = 0): int`
36+
* casts value to `int` if possible
37+
* or throws `InvalidArgumentException` when value set but cannot be casted
38+
* or returns default when value not set
39+
40+
#### `intNullable(string $key, ?int $default = null): ?int`
41+
* casts value to `int` if possible
42+
* or returns default when value not set or is `null`
43+
* or throws `InvalidArgumentException` when value not `null` but cannot be casted
44+
45+
#### `intOrNull(string $key): ?int`
46+
* casts value to `int` if possible
47+
* or returns `null`
48+
49+
#### `intOrDefault(string $key, int $default): int`
50+
* casts value to `int` if possible
51+
* or returns default
52+
53+
#### `float(string $key, float $default = 0): float`
54+
* casts value to `float` if possible
55+
* or throws `InvalidArgumentException` when value set but cannot be casted
56+
* or returns default when value not set
57+
58+
#### `floatNullable(string $key, ?float $default = null): ?float`
59+
* casts value to `float` if possible
60+
* or returns default when value not set or is `null`
61+
* or throws `InvalidArgumentException` when value not `null` but cannot be casted
62+
63+
#### `floatOrNull(string $key): ?float`
64+
* casts value to `float` if possible
65+
* or returns `null`
66+
67+
#### `floatOrDefault(string $key, float $default): float`
68+
* casts value to `float` if possible
69+
* or returns default
70+
71+
#### `strings(string $key): array<int, string>`
72+
* casts value to array of strings if possible
73+
* or throws `InvalidArgumentException` when some item cannot be casted
74+
75+
#### `stringsFiltered(string $key): array<int, string>`
76+
* casts value to array of strings skipping items that cannot be casted
77+
78+
#### `stringsForced(string $key, string $default = ''): array<int, string>`
79+
* casts value to array of strings replacing with default items that cannot be casted
80+
81+
#### `ints(string $key): array<int, int>`
82+
* casts value to array of ints if possible
83+
* or throws `InvalidArgumentException` when some item cannot be casted
84+
85+
#### `intsFiltered(string $key): array<int, int>`
86+
* casts value to array of ints skipping items that cannot be casted
87+
88+
#### `intsForced(string $key, int $default = ''): array<int, int>`
89+
* casts value to array of ints replacing with default items that cannot be casted
90+
91+
#### `floats(string $key): array<int, float>`
92+
* casts value to array of floats if possible
93+
* or throws `InvalidArgumentException` when some item cannot be casted
94+
95+
#### `floatsFiltered(string $key): array<int, float>`
96+
* casts value to array of floats skipping items that cannot be casted
97+
98+
#### `floatsForced(string $key, float $default = ''): array<int, float>`
99+
* casts value to array of floats replacing with default items that cannot be casted
100+
101+
#### `array(string $key): SafeAssocArray`
102+
* casts value to associative array and wraps with `SafeAssocArray`
103+
* or throws `InvalidArgumentException` when value cannot be casted
104+
105+
#### `list(string $key): SafeAssocList`
106+
* casts value to list of associative arrays and wraps with `SafeAssocList`
107+
* or throws `InvalidArgumentException` when value cannot be casted
108+
109+
## Accessors
4110

5111
### SafeAssocArray
6112

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"type": "library",
55
"license": "MIT",
66
"require": {
7-
"php": "^7.4"
7+
"php": "^7.4",
8+
"dazet/type-utils": "^0.1.0"
89
},
910
"require-dev": {
1011
"phpunit/phpunit": "^8.1",

0 commit comments

Comments
 (0)