Skip to content

Commit a99aff0

Browse files
committed
update docs
1 parent 5de93e7 commit a99aff0

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

README.md

+30-3
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,53 @@ composer require nejcc/php-datatypes
3535
Below are examples of how to use the basic integer and float classes in your project.
3636

3737

38+
This approach has a few key benefits:
39+
40+
- Type Safety: By explicitly defining the data types like UInt8, you're eliminating the risk of invalid values sneaking into your application. For example, enforcing unsigned integers ensures that the value remains within valid ranges, offering a safeguard against unexpected data inputs.
41+
42+
43+
- Precision: Especially with floating-point numbers, handling precision can be tricky in PHP due to how it manages floats natively. By offering precise types such as Float32 or Float64, we're giving developers the control they need to maintain consistency in calculations.
44+
45+
46+
- Range Safeguards: By specifying exact ranges, you can prevent issues like overflows or underflows that often go unchecked in dynamic typing languages like PHP.
47+
48+
49+
- Readability and Maintenance: Explicit data types improve code readability. When a developer reads your code, they instantly know what type of value is expected and the constraints around that value. This enhances long-term maintainability.
50+
3851
### Laravel example
3952

53+
here's how it can be used in practice across different types, focusing on strict handling for both integers and floats:
4054
```php
41-
<?php
42-
4355
namespace App\Http\Controllers;
4456

4557
use Illuminate\Http\Request;
4658
use Nejcc\PhpDatatypes\Integers\Unsigned\UInt8;
59+
use Nejcc\PhpDatatypes\Floats\Float32;
4760

4861
class TestController
4962
{
5063
public UInt8 $user_id;
64+
public Float32 $account_balance;
65+
5166
public function __invoke(Request $request)
5267
{
68+
// Validating and assigning UInt8 (ensures non-negative user ID)
5369
$this->user_id = uint8($request->input('user_id'));
54-
dd($this->user_id->getValue());
70+
71+
// Validating and assigning Float32 (ensures correct precision)
72+
$this->account_balance = float32($request->input('account_balance'));
73+
74+
// Now you can safely use the $user_id and $account_balance knowing they are in the right range
75+
dd([
76+
'user_id' => $this->user_id->getValue(),
77+
'account_balance' => $this->account_balance->getValue(),
78+
]);
5579
}
5680
}
81+
5782
```
83+
Here, we're not only safeguarding user IDs but also handling potentially complex floating-point operations, where precision is critical. This could be especially beneficial for applications in fields like finance or analytics where data integrity is paramount.
84+
5885

5986
PHP examples
6087

0 commit comments

Comments
 (0)