Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Commit

Permalink
Allow bypassing of constructor validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Jul 4, 2014
1 parent 6d43c2b commit 2d0e0ba
Show file tree
Hide file tree
Showing 15 changed files with 206 additions and 87 deletions.
50 changes: 42 additions & 8 deletions src/AbstractPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,33 @@ abstract class AbstractPath implements PathInterface
const SELF_ATOM = '.';

/**
* Construct a new path instance.
* Construct a new path instance (internal use only).
*
* @internal This method is not intended for public use.
*
* @param mixed<string> $atoms The path atoms.
* @param boolean|null $hasTrailingSeparator True if this path has a trailing separator.
*
* @throws Exception\InvalidPathAtomExceptionInterface If any of the supplied path atoms are invalid.
*/
public function __construct($atoms, $hasTrailingSeparator = null)
public static function construct($atoms, $hasTrailingSeparator = null)
{
if (null === $hasTrailingSeparator) {
$hasTrailingSeparator = false;
}
return new static(static::normalizeAtoms($atoms), $hasTrailingSeparator);
}

$this->atoms = $this->normalizeAtoms($atoms);
$this->hasTrailingSeparator = $hasTrailingSeparator === true;
/**
* Construct a new path instance without validation (internal use only).
*
* @internal This method is not intended for public use.
*
* @param mixed<string> $atoms The path atoms.
* @param boolean|null $hasTrailingSeparator True if this path has a trailing separator.
*
* @throws Exception\InvalidPathAtomExceptionInterface If any of the supplied path atoms are invalid.
*/
public static function constructUnsafe($atoms, $hasTrailingSeparator = null)
{
return new static($atoms, $hasTrailingSeparator);
}

