This repository has been archived by the owner on Mar 20, 2022. It is now read-only.
-
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
Dusan Malusev
committed
Nov 9, 2019
1 parent
f1adfa6
commit 946b8cf
Showing
9 changed files
with
211 additions
and
44 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
|
||
namespace BrosSquad\DotEnv; | ||
|
||
|
||
interface Parser | ||
{ | ||
/** | ||
* @param string $value | ||
* @return mixed | ||
*/ | ||
public function parse(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
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,69 @@ | ||
<?php | ||
|
||
|
||
namespace BrosSquad\DotEnv; | ||
|
||
|
||
class TypeChecker implements ValueType | ||
{ | ||
private $emptyStringAsNull; | ||
|
||
public function __construct(bool $emptyStringAsNull) | ||
{ | ||
$this->emptyStringAsNull = $emptyStringAsNull; | ||
} | ||
|
||
public function detectValue(string $value) | ||
{ | ||
$toLower = strtolower($value); | ||
|
||
// Detecting NULL | ||
if (strcmp('null', $toLower) === 0 || ($this->emptyStringAsNull === true && strcmp('', $toLower) === 0)) { | ||
return null; | ||
} | ||
|
||
// Detecting BOOLEANS | ||
if (($isBoolean = $this->checkForBoolean($toLower)) !== null) { | ||
return (bool)$isBoolean; | ||
} | ||
|
||
// Detecting FLOATS | ||
$trimmed = ltrim($toLower, '0'); | ||
|
||
|
||
|
||
$casted = (float)$toLower; | ||
|
||
if (($casted !== 0.0 || strcmp('.0', $trimmed) === 0) && preg_match('/^\d+\.\d+$/', $toLower) === 1) { | ||
return $casted; | ||
} | ||
|
||
// Detecting INTEGERS | ||
$casted = (int)$trimmed; | ||
|
||
if ($casted !== 0 || strcmp('', $trimmed) === 0) { | ||
return $casted; | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
|
||
private function checkForBoolean(string $value): ?int | ||
{ | ||
switch ($value) { | ||
case 'ok': | ||
// case '1': | ||
case 'true': | ||
case 'yes': | ||
case 'y': | ||
return true; | ||
case 'no': | ||
// case '0': | ||
case 'false': | ||
return false; | ||
default: | ||
return null; | ||
} | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
|
||
namespace BrosSquad\DotEnv; | ||
|
||
|
||
interface ValueType | ||
{ | ||
public const NULL = 0; | ||
public const INTEGER = 1; | ||
public const FLOAT = 2; | ||
public const STRING = 'STRING'; | ||
public const BOOLEAN = 'BOOLEAN'; | ||
|
||
public function detectValue(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,9 @@ | ||
INTEGERS=123 | ||
BOOLEAN=false | ||
BOOLEAN_TRUE=true | ||
NULLABLE=null | ||
UPPERNULL = NULL | ||
STR=SOME_STRING | ||
FLOATS=1.65 | ||
FLOAT_ZERO=0.0 | ||
INT_ZERO=0 |
Oops, something went wrong.