-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip: update checkArrayType with castable values for #49 * wip: update examples * feature: consistent type safety within arrays closes #49 * tidy: reduce cyclomatic complexity * tidy: split example JSON
- Loading branch information
Showing
6 changed files
with
168 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
use Gt\DataObject\DataObject; | ||
|
||
require __DIR__ . "/../vendor/autoload.php"; | ||
|
||
$obj = (new DataObject()) | ||
->with("name", "Cody") | ||
->with("colour", "orange") | ||
->with("food", [ | ||
"biscuits", | ||
"mushrooms", | ||
"corn on the cob", | ||
]); | ||
|
||
echo json_encode($obj), PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
use Gt\DataObject\DataObjectBuilder; | ||
|
||
require __DIR__ . "/../vendor/autoload.php"; | ||
|
||
$jsonString = <<<JSON | ||
{ | ||
"name": "Cody", | ||
"colour": "orange", | ||
"food": [ | ||
"biscuits", | ||
"mushrooms", | ||
"corn on the cob" | ||
] | ||
} | ||
JSON; | ||
|
||
$builder = new DataObjectBuilder(); | ||
$obj = $builder->fromObject(json_decode($jsonString)); | ||
|
||
echo "Hello, ", | ||
$obj->getString("name"), | ||
"! Your favourite food is ", | ||
$obj->getArray("food")[0], | ||
PHP_EOL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use Gt\DataObject\DataObject; | ||
|
||
require __DIR__ . "/../vendor/autoload.php"; | ||
|
||
// Set an object with all string values, similar to a web requests: | ||
$obj = new DataObject(); | ||
$obj = $obj->with("one", "1"); | ||
$obj = $obj->with("two", "two"); | ||
$obj = $obj->with("pi", "3.14159"); | ||
|
||
// Automatically cast to int: | ||
|
||
$int1 = $obj->getInt("one"); | ||
$int2 = $obj->getInt("two"); | ||
$int3 = $obj->getInt("pi"); | ||
|
||
echo "One: $int1, two: $int2, three: $int3", PHP_EOL; | ||
// Outputs: One: 1, two: 0, three: 3 | ||
// Note how the decimal data is lost in the cast to int, | ||
// but how the original data is not lost. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
use Gt\DataObject\DataObject; | ||
|
||
require __DIR__ . "/../vendor/autoload.php"; | ||
|
||
$obj = new DataObject(); | ||
$obj = $obj->with("arrayOfData", [ | ||
1, | ||
2, | ||
3.14159, | ||
]); | ||
|
||
echo "The third element in the array is: ", | ||
$obj->getArray("arrayOfData", "int")[2], // note the type check of "int" | ||
PHP_EOL; | ||
|
||
/* Output: | ||
The third element in the array is: 3 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters