Skip to content

Commit 60adc65

Browse files
committed
Merge branch 'master' of https://github.com/Level-2/Transphporm
All tests are passing
2 parents 8c3253f + 43b17bf commit 60adc65

File tree

8 files changed

+116
-20
lines changed

8 files changed

+116
-20
lines changed

src/Parser/CssToXpath.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static function processAttr($attr, $element, $hash) {
5050

5151
$parser = new \Transphporm\Parser\Value($functionSet, true);
5252
$return = $parser->parseTokens($attr, $attributes);
53-
return $return[0] === '' ? false : $return[0];
53+
return is_array($return[0]) || $return[0] === '' ? false : $return[0];
5454
}
5555

5656
public function cleanup() {

src/Parser/Tokenizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class Tokenizer {
99
private $str;
1010
private $tokenizeRules = [];
11-
11+
1212
const NAME = 'LITERAL';
1313
const STRING = 'STRING';
1414
const OPEN_BRACKET = 'OPEN_BRACKET';
@@ -26,6 +26,7 @@ class Tokenizer {
2626
const OPEN_BRACE = 'OPEN_BRACE';
2727
const CLOSE_BRACE = 'CLOSE_BRACE';
2828
const BOOL = 'BOOL';
29+
const IN = 'IN';
2930
const COLON = 'COLON';
3031
const SEMI_COLON = 'SEMI_COLON';
3132
const NUM_SIGN = 'NUM_SIGN';

src/Parser/Tokenizer/Literals.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function tokenize(TokenizedString $str, Tokens $tokens) {
2121
$name .= $str->read($j+1);
2222
$j++;
2323
}
24-
24+
2525
$str->move($j);
2626

2727
$this->processLiterals($tokens, $name, $str);
@@ -38,8 +38,19 @@ private function isLiteral($n, $str) {
3838

3939
private function processLiterals($tokens, $name, $str) {
4040
if (is_numeric($name)) $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]);
41-
else if ($name == 'true') $tokens->add(['type' => Tokenizer::BOOL, 'value' => true]);
42-
else if ($name == 'false') $tokens->add(['type' => Tokenizer::BOOL, 'value' => false]);
41+
else if (method_exists($this, $name)) $this->$name($tokens);
4342
else $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]);
4443
}
44+
45+
private function true($tokens) {
46+
$tokens->add(['type' => Tokenizer::BOOL, 'value' => true]);
47+
}
48+
49+
private function false($tokens) {
50+
$tokens->add(['type' => Tokenizer::BOOL, 'value' => false]);
51+
}
52+
53+
private function in($tokens) {
54+
$tokens->add(['type' => Tokenizer::IN, 'value' => 'in']);
55+
}
4556
}

