Skip to content

Commit e82f42a

Browse files
authored
Added factory for source location (#21)
* Added factory for source location * bugfix * style
1 parent 5a43062 commit e82f42a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Model/SourceLocation.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ public function __construct($message, $path, $line, array $context = [])
5252
$this->context = $context;
5353
}
5454

55+
/**
56+
* Create a source location from your current location.
57+
*
58+
* @param string $message
59+
* @param array $context
60+
*
61+
* @return SourceLocation
62+
*/
63+
public static function createHere($message, array $context = [])
64+
{
65+
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1);
66+
67+
return new self($message, $trace[0]['file'], $trace[0]['line'], $context);
68+
}
69+
5570
/**
5671
* @return string
5772
*/
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Extractor\Tests\Unit\Model;
13+
14+
use Translation\Extractor\Model\SourceLocation;
15+
16+
class SourceLocationTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testCreateHere()
19+
{
20+
$location = SourceLocation::createHere('foobar', ['foo' => 'bar']);
21+
22+
$this->assertContains('tests/Unit/Model/SourceLocationTest.php', $location->getPath());
23+
$this->assertEquals(20, $location->getLine());
24+
25+
$this->assertEquals('foobar', $location->getMessage());
26+
$this->assertEquals(['foo' => 'bar'], $location->getContext());
27+
}
28+
}

0 commit comments

Comments
 (0)