Skip to content

Commit

Permalink
v2 새로운 구조로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFoxTail committed Jan 20, 2022
1 parent e277772 commit a941034
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 268 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ Usage
The return value is the *Hangul Compatibility Jamo* between `U+3130` and `U+318F`
반환 값은 `U+3130``U+318F` 사이의 *Hangul Compatibility Jamo*가 반환 됩니다.
- See [Hangul Compatibility Jamo](https://unicode.org/charts/PDF/U3130.pdf)

```php
use AcidF0x\KoreanHandler\Separator;
use AcidF0x\KoreanHandler\aaaaaaa;

$separator = new Separator();
$separator = new aaaaaaa();

$separator->separate("가");
$separator->getChoseong(); // "ㄱ"
Expand All @@ -36,9 +37,9 @@ The return value is the *Hangul Jamo* between `U+1100` and `U+11FF`
- See [Hangul Jamo](https://unicode.org/charts/PDF/U1100.pdf)

```php
use AcidF0x\KoreanHandler\Separator;
use AcidF0x\KoreanHandler\aaaaaaa;

$separator = new Separator();
$separator = new aaaaaaa();
$separator->strictMode();

$separator->separate("가");
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"require": {
"php": ">=7.4",
"ext-mbstring": "*"
"ext-mbstring": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
Expand Down
27 changes: 15 additions & 12 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions src/Character.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace AcidF0x\KoreanHandler;

class Character
{
private ?string $choseong = null;
private ?string $jungseong = null;
private ?string $jongseong = null;
/** @var array|string[] */
private array $split = [];

public function __construct(?string $choseong, ?string $jungseong, ?string $jongseong)
{
$this->choseong = $choseong;
$this->jungseong = $jungseong;
$this->jongseong = $jongseong;
}

public function getChoseong(): ?string
{
return $this->choseong;
}

public function setChoseong(?string $choseong): Character
{
$this->choseong = $choseong;
return $this;
}

public function getJungseong(): ?string
{
return $this->jungseong;
}

public function setJungseong(?string $jungseong): Character
{
$this->jungseong = $jungseong;
return $this;
}

public function getJongseong(): ?string
{
return $this->jongseong;
}

public function setJongseong(?string $jongseong): Character
{
$this->jongseong = $jongseong;
return $this;
}

/**
* @return array|string[]
*/
public function getSplit(): array
{
return $this->split;
}

public function setSplit(): Character
{
$split = [
$this->choseong,
$this->jungseong
];

if ($this->jongseong) {
$split[] = $this->jongseong;
}

$this->split = $split;
return $this;
}
}
17 changes: 17 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace AcidF0x\KoreanHandler;

final class Constants
{
/** @var int 한글 음절 첫글자 = 가 */
public const UNICODE_HANGUL_SYLLABLE_START = 44032;
/** @var int 한글 음절 마지막 글자 = 힣 */
public const UNICODE_HANGUL_SYLLABLE_END = 55203;
/** @var string[] 초성리스트 */
public const CHOSEONG_LIST = ["","","","","","","","","","","","","","","","","","",""];
/** @var string[] 중성리스트 */
const JUNGSEONG_LIST = ["","","","","","","","","","","","","","","","","","","","",""];
/** @var string[] 종성리스트 */
const JONGSEONG_LIST = ["","","","","","","","","","","","","","","","","","","","","","","","","","",""];
}
96 changes: 96 additions & 0 deletions src/SeparateResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace AcidF0x\KoreanHandler;

use ArrayAccess;
use Iterator;

class SeparateResult implements ArrayAccess, Iterator
{
private int $position;
/** @var array|Character */
private array $characters = [];

public function __construct(array $characters = [])
{
$this->position = 0;
$this->characters = $characters;
}

public function offsetSet($offset, $value) {
if (is_null($offset)) {
$this->characters[] = $value;
} else {
$this->characters[$offset] = $value;
}
}

public function offsetExists($offset): bool
{
return isset($this->characters[$offset]);
}

public function offsetUnset($offset)
{
unset($this->characters[$offset]);
}

public function offsetGet($offset): ?Character
{
return $this->characters[$offset] ?? null;
}

public function rewind()
{
$this->position = 0;
}

public function current(): Character
{
return $this->characters[$this->position];
}

public function key(): int
{
return $this->position;
}

public function next()
{
++$this->position;
}

public function valid(): bool
{
return isset($this->characters[$this->position]);
}

public function getChoseongList(): array
{
return array_map(fn(Character $char) => $char->getChoseong(), $this->characters);
}

public function getJungseongList(): array
{
return array_map(fn(Character $char) => $char->getJungseong(), $this->characters);
}

public function getJongseongList(): array
{
return array_filter(
array_map(fn(Character $char) => $char->getJongseong(), $this->characters),
fn($i) => $i !== null
);
}

public function getSplitList(): array
{
$result = [];
/** @var Character $character */
foreach ($this->characters as $character) {
array_push($result, ...$character->getSplit());
}

return $result;
}
}
Loading

0 comments on commit a941034

Please sign in to comment.