LanguageDetector\AbstractFormat::initFormatByPath PHP Method

initFormatByPath() public static method

public static initFormatByPath ( $path )
    public static function initFormatByPath($path)
    {
        $ext = explode(".", $path);
        $class = __NAMESPACE__ . '\\Format\\' . strtoupper(end($ext));
        if (!class_exists($class)) {
            throw new \RuntimeException("cannot find class {$class}");
        }
        $format = new $class($path);
        if (!$format instanceof AbstractFormat) {
            throw new \RuntimeException("{$class} must implement " . __NAMESPACE__ . "\\FormatInterface interface");
        }
        return $format;
    }

Usage Example

Example #1
0
<?php

require __DIR__ . '/../lib/LanguageDetector/autoload.php';
set_time_limit(0);
use LanguageDetector\Config;
use LanguageDetector\AbstractFormat;
use LanguageDetector\Learn;
ini_set('memory_limit', '1G');
mb_internal_encoding('UTF-8');
$config = new LanguageDetector\Config();
$config->useMb(true);
$c = new Learn($config);
foreach (glob(__DIR__ . '/samples/*') as $file) {
    $c->addSample(basename($file), file_get_contents($file));
}
$c->addStepCallback(function ($lang, $status) {
    echo "Learning {$lang}: {$status}\n";
});
$c->save(AbstractFormat::initFormatByPath(__DIR__ . '/datafile.php'));
$c->save(AbstractFormat::initFormatByPath(__DIR__ . '/datafile.ses'));
$c->save(AbstractFormat::initFormatByPath(__DIR__ . '/datafile.json'));