Chris@17: assertCommands($commands); Chris@17: $commands = $commands ?: $this->availableCommands; Chris@17: Chris@17: $target = clone $this; Chris@17: foreach ($commands as $command) { Chris@17: $target->assertions[$command] = $assertable; Chris@17: } Chris@17: return $target; Chris@17: } Chris@17: Chris@17: /** Chris@17: * @param string $path Chris@17: * @param string $command Chris@17: * @return bool Chris@17: */ Chris@17: public function assert($path, $command) Chris@17: { Chris@17: $this->assertCommand($command); Chris@17: $this->assertAssertionCompleteness(); Chris@17: Chris@17: return $this->assertions[$command]->assert($path, $command); Chris@17: } Chris@17: Chris@17: /** Chris@17: * @param array $commands Chris@17: */ Chris@17: private function assertCommands(array $commands) Chris@17: { Chris@17: $unknownCommands = array_diff($commands, $this->availableCommands); Chris@17: if (empty($unknownCommands)) { Chris@17: return; Chris@17: } Chris@17: throw new \LogicException( Chris@17: sprintf( Chris@17: 'Unknown commands: %s', Chris@17: implode(', ', $unknownCommands) Chris@17: ), Chris@17: 1535189881 Chris@17: ); Chris@17: } Chris@17: Chris@17: private function assertCommand($command) Chris@17: { Chris@17: if (in_array($command, $this->availableCommands, true)) { Chris@17: return; Chris@17: } Chris@17: throw new \LogicException( Chris@17: sprintf( Chris@17: 'Unknown command "%s"', Chris@17: $command Chris@17: ), Chris@17: 1535189882 Chris@17: ); Chris@17: } Chris@17: Chris@17: private function assertAssertionCompleteness() Chris@17: { Chris@17: $undefinedAssertions = array_diff( Chris@17: $this->availableCommands, Chris@17: array_keys($this->assertions) Chris@17: ); Chris@17: if (empty($undefinedAssertions)) { Chris@17: return; Chris@17: } Chris@17: throw new \LogicException( Chris@17: sprintf( Chris@17: 'Missing assertions for commands: %s', Chris@17: implode(', ', $undefinedAssertions) Chris@17: ), Chris@17: 1535189883 Chris@17: ); Chris@17: } Chris@17: }