PHP Classes

File: src/Wordlist.php

Recommend this page to a friend!
  Classes of Rodolfo Berrios Arce   Wordle CLI   src/Wordlist.php   Download  
File: src/Wordlist.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Wordle CLI
Run the Wordle game on the command line console
Author: By
Last change:
Date: 8 days ago
Size: 2,372 bytes
 

Contents

Class file image Download
<?php

/*
 * This file is part of Chevere.
 *
 * (c) Rodolfo Berrios <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace
Rodber\Wordle;

use
Chevere\Filesystem\File;
use
Chevere\Filesystem\Interfaces\DirectoryInterface;
use
Chevere\Filesystem\Interfaces\FileInterface;
use
RuntimeException;
use function
Chevere\Message\message;
use function
Chevere\Writer\streamFor;

final class
Wordlist
{
    private
$fopen;

    private
string $lang;

    public function
__construct(
        private
FileInterface $file
   
) {
       
$this->openFile();
       
$this->lang = basename($file->path()->__toString());
    }

    public function
split(
       
DirectoryInterface $dir,
       
int $min = 3,
       
int $max = 8
   
): void {
       
$countWords = 0;
       
$collect = range($min, $max);
       
$writers = [];
        foreach (
$collect as $size) {
           
$file = new File($dir->path()->getChild("{$size}.php"));
           
$file->removeIfExists();
           
$file->create();
           
$writer = streamFor($file->path()->__toString(), 'w');
           
$writer->write('<?php return [' . PHP_EOL);
           
$writers[$size] = $writer;
        }
       
$this->openFile();
        while ((
$line = fgets($this->fopen)) !== false) {
           
$line = trim($line);
           
$line = preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($line, ENT_QUOTES, 'UTF-8'));
           
$length = mb_strlen($line);
            if (
in_array($length, $collect, true)) {
               
$writers[$length]->write("'" . $line . "',");
               
$countWords++;
            }
        }
        foreach (
$collect as $size) {
           
$writers[$size]->write('];');
        }
       
$countedWords = number_format($countWords);
        echo
" ? {$countedWords} words\n";

       
fclose($this->fopen);
    }

    private function
openFile(): void
   
{
       
$this->fopen = fopen($this->file->path()->__toString(), 'r');
        if (!
$this->fopen) {
            throw new
RuntimeException(
               
message('Could not open file %file%', [
                   
'%file%' => $this->file->path()->__toString(),
                ])
            );
        }
    }
}