-
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.
- Coerce values that can sensibly be coerced - Convert the functions into a trait so they can be shared between the three select classes - Adjust tests accordingly
- Loading branch information
Showing
7 changed files
with
194 additions
and
105 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
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,136 @@ | ||
<?php | ||
|
||
namespace Squirrel\Entities\Action; | ||
|
||
use Squirrel\Debug\Debug; | ||
use Squirrel\Queries\Exception\DBInvalidOptionException; | ||
|
||
trait FlattenedFieldsWithTypeTrait | ||
{ | ||
/** | ||
* @return int[] | ||
*/ | ||
public function getFlattenedIntegerFields(): array | ||
{ | ||
$values = $this->getFlattenedFields(); | ||
|
||
foreach ($values as $key => $value) { | ||
// Convert non-int values which do not change when type casted | ||
if ( | ||
!\is_integer($value) | ||
&& \strval(\intval($value)) === \strval($value) | ||
) { | ||
$values[$key] = \intval($value); | ||
continue; | ||
} | ||
|
||
if (!\is_int($value)) { | ||
throw Debug::createException( | ||
DBInvalidOptionException::class, | ||
[ActionInterface::class], | ||
'Flattened integers requested, but not all values were integers' | ||
); | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
/** | ||
* @return float[] | ||
*/ | ||
public function getFlattenedFloatFields(): array | ||
{ | ||
$values = $this->getFlattenedFields(); | ||
|
||
foreach ($values as $key => $value) { | ||
if (\is_int($value)) { | ||
$values[$key] = \floatval($value); | ||
continue; | ||
} | ||
|
||
// Convert non-float values which do not change when type casted | ||
if ( | ||
!\is_float($value) | ||
&& \strval(\floatval($value)) === \strval($value) | ||
) { | ||
$values[$key] = \floatval($value); | ||
continue; | ||
} | ||
|
||
if (!\is_float($value)) { | ||
throw Debug::createException( | ||
DBInvalidOptionException::class, | ||
[ActionInterface::class], | ||
'Flattened floats requested, but not all values were floats' | ||
); | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getFlattenedStringFields(): array | ||
{ | ||
$values = $this->getFlattenedFields(); | ||
|
||
foreach ($values as $key => $value) { | ||
// Integers and floats can be converted to strings without problems | ||
if ( | ||
\is_int($value) | ||
|| \is_float($value) | ||
) { | ||
$values[$key] = \strval($value); | ||
continue; | ||
} | ||
|
||
if (!\is_string($value)) { | ||
throw Debug::createException( | ||
DBInvalidOptionException::class, | ||
[ActionInterface::class], | ||
'Flattened strings requested, but not all values were strings' | ||
); | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
|
||
/** | ||
* @return bool[] | ||
*/ | ||
public function getFlattenedBooleanFields(): array | ||
{ | ||
$values = $this->getFlattenedFields(); | ||
|
||
foreach ($values as $key => $value) { | ||
// Convert non-boolean values which can reasonably be converted to boolean | ||
if ( | ||
$value === 0 | ||
|| $value === '0' | ||
) { | ||
$values[$key] = false; | ||
continue; | ||
} elseif ( | ||
$value === 1 | ||
|| $value === '1' | ||
) { | ||
$values[$key] = true; | ||
continue; | ||
} | ||
|
||
if (!\is_bool($value)) { | ||
throw Debug::createException( | ||
DBInvalidOptionException::class, | ||
[ActionInterface::class], | ||
'Flattened booleans requested, but not all values were booleans' | ||
); | ||
} | ||
} | ||
|
||
return $values; | ||
} | ||
} |
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
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
Oops, something went wrong.