Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
State
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addEdge
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 writeATT
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\LangConv\Construct;
5
6/**
7 * A state in a mutable FST.
8 */
9class State {
10    /** @var int */
11    public $id;
12    /** @var array<?Edge> */
13    public $edges = [];
14    /** @var bool */
15    public $isFinal = false;
16
17    /**
18     * Create a new state.
19     * @param int $id The index of this state in the parent MutableFST.
20     */
21    public function __construct( int $id ) {
22        $this->id = $id;
23    }
24
25    /**
26     * Add an edge from this state to another.
27     * @param string $upper The token on the upper side of the edge
28     * @param string $lower The token on the lower side of the edge
29     * @param State $to The destination of the edge
30     */
31    public function addEdge( string $upper, string $lower, State $to ): void {
32        $this->edges[] = new Edge( $this, count( $this->edges ), $upper, $lower, $to );
33    }
34
35    /**
36     * Write the edges of this state to the given $handle as an AT&T format
37     * file.
38     * @param resource $handle
39     */
40    public function writeATT( $handle ): void {
41        foreach ( $this->edges as $e ) {
42            $line = [
43                strval( $e->from->id ),
44                strval( $e->to->id ),
45                $e->upper, $e->lower
46            ];
47            fwrite( $handle, implode( "\t", $line ) . "\n" );
48        }
49    }
50}