-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 340a63d
Showing
4 changed files
with
97 additions
and
0 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 @@ | ||
vendor |
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,24 @@ | ||
# TypeHelper | ||
|
||
* ```TypeHelper::toStrictType($value)``` - return $value to strict type | ||
|
||
## Example | ||
|
||
```php | ||
use \darkfriend\helpers\TypeHelper; | ||
|
||
var_dump( | ||
TypeHelper::toStrict('false'), // bool(false) | ||
TypeHelper::toStrict(null), // NULL | ||
TypeHelper::toStrict(0), // int(0) | ||
TypeHelper::toStrict('0'), // int(0) | ||
TypeHelper::toStrict('1'), // int(1) | ||
TypeHelper::toStrict(1), // int(1) | ||
TypeHelper::toStrict('1.1'), // float(1.1) | ||
TypeHelper::toStrict(1.1), // float(1.1) | ||
TypeHelper::toStrict(true), // bool(true) | ||
TypeHelper::toStrict(false), // bool(false) | ||
TypeHelper::toStrict('my string'), // string(9) "my string" | ||
TypeHelper::toStrict('') // string(0) "" | ||
); | ||
``` |
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,51 @@ | ||
<?php | ||
|
||
namespace darkfriend\helpers; | ||
|
||
/** | ||
* PHP type helper package | ||
* @package darkfriend\helpers | ||
* @version 1.0.0 | ||
* @author darkfriend <hi@darkfriend.ru> | ||
*/ | ||
class TypeHelper | ||
{ | ||
/** | ||
* Return value to strict type | ||
* @param mixed $value | ||
* @return bool|int|string|null | ||
*/ | ||
public static function toStrict($value) | ||
{ | ||
if ($value === null) { | ||
return null; | ||
} | ||
|
||
if (\is_numeric($value)) { | ||
$num = \filter_var($value, \FILTER_VALIDATE_INT | \FILTER_VALIDATE_FLOAT); | ||
if ($num === false) { | ||
return (string)$value; | ||
} | ||
|
||
if ((int)$num == (float)$num) { | ||
return (int)$num; | ||
} | ||
|
||
return $num; | ||
} | ||
|
||
if ((bool)$value == $value) { | ||
switch ($value) { | ||
case 'true': | ||
$value = true; | ||
break; | ||
case 'false': | ||
$value = false; | ||
break; | ||
} | ||
return $value; | ||
} | ||
|
||
return (string)$value; | ||
} | ||
} |
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,21 @@ | ||
{ | ||
"name": "darkfriend/php7-type", | ||
"type": "library", | ||
"description": "PHP7-helper for type data", | ||
"keywords": ["dev","types","darkfriend","development","php7","php"], | ||
"homepage": "https://github.com/darkfriend/php7-helpers", | ||
"authors": [ | ||
{ | ||
"name": "darkfriend", | ||
"email": "hi@darkfriend.ru" | ||
} | ||
], | ||
"require": { | ||
"php":">=7.0" | ||
}, | ||
"autoload":{ | ||
"classmap":[ | ||
"./TypeHelper.php" | ||
] | ||
} | ||
} |