PHP code example of adrenalinkin / enum-mapper
1. Go to this page and download the library: Download adrenalinkin/enum-mapper library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
adrenalinkin / enum-mapper example snippets
use Linkin\Component\EnumMapper\Mapper\AbstractEnumMapper;
class GenderMapper extends AbstractEnumMapper
{
const DB_UNDEFINED = 0;
const DB_MALE = 10;
const DB_FEMALE = 20;
const HUMAN_UNDEFINED = 'Undefined';
const HUMAN_MALE = 'Male';
const HUMAN_FEMALE = 'Female';
}
$mapper = new GenderMapper();
$dbGenderValue = GenderMapper::DB_MALE; // 10
$humanValue = $mapper->fromDbToHuman($dbGenderValue);
$mapper = new GenderMapper();
$humanGenderValue = GenderMapper::HUMAN_FEMALE; // Female
$dbValue = $mapper->fromHumanToDb($humanGenderValue);
$mapper = new GenderMapper();
$map = $mapper->getMap(); // [0 => 'Undefined', 10 => 'Male', 20 => 'Female']
if (GenderMapper::DB_UNDEFINED === $maleFromForm) {
throw new \Exception('Field "Gender" is
$mapper = new GenderMapper();
$allowedDb = $mapper->getAllowedDbValues(); // [0, 10, 20]
$allowedHuman = $mapper->getAllowedHumanValues(); // ['Undefined', 'Male', 'Female']
// Exclude values from result
$allowedDb = $mapper->getAllowedDbValues([GenderMapper::DB_UNDEFINED]); // [10, 20]
$allowedHuman = $mapper->getAllowedHumanValues([GenderMapper::HUMAN_UNDEFINED]); // ['Male', 'Female']
$mapper = new GenderMapper();
$randomDb = $mapper->getRandomDbValue(); // 0 || 10 || 20
$randomHuman = $mapper->getRandomHumanValue(); // Undefined || Male || Female
// Exclude values from result
$randomDb = $mapper->getRandomDbValue([GenderMapper::DB_UNDEFINED]); // 10 || 20
$randomHuman = $mapper->getRandomHumanValue([GenderMapper::HUMAN_UNDEFINED]); // Male || Female