Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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?
Expand Down
106 changes: 78 additions & 28 deletions src/Typed.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<int|string,mixed> $positive
* @param array<int|string,mixed> $negative
*/
public static function bool(
public static function boolExtended(
$source,
$key = null,
bool $default = false,
Expand All @@ -156,7 +232,7 @@ public static function bool(
* @param array<int|string,mixed> $positive
* @param array<int|string,mixed> $negative
*/
public static function boolOrNull(
public static function boolExtendedOrNull(
$source,
$key = null,
array $positive = [true, 1, '1', 'on',],
Expand All @@ -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
Expand Down
Loading