public static createFromFile ( string $filename ) : |
||
$filename | string | |
return | an instance of XML with the data from the file |
public static function createFromFile($filename)
{
if (!file_exists($filename)) {
throw new Exception(sprintf('Failed to load xml from file %s. The file does not exist', $filename), Exception::BAD_LOADERDATA);
}
//suppress warning with @ operator, since we explicitely check the return value
$xml = @file_get_contents($filename);
if (false === $xml) {
throw new Exception(sprintf('Failed to read xml data from file %s. Unknown error (permissions?)', $filename), Exception::BAD_LOADERDATA);
}
return new static($xml);
}
/** * @test */ public function shouldBehave() { $loader = XML::createFromFile(__DIR__ . '/../../../../assets/xml/example.xml'); $writer = new Memory(); $delegator = new ReaderWriterDelegator($loader, $writer); $this->assertSame($loader, $delegator->getReader()); $this->assertSame($writer, $delegator->getWriter()); $this->assertContains('Memory', $delegator->toString()); $this->assertContains('XML', $delegator->toString()); $this->assertContains('Memory', $delegator . ''); $this->assertContains('XML', $delegator . ''); }