-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from photogabble/dev-1.1.0
1.1.0 release
- Loading branch information
Showing
11 changed files
with
12,656 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
namespace Photogabble\ConfusableHomoglyphs\Categories; | ||
|
||
use Exception; | ||
|
||
class JsonGenerator | ||
{ | ||
|
||
/** | ||
* @var \DateTime | ||
*/ | ||
private $sourceDatetime; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $codePointsRanges = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $iso15924Aliases = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $categories = []; | ||
|
||
/** | ||
* Generates the categories JSON data file from the unicode specification | ||
* loaded from the given `$filePathname` string. | ||
* | ||
* @param string $filePathname | ||
* @throws Exception | ||
*/ | ||
public function generateFromFile(string $filePathname) | ||
{ | ||
if (!file_exists($filePathname)){ | ||
throw new Exception('The file found at ['.$filePathname.'] could not be read.'); | ||
} | ||
$handle = fopen($filePathname, "r"); | ||
if ($handle) { | ||
while (($line = fgets($handle)) !== false) { | ||
$this->parseLine($line); | ||
} | ||
fclose($handle); | ||
} else { | ||
throw new Exception('The file found at ['.$filePathname.'] could not be opened.'); | ||
} | ||
|
||
sort($this->codePointsRanges); | ||
} | ||
|
||
/** | ||
* Parse the given $line into code point range's, alias and category. | ||
* | ||
* @param string $line | ||
*/ | ||
private function parseLine(string $line) | ||
{ | ||
if (preg_match('/Date: ([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])), ((?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)) ([A-Z]+)/', $line, $dateMatches) > 0) { | ||
$this->sourceDatetime = new \DateTime($dateMatches[1] . ' ' . $dateMatches[4], new \DateTimeZone($dateMatches[8])); | ||
return; | ||
} unset($dateMatches); | ||
|
||
if (preg_match('/([0-9A-F]+)(?:\.\.([0-9A-F]+))?\W+(\w+)\s*#\s*(\w+)/', $line, $matches) < 1) { | ||
return; | ||
} | ||
|
||
$codePointRangeFrom = $matches[1]; | ||
$codePointRangeTo = $matches[2]; | ||
$alias = mb_strtoupper($matches[3]); | ||
$category = $matches[4]; | ||
|
||
if (! in_array($alias, $this->iso15924Aliases)){ | ||
$this->iso15924Aliases[] = $alias; | ||
} | ||
|
||
if (! in_array($category, $this->categories)){ | ||
$this->categories[] = $category; | ||
} | ||
|
||
$this->codePointsRanges[] = [ | ||
hexdec($codePointRangeFrom), | ||
hexdec((empty($codePointRangeTo) ? $codePointRangeFrom : $codePointRangeTo)), | ||
array_search($alias, $this->iso15924Aliases, true), | ||
array_search($category, $this->categories, true) | ||
]; | ||
} | ||
|
||
/** | ||
* Return categories data as an array. | ||
* | ||
* @return array | ||
*/ | ||
public function toArray() : array | ||
{ | ||
return [ | ||
'timestamp' => $this->sourceDatetime->format('c'), | ||
'code_points_ranges' => $this->codePointsRanges, | ||
'categories' => $this->categories, | ||
'iso_15924_aliases' => $this->iso15924Aliases | ||
]; | ||
} | ||
|
||
/** | ||
* Return categories data as a json string. | ||
* | ||
* @return string | ||
* @throws Exception | ||
*/ | ||
public function toJson() : string | ||
{ | ||
$json = json_encode($this->toArray()); | ||
if ($json === false) { | ||
throw new Exception(json_last_error_msg(), json_last_error()); | ||
} | ||
return $json; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace Photogabble\ConfusableHomoglyphs\Confusable; | ||
|
||
use Exception; | ||
|
||
class JsonGenerator | ||
{ | ||
|
||
/** | ||
* @var \DateTime | ||
*/ | ||
private $sourceDatetime; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $confusablesMatrix = []; | ||
|
||
|
||
|
||
/** | ||
* Generates the categories JSON data file from the unicode specification | ||
* loaded from the given `$filePathname` string. | ||
* | ||
* @param string $filePathname | ||
* @throws Exception | ||
*/ | ||
public function generateFromFile(string $filePathname) | ||
{ | ||
if (!file_exists($filePathname)){ | ||
throw new Exception('The file found at ['.$filePathname.'] could not be read.'); | ||
} | ||
$handle = fopen($filePathname, "r"); | ||
if ($handle) { | ||
while (($line = fgets($handle)) !== false) { | ||
$this->parseLine($line); | ||
} | ||
fclose($handle); | ||
} else { | ||
throw new Exception('The file found at ['.$filePathname.'] could not be opened.'); | ||
} | ||
} | ||
|
||
/** | ||
* Parse the given $line into code point range's, alias and category. | ||
* | ||
* @param string $line | ||
*/ | ||
private function parseLine(string $line) | ||
{ | ||
if (preg_match('/Date: ([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])), ((?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)) ([A-Z]+)/', $line, $dateMatches) > 0) { | ||
$this->sourceDatetime = new \DateTime($dateMatches[1] . ' ' . $dateMatches[4], new \DateTimeZone($dateMatches[8])); | ||
return; | ||
} unset($dateMatches); | ||
|
||
if (preg_match('/[0-9A-F ]+\s+;\s*[0-9A-F ]+\s+;\s*\w+\s*#\*?\s*\( (.+) → (.+) \) (.+) → (.+)\t#/', $line, $matches) < 1) { | ||
return; | ||
} | ||
|
||
$charOne = $matches[1]; | ||
$charTwo = $matches[2]; | ||
$nameOne = $matches[3]; | ||
$nameTwo = $matches[4]; | ||
|
||
if (! isset($this->confusablesMatrix[$charOne])) { | ||
$this->confusablesMatrix[$charOne] = []; | ||
} | ||
|
||
$this->confusablesMatrix[$charOne][] = [ | ||
'c' => $charTwo, | ||
'n' => $nameTwo | ||
]; | ||
|
||
|
||
if (! isset($this->confusablesMatrix[$charTwo])) { | ||
$this->confusablesMatrix[$charTwo] = []; | ||
} | ||
|
||
$this->confusablesMatrix[$charTwo][] = [ | ||
'c' => $charOne, | ||
'n' => $nameOne | ||
]; | ||
} | ||
|
||
/** | ||
* Return categories data as an array. | ||
* | ||
* @return array | ||
*/ | ||
public function toArray() : array | ||
{ | ||
return $this->confusablesMatrix; | ||
} | ||
|
||
/** | ||
* Return categories data as a json string. | ||
* | ||
* @return string | ||
* @throws Exception | ||
*/ | ||
public function toJson() : string | ||
{ | ||
$json = json_encode($this->toArray()); | ||
if ($json === false) { | ||
throw new Exception(json_last_error_msg(), json_last_error()); | ||
} | ||
return $json; | ||
} | ||
} |
Oops, something went wrong.