Last active
May 13, 2021 11:25
-
-
Save koohq/f383e692f47f67dabdaff9ca9fb4c4c0 to your computer and use it in GitHub Desktop.
The PHP array wrapper to get scalar.
This file contains hidden or 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
<?php | |
/** | |
* Provides the methods to get scalar from single-dimensional-array. | |
* This code is designed for the legacy version, such as PHP 5.2. | |
* | |
* @copyright 2021 koohq | |
* @license https://creativecommons.org/publicdomain/zero/1.0/legalcode CC0 | |
*/ | |
class ReadOnlyScalarMap | |
{ | |
/** @var array source */ | |
private $source; | |
/** | |
* Initializes a new instance of ReadOnlyScalarMap using the specified array. | |
* | |
* @param array $source the source array. | |
*/ | |
public function __construct(array $source) | |
{ | |
$this->source = $source; | |
} | |
/** | |
* Gets the value associated with the specified key and converts to the boolean value. | |
* | |
* @param string|int $key The key of the value to get. | |
* @return bool the boolean value associated with the specified key if the key is found; otherwise, false. | |
*/ | |
public function getBool($key) | |
{ | |
// `boolval()` is undefined on PHP 5.2. | |
$value = $this->getOrDefault($key); | |
return !empty($value); | |
} | |
/** | |
* Gets the value associated with the specified key and converts to the float value. | |
* | |
* @param string|int $key The key of the value to get. | |
* @return float the float value associated with the specified key if the key is found; otherwise, 0. | |
*/ | |
public function getFloat($key) | |
{ | |
return floatval($this->getOrDefault($key)); | |
} | |
/** | |
* Gets the value associated with the specified key and converts to the integer value. | |
* | |
* @param string|int The key of the value to get. | |
* @return int the integer value associated with the specified key if the key is found; otherwise, 0. | |
*/ | |
public function getInt($key) | |
{ | |
return intval($this->getOrDefault($key)); | |
} | |
/** | |
* Gets the value associated with the specified key and converts to the string value. | |
* | |
* @param string|int $key The key of the value to get. | |
* @return string the boolean value associated with the specified key if the key is found; otherwise, empty string. | |
*/ | |
public function getString($key) | |
{ | |
return strval($this->getOrDefault($key)); | |
} | |
/** | |
* Gets the value associated with the specified key. | |
* | |
* @param string|int $key The key of the value to get. | |
* @return mixed the value associated with the specified key if the key is found; otherwise, null. | |
*/ | |
private function getOrDefault($key) | |
{ | |
return isset($this->source[$key]) ? $this->source[$key] : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment