<commit_before><?php
namespace Raideer\Tweech\Command;
use Raideer\Tweech\Command\CommandInterface;

class CommandRegistry{

  protected $commands = array();
  protected $registered = array();
  protected $registeredIdentifiers = array();

  public function register(CommandInterface $command){
    $name = $command->getCommand();
    if(array_key_exists($name, $this->commands)){
      throw new \Raideer\Tweech\Exception\CommandException("Command already exists");
    }

    $this->commands[$name] = $command;
    $registered = array();
    $registered[$name]['ids'] = $ids = $this->buildId($command);
    $this->registeredIdentifiers = array_merge($this->registeredIdentifiers, $ids);
    $this->registered[] = $registered;
  }

  protected function buildId(CommandInterface $command){
    $ids = array();
    foreach($command->getCommandIdentifier() as $id){
      $ids[] = $id . $command->getCommand();
    }

    return $ids;
  }

  public function getCommandAndExecute($command){

  }

  public function isCommand($string){
    foreach($this->registeredIdentifiers as $id){
      if(starts_with($id , $string)){
        return true;
      }
    }
    return false;
  }


}<commit_msg>Rewritten command registry<commit_after><?php
namespace Raideer\Tweech\Command;
use Raideer\Tweech\Command\CommandInterface;

class CommandRegistry{

  protected $commands = [];
  protected $registeredIdentifiers = [];
  protected $ids = [];

  public function register(CommandInterface $command){
    $name = $command->getCommand();
    if(array_key_exists($name, $this->commands)){
      throw new CommandException("Command with name '$name' already registered");
      return;
    }

    $this->registeredIdentifiers[$name] = $ids = $command->getCommandIdentifier();
    $this->ids = array_merge($this->ids, $ids);
    $this->commands[$name] = $command;
  }

  public function getId($string){
    foreach($this->ids as $id){
      if(starts_with($string, $id)){
        return $id;
      }
    }

    return null;
  }

  public function getCommandIfExists($string){
    $id = $this->getId($string);
    if($id === null){
      return null;
    }

    foreach($this->registeredIdentifiers as $commandName => $ids){
      if(array_search($id, $ids) !== false){
        if(starts_with($string, $id . $commandName)){
          return $this->commands[$commandName];
        }
      }
    }
    return null;
  }
}
{'fraction_non_alphanumeric': 0.15195670274771025, 'fraction_numerical': 0.0, 'mean_word_length': 3.638996138996139, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2123951', 'n_tokens_mistral': 733, 'n_tokens_neox': 703, 'n_words': 145}