Skip to content

Commit

Permalink
Skip references inside of double-quoted strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kalifg committed Mar 10, 2014
1 parent 8eb78d6 commit b0a698b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Nelmio/Alice/Loader/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,12 @@ private function process($data, array $variables)
}, $args);

// replace references to other objects
$args = preg_replace_callback('{(?:(?<multi>\d+)x )?(?<!\\\\)@(?<reference>[a-z0-9_.*]+)(?:\->(?<property>[a-z0-9_-]+))?}i', function ($match) use ($that, $args) {
$args = preg_replace_callback('{(?<string>".*?[^\\\\]")|(?:(?<multi>\d+)x )?(?<!\\\\)@(?<reference>[a-z0-9_.*]+)(?:\->(?<property>[a-z0-9_-]+))?}i', function ($match) use ($that, $args) {

if (!empty($match['string'])) {
return $match['string'];
}

$multi = ('' !== $match['multi']) ? $match['multi'] : null;
$property = isset($match['property']) ? $match['property'] : null;
if (strpos($match['reference'], '*')) {
Expand Down
34 changes: 34 additions & 0 deletions tests/Nelmio/Alice/Loader/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,40 @@ public function testPassingReferenceToProvider()
$this->assertEquals($user1->username . '_' . $user2->username, $user3->username);
}

public function testSkippingReferencesInStrings()
{
$res = $this->loadData(array(
self::USER => array(
'user1' => array(
'username' => '<("[email protected]")>',
),
'user2' => array(
'username' => '<("foo@test" . "@com")>',
),
'user3' => array(
'username' => '<("foo\"@test.com")>',
),
),
));

$this->assertCount(3, $res);

$user1 = $this->loader->getReference('user1');
$this->assertInstanceOf(self::USER, $user1);

$this->assertEquals('[email protected]', $user1->username);

$user2 = $this->loader->getReference('user2');
$this->assertInstanceOf(self::USER, $user2);

$this->assertEquals('foo@test@com', $user2->username);

$user3 = $this->loader->getReference('user3');
$this->assertInstanceOf(self::USER, $user3);

$this->assertEquals('foo"@test.com', $user3->username);
}

public function testLoadCreatesEnumsOfObjects()
{
$res = $this->loadData(array(
Expand Down

0 comments on commit b0a698b

Please sign in to comment.