Skip to content

Commit

Permalink
Add prettifier feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sunchayn committed Feb 4, 2019
1 parent f328093 commit dee9b37
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
'output' => [
'ranges' => 'output/ranges.txt',
'rangesFileTitle' => 'Unicode Ranges ( RegEx Pattern )',
'prettyRanges' => 'output/prettyRanges.txt',
'prettyRangesFileTitle' => 'Pretty Ranges',
'json' => 'output/scrapped_data.json',
'jsonFileTitle' => 'Scrapped Data',
'test' => 'output/patternTestResult.txt',
'testFileTitle' => 'Pattern Test Result',

]
];
4 changes: 4 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use MazenTouati\Simple2wayConfig\S2WConfigFactory;
use MazenTouati\NoEmoji\Scrapper;
use MazenTouati\NoEmoji\Handler;
use MazenTouati\NoEmoji\Prettifier;

// Initialize the configuration
$config = S2WConfigFactory::create(__DIR__ . '/config');
Expand All @@ -30,6 +31,9 @@
// ---
$viewData['test'] = $handler->testPattern();

// Prettifier
// ---
$viewData['Prettifier'] = Prettifier::factory($config)->run()->export();

// Functions
// ---
Expand Down
99 changes: 99 additions & 0 deletions src/Prettifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
declare(strict_types=1);

namespace MazenTouati\NoEmoji;

use MazenTouati\Simple2wayConfig\S2WConfig;
use MazenTouati\NoEmoji\Entities\File;

/**
* Uses the scrapped unicodes to generate a RegEx pattern
*
* @author Mazen Touati <[email protected]>
*/
class Prettifier
{
/**
* The class instance
*
* @var Prettifier
*/
private static $_instance = null;

/**
* File instance
* @var File
*/
private $_file;

/**
* The pretty content
* @var string
*/
private $_content;

/**
* Retrieves the singleton instance
*
* @return Prettifier
*/
public static function getInstance()
{
if (self::$_instance == null) {
self::$_instance = new Prettifier();
}

return self::$_instance;
}

/**
* Initializes the Scrapper
*
* @param S2WConfig $c Configuration object
*
* @return Handler
*/
public static function factory(S2WConfig $c)
{
$instance = self::getInstance();
$instance->config = $c;

$instance->_file = new File($c, 'storage.output.ranges');

return $instance;
}

/**
* Starts prettifying
*
* @param string $type Prettifier class name
*
* @return Prettifier
*/
public function run($type = 'ArrayPrettifier')
{
$type = 'MazenTouati\NoEmoji\Prettifiers\\'.$type;
$this->_content = $type::run($this->_file);
return $this;
}

/**
* Exports the pretty output
*
* @return boolean|array Returns false on error or the result array
*/
public function export()
{
$base = $this->config->get('storage.base');
$fileTitle = $this->config->get('storage.output.prettyRangesFileTitle', 'Undefined Title');
$output = $this->config->get('storage.output.prettyRanges');

$export = File::toText($base . $output, $fileTitle, $this->_content);

if (!is_array($export)) {
return false;
}

return $export;
}
}
35 changes: 35 additions & 0 deletions src/Prettifiers/ArrayPrettifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);

namespace MazenTouati\NoEmoji\Prettifiers;

use MazenTouati\NotEmoji\Entities\File;

/**
* @author Mazen Touati <[email protected]>
*/
class ArrayPrettifier
{
/**
* Prettify the input
*
* @param File $file The file instance
*
* @return string
*/
public static function run($file)
{
$content = trim($file->content, '\'');
$ranges = explode('|', $content);

$pretty = '[' . PHP_EOL;

foreach ($ranges as $range) {
$pretty .= "\t'". $range . "'," . PHP_EOL;
}

$pretty .= ']' . PHP_EOL;

return $pretty;
}
}

0 comments on commit dee9b37

Please sign in to comment.