Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ If a value can not be converted you should return the following *falsy* value fo

There are a few special cases:

1. If an `array` is passed as *input* to `convert_to_string()`, it should a string representation of the elements, in order, as a comma separated list. For example, an input of `[1, 2, 3]` should return `1, 2, 3`, an input of `[1]` should return `1`.
1. If an `array` is passed as *input* to `convert_to_string()`, it should be a string representation of the elements, in order, as a comma separated list. For example, an input of `[1, 2, 3]` should return `1, 2, 3`, an input of `[1]` should return `1`.
2. If any of the *falsy* values above or the string `null` is passed as *input* to `convert_to_null()`, it should return `null`. Otherwise, it should return the original *input*.

## Just getting started?
Expand Down
48 changes: 48 additions & 0 deletions src/data_types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

function convert_to_int($input) {
return (int)$input;
}

function convert_to_float($input) {
settype($input, "float");
if(is_float($input)) {
return $input;
} else {
return 0.0;
}
}

function convert_to_string($input) {
if(is_array($input)) {
return implode(", ", $input);
} else {
return (string)$input;
}
}

function convert_to_bool($input) {
settype($input, "bool");
if(is_bool($input)) {
return $input;
} else {
return false;
}
}

function convert_to_array($input) {
settype($input, "array");
if(is_array($input)) {
return $input;
} else {
return [];
}
}

function convert_to_null($input) {
if(!$input || $input === "null") {
return null;
} else {
return $input;
}
}