Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement methods to make espeak to speak #1

Merged
merged 11 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
10 changes: 8 additions & 2 deletions .github/workflow/phpunit.yml → .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ jobs:
# do not stop on another job's failure
fail-fast: false
matrix:
php-versions: ['8.0', '8.1', '8.2']
php-versions: ['8.2', '8.3']
coverage: ['xdebug']
xdebug_mode: ['debug']
name: php${{ matrix.php-versions }}
steps:
- name: Install Dependencies
run: |
sudo apt-get update
sudo apt-get install -y espeak-ng
- name: Checkout
uses: actions/checkout@v4
- name: Set up php ${{ matrix.php-versions }}
uses: shivammathur/setup-php@v2
with:
Expand All @@ -26,7 +32,7 @@ jobs:
- name: PHPUnit
env:
XDEBUG_MODE: ${{ matrix.xdebug_mode }}
run: ./vendor/bin/phpunit -c phpunit.xml
run: composer run test:unit
upload-coverage:
needs: phpunit
runs-on: ubuntu-latest
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# PHP wrapper for eSpeak NG

### About eSpeak NG

eSpeak is a free and open-source, cross-platform, compact, software speech synthesizer.

### About eSpeak NG in this project

Thinking about accessibility for visually impaired users when they need to fill out the contact form. We decided to implement the eSpeak NG library, which has the functionality to speak captcha characters.

### Library Documentation

https://github.com/espeak-ng/espeak-ng
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@
"cs:check": "phpcs --standard=PSR12 -n src tests/Unit",
"cs:fix": "phpcbf --standard=PSR12 -n src tests/Unit",
"test:unit": "phpunit --no-coverage",
"test:coverage": "XDEBUG_MODE=coverage phpunit"
"test:coverage": "XDEBUG_MODE=coverage phpunit",
"lint": "find . -name \\*.php -not -path './vendor/*' -not -path './vendor-bin/*' -print0 | xargs -0 -n1 php -l",
"post-install-cmd": [
"@composer bin all install --ansi",
"composer dump-autoload"
],
"post-update-cmd": [
"composer dump-autoload"
]
},
"config": {
"allow-plugins": {
Expand Down
33 changes: 31 additions & 2 deletions src/Espeak.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,41 @@

namespace Libresign\Espeak;

use InvalidArgumentException;

class Espeak
{
private InputOptions $inputOptions;
public function __construct()
{
$this->inputOptions = new InputOptions();
}

public function getVersion()
{
\exec('espeak-ng --version', $output);
preg_match('/: (?<version>[\d\.]+) /', $output[0], $matches);
$this->setOption('version');
$output = $this->execute();
preg_match('/: (?<version>[\d\.]+) /', $output, $matches);
return $matches['version'];
}

public function setOption(string $name, ?string $value = null): self
{
$this->inputOptions->setOption($name, $value);
return $this;
}

public function execute(string $argument = ''): string
{
$options = ' ' . $this->inputOptions;
if (strlen($options) === 1) {
throw new InvalidArgumentException();
}
$cmd = 'espeak-ng' . $options;
if (strlen($argument) > 0) {
$cmd .= ' ' . $argument;
}
$output = \shell_exec($cmd);
return $output;
}
}
11 changes: 10 additions & 1 deletion src/InputOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ public function hasOption(string $name)
return array_key_exists($name, $this->options);
}

private function quoteString(string $value): string
{
$value = str_replace('"', '\"', $value);
if (str_contains($value, ' ')) {
$value = '"' . $value . '"';
}
return $value;
}

private function nameValueToString(string $name, array $values, string $separator, string $type)
{
$return = [];
foreach ($values as $value) {
if (empty($value)) {
$return[] = $type . $name;
} else {
$return[] = $type . $name . $separator . $value;
$return[] = $type . $name . $separator . $this->quoteString($value);
}
}
return implode(' ', $return);
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/EspeakTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Libresign\Test;

use InvalidArgumentException;
use Libresign\Espeak\Espeak;
use PHPUnit\Framework\TestCase;

Expand All @@ -13,4 +14,26 @@ public function testGetVersion()
$actual = $Espeak->getVersion();
$this->assertNotEmpty($actual);
}

public function testGetVersionUsingOption()
{
$actual = (new Espeak())
->setOption('version')
->execute();
$this->assertMatchesRegularExpression('/: (?<version>[\d\.]+) /', $actual);
}

public function testThrowExceptionWhenOptionIsEmpty()
{
$this->expectException(InvalidArgumentException::class);
(new Espeak())->execute();
}

public function testSpeakWithSuccess()
{
$actual = (new Espeak())
->setOption('stdout')
->execute('1');
$this->assertNotEmpty($actual);
}
}
4 changes: 4 additions & 0 deletions tests/Unit/InputOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public static function dataStringable(): array
[['test'],'--test'],
[['t'],'-t'],
[['t' => '123'],'-t 123'],
[['t' => '1 2 3'],'-t "1 2 3"'],
[['t' => '1 "2" 3'],'-t "1 \"2\" 3"'],
[['t' => '123', 't'],'-t 123 -t'],
[['test' => '123', 'test'],'--test=123 --test'],
[['test' => '123', 'test', 't' => '123', 't'],'--test=123 --test -t 123 -t'],
];
}
}