/** * See if paths match satisfactorily * @param string $expectedPath an absolute path to target file * @param string $actualPath the absolute or relative path that was found * @param string $currDir the current directory to calculate relative paths * from. If $actualPath is an explicitly relative path (i.e. "../foo"), you * <b>must</b> provide this! * @return string|bool false if the paths do not match. If they do match, * returns the base, if any, that is not explicitly defined by $actualPath. * Since this method may return the empty string on success, strict type * comparison must be used. */ public static function matchPaths($expectedPath, $actualPath, $currDir = false) { if (Scisr_File::isExplicitlyRelative($actualPath)) { if ($currDir === false) { throw new LogicException('You provided a relative path without a current dir!'); } $actualPath = Scisr_File::getAbsolutePath($actualPath, $currDir); // Add a trailing slash if it's not there if (substr($currDir, -1) != '/') { $currDir .= '/'; } return $expectedPath == $actualPath ? $currDir : false; } // If it's an absolute path, it must match exactly if ($actualPath[0] == '/') { return $expectedPath == $actualPath ? '' : false; } // A simple test: see if the actual matches the end of the expected path if (strstr($expectedPath, $actualPath) == $actualPath) { $base = substr($expectedPath, 0, strpos($expectedPath, $actualPath)); return $base; } return false; }
/** * @dataProvider isRelativeProvider */ public function testIsRelative($path, $isRelative) { $this->assertSame($isRelative, Scisr_File::isExplicitlyRelative($path)); }