nodes[ $node->getId() ] ) ) { throw new \InvalidArgumentException( 'Duplicate node ID.' ); } $this->nodes[ $node->getId() ] = $node; } public function addEdge( Edge $edge ): void { if ( isset( $this->edges[ $edge->getId() ] ) ) { throw new \InvalidArgumentException( 'Duplicate edge ID.' ); } if ( ! isset( $this->nodes[ $edge->getSource() ], $this->nodes[ $edge->getTarget() ] ) ) { throw new \InvalidArgumentException( 'Edge endpoints must exist.' ); } $this->edges[ $edge->getId() ] = $edge; } public function jsonSerialize(): array { return array( 'nodes' => array_map( static function ( Node $node ) { return $node->jsonSerialize(); }, array_values( $this->nodes ) ), 'edges' => array_map( static function ( Edge $edge ) { return $edge->jsonSerialize(); }, array_values( $this->edges ) ), ); } }