Skip to content

Commit 29bb489

Browse files
authored
Merge pull request #2 from permafrost-dev/add-tostring-method
Add toString method
2 parents 1bcb09a + dfac355 commit 29bb489

5 files changed

+58
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to `code-snippets` will be documented in this file.
44

5+
## 1.1.0 - 2021-07-25
6+
7+
- add `toString()` and `__toString()` methods
8+
59
## 1.0.2 - 2021-07-25
610

711
- fix minor bugs, refactor line boundary calculations

src/CodeSnippet.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,22 @@ public function getLineNumberEnd(): int
142142
return $this->surroundingLines[count($this->surroundingLines) - 1] ?? 0;
143143
}
144144

145+
public function toString()
146+
{
147+
$result = '';
148+
149+
foreach ($this->getLines() as $line) {
150+
$result .= $line->value() . PHP_EOL;
151+
}
152+
153+
return $result;
154+
}
155+
156+
public function __toString()
157+
{
158+
return $this->toString();
159+
}
160+
145161
protected function isSurroundedLineNumber(int $lineNumber): bool
146162
{
147163
return in_array($lineNumber, $this->surroundingLines, true);

tests/CodeSnippetTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,26 @@ public function it_ensures_the_entire_surrounding_lines_are_displayed()
149149

150150
$this->assertMatchesSnapshot($snippet->getLines());
151151
}
152+
153+
/** @test */
154+
public function it_returns_the_snippet_as_a_string()
155+
{
156+
$snippet = (new CodeSnippet())
157+
->surroundingLines(10, 12)
158+
->snippetLineCount(8)
159+
->fromFile($this->testsPath('data/file3.php'));
160+
161+
$this->assertMatchesSnapshot($snippet->toString());
162+
}
163+
164+
/** @test */
165+
public function it_returns_a_string_when_the_snippet_is_cast_to_a_string()
166+
{
167+
$snippet = (new CodeSnippet())
168+
->surroundingLines(10, 12)
169+
->snippetLineCount(8)
170+
->fromFile($this->testsPath('data/file3.php'));
171+
172+
$this->assertMatchesSnapshot((string)$snippet);
173+
}
152174
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
@var Ray $ray
3+
*/
4+
$ray = ray()->text('hello world')
5+
->blue()
6+
->large();
7+
8+
Ray
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
@var Ray $ray
3+
*/
4+
$ray = ray()->text('hello world')
5+
->blue()
6+
->large();
7+
8+
Ray

0 commit comments

Comments
 (0)