Beispiel #1
0
 /**
  * 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);
 }
Beispiel #2
0
 public function equals($other)
 {
     if (defined('CODE_ANALYSIS')) {
         if ($this instanceof IEquatable) {
             $type = typeof($this);
             $args = ['access' => 'public', 'base_class' => __CLASS__, 'class' => $type->Name, 'function' => __FUNCTION__];
             $msg = nml_msg('You implemented IEquatable, but using default "{base_class}::{function}" ({access}) method.', $args);
             $msg .= nml_msg(' You can replace (override) its behavior by creating "{class}::{function}" ({access}) method.', $args);
             trigger_error($msg, E_USER_NOTICE);
         }
     }
     return $this == $other;
 }
 /**
  *
  *
  * @param int|null    $intValue
  * @param string|null $stringValue
  */
 public function __construct($intValue = null, $stringValue = null)
 {
     // Validates filters for only null or int/string value types.
     parent::__construct($intValue, $stringValue);
     if ($intValue === null) {
         $this->intValue = $intValue;
         // Ignore string value if intValue is null.
         $this->stringValue = '';
     } else {
         // Validation of values
         if ($this->IntValue < 0) {
             $args = ['position' => '1st', 'actual' => $intValue];
             $msg = nml_msg('Invalid argument value.');
             $msg .= nml_msg(' {position} argument must to be a positive number; "{actual}" given.', $args);
             throw new InvalidArgumentException($msg);
         }
         // Integer is valid
         if ($stringValue !== null) {
             if ($this->StringValue != '') {
                 $pattern = '~^([a-z])$~';
                 // 1 char
                 if (strlen($this->StringValue) > 1) {
                     $start = '~^([a-z]|-)';
                     $middle = '([a-z]|[0-9]|-)*';
                     $end = '([a-z]|[0-9])$~';
                     $pattern = $start . $middle . $end;
                 }
                 $correct = (bool) preg_match($pattern, $this->StringValue);
                 if ($correct) {
                     //Último chequeo: que no hayan 2 '-' consecutivos.
                     $correct = strpos($this->StringValue, '--') == false ? true : false;
                 }
                 if (!$correct) {
                     $args = ['position' => '2nd', 'actual' => $stringValue];
                     $msg = nml_msg('Invalid argument value.');
                     $msg .= nml_msg(' {position} parameter has invalid chars; "{actual}" given.', $args);
                     throw new InvalidArgumentException($msg);
                 }
             }
         }
         // String is valid
     }
 }
 /**
  * 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;
 }
Beispiel #5
0
 /**
  * Ensures that an string follows the PHP variables naming convention.
  *
  * @param string $string String to be ensured.
  *
  * @return string
  * @throws InvalidArgumentException if object is not an `string` or do not
  *   follows the PHP variables naming convention.
  */
 public static function ensureIsValidVarName($string)
 {
     $pattern = '/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*/';
     if (!preg_match($pattern, static::ensureIsString($string))) {
         $msg = nml_msg('Provided string do not follows PHP variables naming convention: "{0}".', $string);
         throw new InvalidArgumentException($msg);
     }
     return $string;
 }
Beispiel #6
0
 /**
  * Convierte una cadena a su representación del tipo Version.
  *
  * @param Version|string|int|float|array $value Objeto a convertir.
  *
  * @return Version Objeto convertido desde $value.
  * */
 public static function parse($value)
 {
     if ($value instanceof Version) {
         return $value;
     }
     $version = [];
     // Try to convert into an array
     if (is_integer($value)) {
         // Integer for major value
         $version = [$value, 0];
     } elseif (is_float($value)) {
         // Integer part as major, and decimal part as minor
         $version = sprintf("%F", $value);
         $version = explode('.', $version);
     } elseif (is_array($value)) {
         // Implode first 4 places for major, minor, build and revision respectivally.
         $version = array_slice($value, 0, 4);
     } elseif (is_string($value)) {
         $version = explode('.', $value);
     } else {
         $msg = nml_msg('Unable to parse. Argument passed has an invalid type: "{0}".', typeof($value));
         throw new InvalidArgumentException($msg);
     }
     // $value ya debería ser un array.
     $c = count($version);
     if ($c > 4 || $c < 2) {
         $msg = nml_msg('Unable to parse. Argument passed has an invalid format: "{0}".', $value);
         //var_dump($version);
         throw new InvalidArgumentException($msg);
     }
     $major = (int) $version[0];
     $minor = (int) $version[1];
     $build = null;
     $revision = null;
     if (count($version) >= 3) {
         $build = VersionComponent::Parse($version[2]);
         if (count($version) == 4) {
             $revision = VersionComponent::Parse($version[3]);
         }
     }
     return new Version($major, $minor, $build, $revision);
 }
Beispiel #7
0
 /**
  * Obtiene la ruta del directorio de la versión especificada. Si no se
  * especifica, se devuelve la versión más reciente.
  *
  * @param string|Version $version Versión a obtener. También puede
  *   tomar los valores 'newest' u 'oldest' para representar a la versión
  *   más nueva o más vieja, respectivamente.
  *
  * @return string Ruta del directorio de la versión especificada.
  * */
 public function getDirectoryPath($version = self::NEWEST)
 {
     $c = count($this->Versions);
     if ($c == 0) {
         throw new LogicException(nml_msg('This Asset has not versions.'));
     }
     $v = $version;
     if ($version == self::OLDEST or $version == self::NEWEST) {
         $v = $this->Versions[0];
         if ($c > 1) {
             usort($this->versions, array("NelsonMartell\\Version", "Compare"));
             $v = $this->Versions[0];
             if ($version == self::NEWEST) {
                 $v = $this->Versions[$c - 1];
             }
         }
     } else {
         try {
             $v = Version::parse($version);
         } catch (InvalidArgumentException $e) {
             $args = ['name' => 'version', 'pos' => 0, 'expected' => typeof(new Version(1, 0))->Name, 'actual' => typeof($version)];
             $msg = nml_msg('Invalid argument type.');
             $msg .= nml_msg(' "{name}" (position {pos}) must to be an instance of "{expected}" (or compatible);' . ' "{actual}" given.', $args);
             throw new InvalidArgumentException($msg);
         }
         if (array_search($v, $this->Versions) === false) {
             $msg = nml_msg('Invalid argument value.');
             $msg .= nml_msg(' Asset has not the version "{version}".', ['version' => $v]);
             throw new InvalidArgumentException($msg);
         }
     }
     return sprintf('%s%s/', $this->RootDirectory, $v);
 }