// Implementation of PathInterface =========================================
Expand Down Expand Up @@ -976,7 +988,7 @@ public function normalize()
* @throws Exception\EmptyPathAtomException If any path atom is empty.
* @throws Exception\PathAtomContainsSeparatorException If any path atom contains a separator.
*/
protected function normalizeAtoms($atoms)
protected static function normalizeAtoms($atoms)
{
foreach ($atoms as $atom) {
if ('' === $atom) {
Expand All @@ -989,6 +1001,28 @@ protected function normalizeAtoms($atoms)
return $atoms;
}

/**
* Construct a new path instance (internal use only).
*
* @internal This method is not intended for public use.
*
* @param mixed<string> $atoms The path atoms.
* @param boolean|null $hasTrailingSeparator True if this path has a trailing separator.
*
* @throws Exception\InvalidPathAtomExceptionInterface If any of the supplied path atoms are invalid.
*/
protected function __construct($atoms, $hasTrailingSeparator = null)
{
if (null === $hasTrailingSeparator) {
$hasTrailingSeparator = false;
} else {
$hasTrailingSeparator = $hasTrailingSeparator === true;
}

$this->atoms = $atoms;
$this->hasTrailingSeparator = $hasTrailingSeparator;
}

/**
* Creates a new path instance of the most appropriate type.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Factory/PathFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ public function createFromAtoms(
}

if ($isAbsolute) {
return new AbsolutePath($atoms, $hasTrailingSeparator);
return AbsolutePath::construct($atoms, $hasTrailingSeparator);
}

return new RelativePath($atoms, $hasTrailingSeparator);
return RelativePath::construct($atoms, $hasTrailingSeparator);
}

private static $instance;
Expand Down
2 changes: 1 addition & 1 deletion src/RelativePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function resolveAgainst(AbsolutePathInterface $basePath)
* @throws Exception\EmptyPathAtomException If any path atom is empty.
* @throws Exception\PathAtomContainsSeparatorException If any path atom contains a separator.
*/
protected function normalizeAtoms($atoms)
protected static function normalizeAtoms($atoms)
{
$atoms = parent::normalizeAtoms($atoms);
if (count($atoms) < 1) {
Expand Down
4 changes: 2 additions & 2 deletions src/Unix/Factory/UnixPathFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public function createFromAtoms(
}

if ($isAbsolute) {
return new AbsoluteUnixPath($atoms, $hasTrailingSeparator);
return AbsoluteUnixPath::construct($atoms, $hasTrailingSeparator);
}

return new RelativeUnixPath($atoms, $hasTrailingSeparator);
return RelativeUnixPath::construct($atoms, $hasTrailingSeparator);
}

private static $instance;
Expand Down
63 changes: 55 additions & 8 deletions src/Windows/AbsoluteWindowsPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public static function fromDriveAndAtoms(
}

/**
* Construct a new path instance.
* Construct a new absolute Windows path instance (internal use only).
*
* @internal This method is not intended for public use.
*
* @param string $drive The drive specifier, or null if the path has no drive specifier.
* @param mixed<string> $atoms The path atoms.
Expand All @@ -68,13 +70,39 @@ public static function fromDriveAndAtoms(
* @throws InvalidDriveSpecifierException If the drive specifier is invalid.
* @throws InvalidPathAtomExceptionInterface If any of the supplied path atoms are invalid.
*/
public function __construct($drive, $atoms, $hasTrailingSeparator = null)
{
$this->validateDriveSpecifier($drive);
public static function constructWindowsPath(
$drive,
$atoms,
$hasTrailingSeparator = null
) {
static::validateDriveSpecifier($drive);

parent::__construct($atoms, $hasTrailingSeparator);
return new static(
$drive,
static::normalizeAtoms($atoms),
$hasTrailingSeparator
);
}

$this->drive = $drive;
/**
* Construct a new absolute Windows path instance without validation
* (internal use only).
*
* @internal This method is not intended for public use.
*
* @param string $drive The drive specifier, or null if the path has no drive specifier.
* @param mixed<string> $atoms The path atoms.
* @param boolean|null $hasTrailingSeparator True if this path has a trailing separator.
*
* @throws InvalidDriveSpecifierException If the drive specifier is invalid.
* @throws InvalidPathAtomExceptionInterface If any of the supplied path atoms are invalid.
*/
public static function constructWindowsPathUnsafe(
$drive,
$atoms,
$hasTrailingSeparator = null
) {
return new static($drive, $atoms, $hasTrailingSeparator);
}

// Implementation of WindowsPathInterface ==================================
Expand Down Expand Up @@ -294,7 +322,7 @@ public function toRelative()
* @throws EmptyPathAtomException If any path atom is empty.
* @throws PathAtomContainsSeparatorException If any path atom contains a separator.
*/
protected function normalizeAtoms($atoms)
protected static function normalizeAtoms($atoms)
{
foreach ($atoms as $atom) {
if ('' === $atom) {
Expand All @@ -319,13 +347,32 @@ protected function normalizeAtoms($atoms)
*
* @throws InvalidDriveSpecifierException If the drive specifier is invalid.
*/
protected function validateDriveSpecifier($drive)
protected static function validateDriveSpecifier($drive)
{
if (!preg_match('{^[a-zA-Z]$}', $drive)) {
throw new InvalidDriveSpecifierException($drive);
}
}

/**
* Construct a new absolute Windows path instance (internal use only).
*
* @internal This method is not intended for public use.
*
* @param string $drive The drive specifier, or null if the path has no drive specifier.
* @param mixed<string> $atoms The path atoms.
* @param boolean|null $hasTrailingSeparator True if this path has a trailing separator.
*
* @throws InvalidDriveSpecifierException If the drive specifier is invalid.
* @throws InvalidPathAtomExceptionInterface If any of the supplied path atoms are invalid.
*/
protected function __construct($drive, $atoms, $hasTrailingSeparator = null)
{
parent::__construct($atoms, $hasTrailingSeparator);

$this->drive = $drive;
}

/**
* Get the normalized form of the supplied drive specifier.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Windows/Factory/WindowsPathFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ public function createFromDriveAndAtoms(
}

if ($isAbsolute) {
return new AbsoluteWindowsPath(
return AbsoluteWindowsPath::constructWindowsPath(
$drive,
$atoms,
$hasTrailingSeparator
);
}

return new RelativeWindowsPath(
return RelativeWindowsPath::constructWindowsPath(
$atoms,
$drive,
$isAnchored,
Expand Down
80 changes: 64 additions & 16 deletions src/Windows/RelativeWindowsPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public static function fromDriveAndAtoms(
}

/**
* Construct a new relative Windows path instance.
* Construct a new relative Windows path instance (internal use only).
*
* @internal This method is not intended for public use.
*
* @param mixed<string> $atoms The path atoms.
* @param string|null $drive The drive specifier.
Expand All @@ -73,31 +75,48 @@ public static function fromDriveAndAtoms(
* @throws EmptyPathException If the path atoms are empty, and the path is not anchored.
* @throws InvalidPathAtomExceptionInterface If any of the supplied path atoms are invalid.
*/
public function __construct(
public static function constructWindowsPath(
$atoms,
$drive = null,
$isAnchored = null,
$hasTrailingSeparator = null
) {
if (null === $isAnchored) {
$isAnchored = false;
}
if (null === $hasTrailingSeparator) {
$hasTrailingSeparator = false;
}

if (!$isAnchored && count($atoms) < 1) {
throw new EmptyPathException;
}

if (null !== $drive) {
$this->validateDriveSpecifier($drive);
static::validateDriveSpecifier($drive);
}

parent::__construct($atoms, $hasTrailingSeparator);
return new static(
static::normalizeAtoms($atoms),
$drive,
$isAnchored,
$hasTrailingSeparator
);
}

$this->drive = $drive;
$this->isAnchored = $isAnchored;
/**
* Construct a new relative Windows path instance without validation
* (internal use only).
*
* @internal This method is not intended for public use.
*
* @param mixed<string> $atoms The path atoms.
* @param string|null $drive The drive specifier.
* @param boolean|null $isAnchored True if this path is anchored to the drive root.
* @param boolean|null $hasTrailingSeparator True if this path has a trailing separator.
*
* @throws EmptyPathException If the path atoms are empty, and the path is not anchored.
* @throws InvalidPathAtomExceptionInterface If any of the supplied path atoms are invalid.
*/
public static function constructWindowsPathUnsafe(
$atoms,
$drive = null,
$isAnchored = null,
$hasTrailingSeparator = null
) {
return new static($atoms, $drive, $isAnchored, $hasTrailingSeparator);
}

// Implementation of WindowsPathInterface ==================================
Expand Down Expand Up @@ -306,7 +325,7 @@ public function toAbsolute()
* @throws EmptyPathAtomException If any path atom is empty.
* @throws PathAtomContainsSeparatorException If any path atom contains a separator.
*/
protected function normalizeAtoms($atoms)
protected static function normalizeAtoms($atoms)
{
foreach ($atoms as $atom) {
if ('' === $atom) {
Expand All @@ -331,13 +350,42 @@ protected function normalizeAtoms($atoms)
*
* @throws InvalidDriveSpecifierException If the drive specifier is invalid.
*/
protected function validateDriveSpecifier($drive)
protected static function validateDriveSpecifier($drive)
{
if (!preg_match('{^[a-zA-Z]$}', $drive)) {
throw new InvalidDriveSpecifierException($drive);
}
}

/**
* Construct a new relative Windows path instance (internal use only).
*
* @internal This method is not intended for public use.
*
* @param mixed<string> $atoms The path atoms.
* @param string|null $drive The drive specifier.
* @param boolean|null $isAnchored True if this path is anchored to the drive root.
* @param boolean|null $hasTrailingSeparator True if this path has a trailing separator.
*
* @throws EmptyPathException If the path atoms are empty, and the path is not anchored.
* @throws InvalidPathAtomExceptionInterface If any of the supplied path atoms are invalid.
*/
protected function __construct(
$atoms,
$drive = null,
$isAnchored = null,
$hasTrailingSeparator = null
) {
if (null === $isAnchored) {
$isAnchored = false;
}

parent::__construct($atoms, $hasTrailingSeparator);

$this->drive = $drive;
$this->isAnchored = $isAnchored;
}

/**
* Get the normalized form of the supplied drive specifier.
*
Expand Down
6 changes: 3 additions & 3 deletions test/suite/AbsolutePathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function testConstructor($pathString, array $atoms, $hasTrailingSeparator

public function testConstructorDefaults()
{
$this->path = new AbsolutePath(array());
$this->path = AbsolutePath::construct(array());

$this->assertFalse($this->path->hasTrailingSeparator());
}
Expand All @@ -69,15 +69,15 @@ public function testConstructorFailureAtomContainingSeparator()
'Eloquent\Pathogen\Exception\PathAtomContainsSeparatorException',
"Invalid path atom 'foo/bar'. Path atoms must not contain separators."
);
new AbsolutePath(array('foo/bar'));
AbsolutePath::construct(array('foo/bar'));
}

public function testConstructorFailureEmptyAtom()
{
$this->setExpectedException(
'Eloquent\Pathogen\Exception\EmptyPathAtomException'
);
new AbsolutePath(array(''));
AbsolutePath::construct(array(''));
}

public function testAtomAt()
Expand Down
2 changes: 1 addition & 1 deletion test/suite/Exception/NonAbsolutePathExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NonAbsolutePathExceptionTest extends PHPUnit_Framework_TestCase
{
public function testException()
{
$path = new RelativePath(array('foo', 'bar'));
$path = RelativePath::construct(array('foo', 'bar'));
$previous = new Exception;
$exception = new NonAbsolutePathException($path, $previous);

Expand Down
Loading

0 comments on commit 2d0e0ba

Please sign in to comment.