/** * Convert the object to an instance of ``IntString``. * * @param string|IntString $obj Object to convert to ``IntString``. * * @return IntString * @throws InvalidArgumentException if object is not a string or format is invalid. */ public static function parse($obj) { if ($obj instanceof IntString) { return $obj; } if (is_integer($obj)) { return new VersionComponent($obj); } try { $intValue = (int) String::ensureIsString($obj); } catch (InvalidArgumentException $e) { $args = ['position' => '1st', 'expected' => 'string" or "integer', 'actual' => typeof($obj)]; $msg = nml_msg('Invalid argument type.'); $msg .= nml_msg(' {position} parameter must to be an instance of "{expected}"; "{actual}" given.', $args); throw new InvalidArgumentException($msg, 1, $e); } $stringValue = ltrim($obj, "{$intValue}"); // Validate that 0 (zero) is not interpreted as '' (empty string) if ($stringValue === $obj) { $msg = nml_msg('Invalid argument value.'); $msg .= nml_msg(' "{0}" (string) must to start with an integer.', $obj); throw new InvalidArgumentException($msg); } return new IntString($intValue, $stringValue); }
/** * Gets the property getter method name. * You can customize the getter prefix by implementing ``ICustomPrefixedPropertiesContainer`` interface. * * @param string $name Property name. * * @return string * @throws InvalidArgumentException If property is not valid or has not getter. * @throws BadMethodCallException If custom prefix is not an ``string`` instance. * @see ICustomPrefixedPropertiesContainer::getCustomGetterPrefix() */ protected static function getPropertyGetter($name) { $args = ['class' => get_called_class()]; $prefix = 'get'; $args['name'] = static::ensurePropertyExists($name, $args['class']); try { $getter = static::ensureMethodExists($prefix . $args['name']); } catch (InvalidArgumentException $error) { $msg = nml_msg('"{name}" property has not a getter method in "{class}".', $args); if (is_subclass_of($args['class'], ICustomPrefixedPropertiesContainer::class)) { // If not available standard getter, check if custom available try { $prefix = String::ensureIsString(static::getCustomGetterPrefix()); } catch (InvalidArgumentException $e) { $msg = nml_msg('"{class}::getCustomGetterPrefix" method must to return an string.', $args['class']); throw new BadMethodCallException($msg, 31, $e); } try { $getter = static::ensureMethodExists($prefix . $args['name']); } catch (InvalidArgumentException $e) { throw new InvalidArgumentException($msg, 32, $e); } } else { // Error for non custom prefixes throw new InvalidArgumentException($msg, 30, $error); } } return $getter; }