/** {@inheritDoc} */ public function execute(array $input) { $text = $this->textSanitizer->sanitize($input); $pattern = $input['pattern']; $replacement = $input['replacement']; $content = $this->contentFactory->make($text); $replacedContent = preg_replace($pattern, $replacement, $content); $replacedText = Text::fromString($replacedContent); $text->setLines($replacedText->getLines()); }
/** {@inheritdoc} */ public function execute(array $input) { $text = $this->textSanitizer->sanitize($input); $location = $this->locationSanitizer->sanitize($input); $addition = $input['addition']; $lines = $text->getLines(); array_splice($lines, $location, 0, $addition); $text->setLines($lines); $text->setCurrentLineNumber($location); }
/** {@inheritdoc} */ public function execute(array $input) { $text = $this->textSanitizer->sanitize($input); $location = $this->locationSanitizer->sanitize($input); $lines = $text->getLines(); array_splice($lines, $location, 1); $text->setLines($lines); $lineNumber = $location === $text->getLength() ? $location - 1 : $location; $text->setCurrentLineNumber($lineNumber); }
/** * {@inheritdoc} * * @throws InvalidArgumentException If replacement isn't valid */ public function execute(array $input) { $text = $this->textSanitizer->sanitize($input); $location = $this->locationSanitizer->sanitize($input); if (!is_callable($input['replacement'])) { throw new InvalidArgumentException('Invalid replacement'); } $line = $text->getLine($location); $replacement = $input['replacement']($line); $text->setLine($replacement, $location); $text->setCurrentLineNumber($location); }
function it_fails_when_the_line_number_is_invalid(TextSanitizer $textSanitizer, Text $text) { $exception = '\\Gnugat\\Redaktilo\\Exception\\InvalidLineNumberException'; $input = array('location' => 'toto'); $textSanitizer->sanitize($input)->willReturn($text); $this->shouldThrow($exception)->duringSanitize($input); $input = array('location' => -1); $textSanitizer->sanitize($input)->willReturn($text); $this->shouldThrow($exception)->duringSanitize($input); $input = array('location' => 55); $textSanitizer->sanitize($input)->willReturn($text); $this->shouldThrow($exception)->duringSanitize($input); }
function it_inserts_new_lines(TextSanitizer $textSanitizer, LocationSanitizer $locationSanitizer, Text $text) { $expectedFilename = sprintf(self::EXPECTED_FILENAME, $this->rootPath); $expectedLines = file($expectedFilename, FILE_IGNORE_NEW_LINES); $lineNumber = 6; $input = array('text' => $text, 'location' => $lineNumber - 1, 'addition' => "Pontius Pilate: '...Dickus?'"); $textSanitizer->sanitize($input)->willReturn($text); $locationSanitizer->sanitize($input)->willReturn($lineNumber - 1); $text->setLines($expectedLines)->shouldBeCalled(); $text->setCurrentLineNumber($lineNumber)->shouldBeCalled(); $this->execute($input); $input = array('text' => $text, 'addition' => "Pontius Pilate: '...Dickus?'"); $textSanitizer->sanitize($input)->willReturn($text); $locationSanitizer->sanitize($input)->willReturn($lineNumber - 1); $text->getCurrentLineNumber()->willReturn($lineNumber - 1); $text->setCurrentLineNumber($lineNumber)->shouldBeCalled(); $this->execute($input); }
/** * {@inheritdoc} * * @return int */ public function sanitize(array $input) { $text = $this->textSanitizer->sanitize($input); $location = isset($input['location']) ? $input['location'] : null; if (null === $location) { return $text->getCurrentLineNumber(); } if (!is_int($location)) { throw new InvalidLineNumberException($location, $text, 'The line number should be an integer'); } if ($location < 0) { throw new InvalidLineNumberException($location, $text, 'The line number should be positive'); } if ($location >= $text->getLength()) { throw new InvalidLineNumberException($location, $text, 'The line number should be strictly lower than the number of lines'); } return $location; }
function it_removes_lines(TextSanitizer $textSanitizer, LocationSanitizer $locationSanitizer, Text $text) { $expectedFilename = sprintf(self::EXPECTED_FILENAME, $this->rootPath); $expectedLines = file($expectedFilename, FILE_IGNORE_NEW_LINES); $lineNumber = 2; $input = array('text' => $text, 'location' => $lineNumber); $textSanitizer->sanitize($input)->willReturn($text); $locationSanitizer->sanitize($input)->willReturn($lineNumber); $text->setLines($expectedLines)->shouldBeCalled(); $text->getLength()->willReturn(count($expectedLines)); $text->setCurrentLineNumber($lineNumber)->shouldBeCalled(); $this->execute($input); $input = array('text' => $text); $textSanitizer->sanitize($input)->willReturn($text); $locationSanitizer->sanitize($input)->willReturn($lineNumber); $text->getCurrentLineNumber()->willReturn($lineNumber); $text->setCurrentLineNumber($lineNumber)->shouldBeCalled(); $this->execute($input); }
function it_replaces_line(TextSanitizer $textSanitizer, LocationSanitizer $locationSanitizer, Text $text) { $expectedFilename = sprintf(self::EXPECTED_FILENAME, $this->rootPath); $expectedLines = file($expectedFilename, FILE_IGNORE_NEW_LINES); $expectedLine = $expectedLines[self::LINE_NUMBER]; $replacement = function ($line) { return '[Even more sniggering]'; }; $input = array('text' => $text, 'location' => self::LINE_NUMBER, 'replacement' => $replacement); $textSanitizer->sanitize($input)->willReturn($text); $locationSanitizer->sanitize($input)->willReturn(self::LINE_NUMBER); $text->setLine($expectedLine, self::LINE_NUMBER)->shouldBeCalled(); $text->setCurrentLineNumber(self::LINE_NUMBER)->shouldBeCalled(); $this->execute($input); $input = array('text' => $text, 'replacement' => $replacement); $textSanitizer->sanitize($input)->willReturn($text); $locationSanitizer->sanitize($input)->willReturn(self::LINE_NUMBER); $text->getCurrentLineNumber()->willReturn(self::LINE_NUMBER); $text->setCurrentLineNumber(self::LINE_NUMBER)->shouldBeCalled(); $this->execute($input); }