/** * @testdox Can check if Version instance is valid * @coverage Version::isValid * @dataProvider isValidProvider */ public function testIsValid($expected, Version $version) { $actual = $version->isValid(); $message = String::format('$version->{method}(); // {actual}', ['method' => 'isValid', 'obj' => static::export($version), 'actual' => static::export($actual)]); $this->assertInternalType('boolean', $actual, $message . ' # Should return a boolean #'); if ($expected === true) { $this->assertTrue($actual, $message); } else { $this->assertFalse($actual, $message); } }
public function isValidProvider() { $args = []; foreach ($this->parseableStrings['valid'] as $str) { $args[$str] = [true, Version::parse($str)]; } foreach ($this->parseableStrings['invalid'] as $str) { $args[$str] = [false, Version::parse($str)]; } return $args; }
/** * Determina la posición relativa de esta instancia con respecto al objeto especificado. * * For types different than ``Version``: * - ``integer`` and ``null`` are always < 0; * - ``string`` and ``array`` are parsed and then evaluated (if is not parseable, always > 0); * - other types are always > 0 * * @param Version|int|string|mixed $other * The other object to compare with. * * @return integer|null * Returns: * - ``= 0`` if this instance is considered equivalent to $other; * - ``> 0`` si esta instancia se considera mayor a $other; * - ``< 0`` si esta instancia se considera menor a $other. * - ``null`` if this instance can't be compared against $other . * @see Object::compare() * */ public function compareTo($other) { $r = $this->equals($other) ? 0 : 9999; if (!$other instanceof Version) { switch (typeof($other)->toString()) { case 'integer': case 'float': case 'double': case 'null': case 'NULL': $r = 1; // Siempre es mayor a cualquier número o null break; case 'string': case 'array': // Se tratan de convertir las cadenas y arrays try { $tmp = Version::parse($other); $r = $this->compareTo($tmp); } catch (InvalidArgumentException $e) { // Siempre es mayor a strings o arrays que no se puedan convertir $r = 1; } break; default: // No se puede determinar comparando a otros objetos. $r = null; } return $r; } if ($r != 0) { $r = $this->Major - $other->Major; if ($r == 0) { $r = $this->Minor - $other->Minor; if ($r == 0) { $r = $this->Build->compareTo($other->Build); if ($r == 0) { $r = $this->Revision->compareTo($other->Revision); } } } } return $r; }
/** * 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); }