src/Parser/Value.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Value {
1818
private $data;
1919
private $result;
2020
private $traversing = false;
21-
private $allowNullResult;
21+
private $allowNullResult = false;
2222

2323
private $tokenFuncs = [
2424
Tokenizer::NOT => 'processComparator',
@@ -37,7 +37,8 @@ class Value {
3737
Tokenizer::OPEN_BRACKET => 'processBrackets',
3838
Tokenizer::GREATER_THAN => 'processComparator',
3939
Tokenizer::LOWER_THAN => 'processComparator',
40-
];
40+
Tokenizer::IN => 'processComparator'
41+
];
4142

4243
public function __construct($data, $autoLookup = false, $allowNullResult = false) {
4344
$this->baseData = $data;
@@ -69,6 +70,7 @@ public function parseTokens($tokens, $data = null) {
6970
}
7071

7172
private function processComparator($token) {
73+
$this->allowNullResult = false;
7274
$this->last->process();
7375

7476
if (!(in_array($this->result->getMode(), array_keys($this->tokenFuncs, 'processComparator')) && $token['type'] == Tokenizer::EQUALS)) {
@@ -143,7 +145,7 @@ private function callTransphpormFunctions($token) {
143145
$val = $this->baseData->{$this->last->read()}($token['value']);
144146
$this->result->processValue($val);
145147

146-
if ($this->autoLookup && is_string($val)) {
148+
if ($this->autoLookup && is_string($val)) {
147149
$parser = new Value($this->data->getData());
148150
$parsedArr = $parser->parse($val);
149151
$parsedVal = isset($parsedArr[0]) ? $parsedArr[0] : null;

src/Parser/ValueResult.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public function processValue($newValue) {
2828
Tokenizer::MULTIPLY => 'mult',
2929
Tokenizer::DIVIDE => 'div',
3030
Tokenizer::GREATER_THAN => 'greater',
31-
Tokenizer::LOWER_THAN => 'lower'
31+
Tokenizer::LOWER_THAN => 'lower',
32+
Tokenizer::IN => 'in'
3233
];
3334

3435
if ($funcs[$this->mode] === 'concat' && is_numeric($newValue)
@@ -38,6 +39,11 @@ public function processValue($newValue) {
3839
$this->{$funcs[$this->mode]}($newValue);
3940
}
4041

42+
public function in($value) {
43+
if (!is_array($value)) throw new \Exception(' `in` can only be used with arrays');
44+
$this->result[count($this->result)-1] = in_array($this->result[count($this->result)-1], $value);
45+
}
46+
4147
public function arg($value) {
4248
$this->result[] = $value;
4349
}

tests/PseudoTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,4 +546,51 @@ public function testNotWithPseudoInside() {
546546
<div class="three">foo</div>
547547
'), $this->stripTabs($template->output()->body));
548548
}
549+
550+
public function testInOperator() {
551+
// Issue #185 https://github.com/Level-2/Transphporm/issues/185
552+
$xml = '<select name="teachers[]" id="teachers" multiple>
553+
<option value="1">T1</option>
554+
</select>';
555+
556+
$tss = '
557+
select#teachers option {repeat: data(teachers); content: iteration(name)}
558+
select#teachers option:attr(value) {content: iteration(id)}
559+
select#teachers option:iteration[name in data(subject.teachers)]:attr(selected) { content: "selected"}
560+
';
561+
562+
$data = json_decode('{
563+
"subject": {
564+
"id": 11,
565+
"name": "Subject 5",
566+
"year": 2,
567+
"teachers": [ "Teacher 3", "Teacher 11"]
568+
},
569+
"teachers": [
570+
{
571+
"id": 1,
572+
"name": "Teacher 1"
573+
},
574+
{
575+
"id": 3,
576+
"name": "Teacher 3"
577+
578+
},
579+
{
580+
"id": 11,
581+
"name": "Teacher 11"
582+
}
583+
]
584+
}');
585+
586+
587+
$template = new \Transphporm\Builder($xml, $tss);
588+
$this->assertEquals($this->stripTabs('
589+
<select name="teachers[]" id="teachers" multiple>
590+
<option value="1">Teacher 1</option>
591+
<option value="3" selected>Teacher 3</option>
592+
<option value="11" selected>Teacher 11</option>
593+
</select>
594+
'), $this->stripTabs($template->output($data)->body));
595+
}
549596
}

tests/TransphpormTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,9 +1673,9 @@ public function testRuleWithNewline() {
16731673
public function testCommentBlock() {
16741674

16751675
$tss = 'div {content: "foo"}
1676-
1676+
16771677
/*.comment {foo: bar} */
1678-
1678+
16791679
span {content: "bar"}
16801680
';
16811681

@@ -1795,7 +1795,7 @@ public function testSetLocale() {
17951795
$template2 = new \Transphporm\Builder($xml, $tss);
17961796
$template2->setLocale('enUS');
17971797

1798-
$this->assertEquals('<div>' . date('m/d/Y') . '</div>', $template1->output()->body);
1798+
$this->assertEquals('<div>' . date('m/d/Y') . '</div>', $template2->output()->body);
17991799
}
18001800

18011801
public function testDebugOutput() {
@@ -1824,7 +1824,7 @@ public function testGreater() {
18241824
$template = new Builder($xml, $tss);
18251825

18261826
$this->assertEquals('<div><span class="one"></span><span class="two">true</span></div>', $this->stripTabs($template->output()->body));
1827-
1827+
18281828
}
18291829

18301830
public function testLess() {
@@ -1840,7 +1840,7 @@ public function testLess() {
18401840
$template = new Builder($xml, $tss);
18411841

18421842
$this->assertEquals('<div><span class="one">true</span><span class="two"></span></div>', $this->stripTabs($template->output()->body));
1843-
1843+
18441844
}
18451845

18461846
public function testFillSelect() {
@@ -1863,7 +1863,7 @@ public function testFillSelect() {
18631863

18641864
$this->assertEquals($this->stripTabs('<select><option value="01">January</option>
18651865
<option value="02">Februrary</option>
1866-
<option value="03">March</option></select>'), $this->stripTabs($output));
1866+
<option value="03">March</option></select>'), $this->stripTabs($output));
18671867

18681868
}
18691869

@@ -1872,10 +1872,10 @@ public function testFillSelect() {
18721872
public function testFillSelectWithNull() {
18731873
$xml = '<select><option></option></select>';
18741874

1875-
$tss = 'select option {repeat: data(options); }
1875+
$tss = 'select option {repeat: data(options); }
18761876
select option { content: iteration(); }
18771877
select option:attr(value) { content: key(); }
1878-
1878+
18791879
';
18801880

18811881
$template = new Builder($xml, $tss);
@@ -1891,7 +1891,7 @@ public function testFillSelectWithNull() {
18911891

18921892
$this->assertEquals($this->stripTabs('<select><option value="0"></option><option value="01">January</option>
18931893
<option value="02">Februrary</option>
1894-
<option value="03">March</option></select>'), $this->stripTabs($output));
1894+
<option value="03">March</option></select>'), $this->stripTabs($output));
18951895

18961896
}
18971897

@@ -1917,7 +1917,7 @@ public function testKeyBeforeIteration() {
19171917

19181918
$this->assertEquals($this->stripTabs('<select><option value="01">January</option>
19191919
<option value="02">Februrary</option>
1920-
<option value="03">March</option></select>'), $this->stripTabs($output));
1920+
<option value="03">March</option></select>'), $this->stripTabs($output));
19211921

19221922
}
19231923

@@ -1943,7 +1943,7 @@ public function testRepeatAtEnd() {
19431943

19441944
$this->assertEquals($this->stripTabs('<select><option value="01">January</option>
19451945
<option value="02">Februrary</option>
1946-
<option value="03">March</option></select>'), $this->stripTabs($output));
1946+
<option value="03">March</option></select>'), $this->stripTabs($output));
19471947

19481948
}
19491949

tests/ValueParserTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,35 @@ public function testReturnZero() {
362362
$this->assertEquals([0], $result);
363363
}
364364

365+
public function testInComparisonTrue() {
366+
367+
$stub = $this->getMockBuilder('TestData')->setMethods(['data'])
368+
->getMock();
369+
370+
$stub->expects($this->any())->method('data')->with($this->equalTo('array'))->will($this->returnValue(['one', 'two']));
371+
372+
$value = new Value($stub);
373+
374+
$result = $value->parse('"one" in data(array)');
375+
376+
$this->assertEquals($result[0], true);
377+
}
378+
379+
380+
public function testInComparisonFalse() {
381+
382+
$stub = $this->getMockBuilder('TestData')->setMethods(['data'])
383+
->getMock();
384+
385+
$stub->expects($this->any())->method('data')->with($this->equalTo('array'))->will($this->returnValue(['one', 'two']));
386+
387+
$value = new Value($stub);
388+
389+
$result = $value->parse('"three" in data(array)');
390+
391+
$this->assertEquals($result[0], false);
392+
}
393+
365394
}
366395

367396

0 commit comments

Comments
 (0)