/** * @param $value * @param bool $strict = true allows only explicit values, not null and empty string * @param bool $paranoid = false raises an exception if some value is lost on cast due to rounding on cast * @return float|int * @throws \Granam\Number\Tools\Exceptions\WrongParameterType */ public static function toNumber($value, $strict = true, $paranoid = false) { if (is_float($value)) { return $value; } if (is_int($value)) { return $value; } if (is_bool($value)) { // true = 1; false = 0; return (int) $value; } try { $stringValue = trim(ToString::toString($value, $strict)); } catch (\Granam\Scalar\Tools\Exceptions\WrongParameterType $exception) { throw new Exceptions\WrongParameterType($exception->getMessage(), $exception->getCode(), $exception); } if ($strict && !is_numeric($stringValue)) { // no number at all (casted null, empty string, non-digits...) throw new Exceptions\WrongParameterType('Only numbers and booleans are allowed in strict mode, got ' . ValueDescriber::describe($value) . ' expressed as string ' . ValueDescriber::describe($stringValue)); } $floatValue = (double) $stringValue; // note: '' = 0.0 if ($paranoid) { self::checkIfNoValueHasBeenLostByCast($floatValue, $stringValue); } $intValue = (int) $floatValue; if ((double) $intValue === $floatValue) { return $intValue; } return $floatValue; }
/** * @param bool|float|int|null|object|string $value * @param bool $strict = true NULL raises an exception * @throws \Granam\String\Exceptions\WrongParameterType */ public function __construct($value, $strict = true) { try { parent::__construct(ToString::toString($value, $strict)); } catch (ToString_WrongParameterType $exception) { // wrapping by a local one throw new Exceptions\WrongParameterType($exception->getMessage(), $exception->getCode(), $exception); } }
public static function toConstant($value) { $value = ToString::toString($value); $trimmed = trim($value); $specialsReplaced = self::replaceSpecials($trimmed); $nonCharactersReplaced = preg_replace('~\\W+~u', '_', $specialsReplaced); $originalLocale = setlocale(LC_CTYPE, 0); setlocale(LC_CTYPE, 'C.UTF-8'); $withoutDiacritics = iconv('UTF-8', 'ASCII//TRANSLIT', $nonCharactersReplaced); setlocale(LC_CTYPE, $originalLocale); $underscored = preg_replace('~[^a-zA-Z0-9]+~', '_', $withoutDiacritics); return strtolower($underscored); }
public function getFate($fateCode) { switch (ToString::toString($fateCode)) { case FateOfGoodRear::FATE_OF_GOOD_REAR: return $this->getFateOfGoodRear(); case FateOfCombination::FATE_OF_COMBINATION: return $this->getFateOfCombination(); case FateOfExceptionalProperties::FATE_OF_EXCEPTIONAL_PROPERTIES: return $this->getFateOfExceptionalProperties(); default: throw new Exceptions\UnknownExceptionalityFate('Unknown exceptionality fate code ' . ValueDescriber::describe($fateCode)); } }
/** * @param $propertyValue * @param string|StringInterface $propertyCode * @return Agility|Charisma|Intelligence|Knack|Strength|Will * @throws \DrdPlus\Properties\Base\Exceptions\UnknownBasePropertyCode * @throws \Granam\Scalar\Tools\Exceptions\WrongParameterType */ public function createProperty($propertyValue, $propertyCode) { switch (ToString::toString($propertyCode)) { case Strength::STRENGTH: return $this->createStrength($propertyValue); case Agility::AGILITY: return $this->createAgility($propertyValue); case Knack::KNACK: return $this->createKnack($propertyValue); case Will::WILL: return $this->createWill($propertyValue); case Intelligence::INTELLIGENCE: return $this->createIntelligence($propertyValue); case Charisma::CHARISMA: return $this->createCharisma($propertyValue); default: throw new Exceptions\UnknownBasePropertyCode('Unknown code of a base property ' . ValueDescriber::describe($propertyCode)); } }
/** * @param array $row * @param string|int|float|ScalarInterface $columnIndex * @return int|float|string|bool * @throws \DrdPlus\Tables\Partials\Exceptions\RequiredColumnNotFound * @throws \Granam\Scalar\Tools\Exceptions\WrongParameterType */ private function getValueInRow(array $row, $columnIndex) { $stringColumnIndex = ToString::toString($columnIndex); if (!array_key_exists($stringColumnIndex, $row)) { throw new Exceptions\RequiredColumnNotFound('Column of name ' . ValueDescriber::describe($columnIndex) . ' does not exist'); } return $row[$stringColumnIndex]; }
public function getProperty($propertyCode) { switch (ToString::toString($propertyCode)) { case Strength::STRENGTH: return $this->getStrength(); case Agility::AGILITY: return $this->getAgility(); case Knack::KNACK: return $this->getKnack(); case Will::WILL: return $this->getWill(); case Intelligence::INTELLIGENCE: return $this->getIntelligence(); case Charisma::CHARISMA: return $this->getCharisma(); default: throw new Exceptions\UnknownBasePropertyCode($propertyCode); } }