izzum\statemachine\loader\XML::createFromFile PHP Method

createFromFile() public static method

creates an instance of this class with the data loaded from a file.
public static createFromFile ( string $filename ) : XML
$filename string
return XML 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);
    }

Usage Example

 /**
  * @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 . '');
 }
All Usage Examples Of izzum\statemachine\loader\XML::createFromFile