diff --git a/README.md b/README.md index 399b694..93bd92f 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Want to provide a default value when the key is missing? Here you go: Typed::string($data, 'some.key', 'Default Value'); ``` -## 2. Installation +## 2. Installation and usage Typed class is distributed as a Composer package, making installation straightforward: @@ -64,6 +64,14 @@ After installation, ensure that your application includes the Composer autoloade `require __DIR__ . '/vendor/autoload.php';` +Usage: + +```php +use WPLake\Typed\Typed; + +$string = Typed::string($array, 'first.second','default value'); +``` + ## 3. Supported types Static methods for the following types are present: @@ -77,7 +85,12 @@ Static methods for the following types are present: * `dateTime` * `any` (allows to use short dot-keys usage for unknowns) -For optional cases, each type has an `OrNull` method option (e.g. `stringOrNull`), which returns `null` if the key +Additionally: + +* `boolExtended` (`true`,`1`,`"1"`, `"on"` are treated as true, `false`,`0`,`"0"`, `"off"` as false) +* `stringExtended` (supports objects with `__toString`) + +> Plus, for optional cases, each item has an `OrNull` method option (e.g. `stringOrNull`, `intOrNull`, and so on), which returns `null` if the key doesn’t exist. ## 4. How It Works @@ -123,6 +136,12 @@ $userName = Typed::string($companyObject, 'user.name'); $userName = Typed::string($companyObject,'users.john.name'); ``` +In all the cases, you can pass a default value as the third argument, e.g.: + +```php +$userName = Typed::string($companyObject,'users.john.name', 'Guest'); +``` + ## 5. FAQ ### 5.1) Why not just Null Coalescing Operator? diff --git a/src/Typed.php b/src/Typed.php index 96f0812..5f98e09 100644 --- a/src/Typed.php +++ b/src/Typed.php @@ -58,6 +58,31 @@ public static function string($source, $key = null, string $default = ''): strin $default; } + /** + * @param mixed $source + * @param int|string|null $key + */ + public static function stringExtended($source, $key = null, string $default = ''): string + { + $value = self::any($source, $key, $default); + + if ( + true === is_string($value) || + true === is_numeric($value) + ) { + return (string)$value; + } + + if ( + true === is_object($value) && + true === method_exists($value, '__toString') + ) { + return (string)$value; + } + + return $default; + } + /** * @param mixed $source * @param int|string|null $key @@ -72,6 +97,31 @@ public static function stringOrNull($source, $key = null): ?string null; } + /** + * @param mixed $source + * @param int|string|null $key + */ + public static function stringExtendedOrNull($source, $key = null): ?string + { + $value = self::any($source, $key); + + if ( + true === is_string($value) || + true === is_numeric($value) + ) { + return (string)$value; + } + + if ( + true === is_object($value) && + true === method_exists($value, '__toString') + ) { + return (string)$value; + } + + return null; + } + /** * @param mixed $source * @param int|string|null $key @@ -124,13 +174,39 @@ public static function floatOrNull($source, $key = null): ?float null; } + /** + * @param mixed $source + * @param int|string|null $key + */ + public static function bool($source, $key = null, bool $default = false): bool + { + $value = self::any($source, $key, $default); + + return true === is_bool($value) ? + $value : + $default; + } + + /** + * @param mixed $source + * @param int|string|null $key + */ + public static function boolOrNull($source, $key = null): ?bool + { + $value = self::any($source, $key); + + return true === is_bool($value) ? + $value : + null; + } + /** * @param mixed $source * @param int|string|null $key * @param array $positive * @param array $negative */ - public static function bool( + public static function boolExtended( $source, $key = null, bool $default = false, @@ -156,7 +232,7 @@ public static function bool( * @param array $positive * @param array $negative */ - public static function boolOrNull( + public static function boolExtendedOrNull( $source, $key = null, array $positive = [true, 1, '1', 'on',], @@ -175,32 +251,6 @@ public static function boolOrNull( return null; } - /** - * @param mixed $source - * @param int|string|null $key - */ - public static function strictBool($source, $key = null, bool $default = false): bool - { - $value = self::any($source, $key, $default); - - return true === is_bool($value) ? - $value : - $default; - } - - /** - * @param mixed $source - * @param int|string|null $key - */ - public static function strictBoolOrNull($source, $key = null): ?bool - { - $value = self::any($source, $key); - - return true === is_bool($value) ? - $value : - null; - } - /** * @param mixed $source * @param int|string|null $key