Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Level-2/Transphporm
Browse files Browse the repository at this point in the history
All tests are passing
  • Loading branch information
solleer committed Mar 16, 2018
2 parents 8c3253f + 43b17bf commit 60adc65
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Parser/CssToXpath.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function processAttr($attr, $element, $hash) {

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

public function cleanup() {
Expand Down
3 changes: 2 additions & 1 deletion src/Parser/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Tokenizer {
private $str;
private $tokenizeRules = [];

const NAME = 'LITERAL';
const STRING = 'STRING';
const OPEN_BRACKET = 'OPEN_BRACKET';
Expand All @@ -26,6 +26,7 @@ class Tokenizer {
const OPEN_BRACE = 'OPEN_BRACE';
const CLOSE_BRACE = 'CLOSE_BRACE';
const BOOL = 'BOOL';
const IN = 'IN';
const COLON = 'COLON';
const SEMI_COLON = 'SEMI_COLON';
const NUM_SIGN = 'NUM_SIGN';
Expand Down
17 changes: 14 additions & 3 deletions src/Parser/Tokenizer/Literals.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function tokenize(TokenizedString $str, Tokens $tokens) {
$name .= $str->read($j+1);
$j++;
}

$str->move($j);

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

private function processLiterals($tokens, $name, $str) {
if (is_numeric($name)) $tokens->add(['type' => Tokenizer::NUMERIC, 'value' => $name]);
else if ($name == 'true') $tokens->add(['type' => Tokenizer::BOOL, 'value' => true]);
else if ($name == 'false') $tokens->add(['type' => Tokenizer::BOOL, 'value' => false]);
else if (method_exists($this, $name)) $this->$name($tokens);
else $tokens->add(['type' => Tokenizer::NAME, 'value' => $name, 'line' => $str->lineNo()]);
}

private function true($tokens) {
$tokens->add(['type' => Tokenizer::BOOL, 'value' => true]);
}

private function false($tokens) {
$tokens->add(['type' => Tokenizer::BOOL, 'value' => false]);
}

private function in($tokens) {
$tokens->add(['type' => Tokenizer::IN, 'value' => 'in']);
}
}
8 changes: 5 additions & 3 deletions src/Parser/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Value {
private $data;
private $result;
private $traversing = false;
private $allowNullResult;
private $allowNullResult = false;

private $tokenFuncs = [
Tokenizer::NOT => 'processComparator',
Expand All @@ -37,7 +37,8 @@ class Value {
Tokenizer::OPEN_BRACKET => 'processBrackets',
Tokenizer::GREATER_THAN => 'processComparator',
Tokenizer::LOWER_THAN => 'processComparator',
];
Tokenizer::IN => 'processComparator'
];

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

private function processComparator($token) {
$this->allowNullResult = false;
$this->last->process();

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

if ($this->autoLookup && is_string($val)) {
if ($this->autoLookup && is_string($val)) {
$parser = new Value($this->data->getData());
$parsedArr = $parser->parse($val);
$parsedVal = isset($parsedArr[0]) ? $parsedArr[0] : null;
Expand Down
8 changes: 7 additions & 1 deletion src/Parser/ValueResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function processValue($newValue) {
Tokenizer::MULTIPLY => 'mult',
Tokenizer::DIVIDE => 'div',
Tokenizer::GREATER_THAN => 'greater',
Tokenizer::LOWER_THAN => 'lower'
Tokenizer::LOWER_THAN => 'lower',
Tokenizer::IN => 'in'
];

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

public function in($value) {
if (!is_array($value)) throw new \Exception(' `in` can only be used with arrays');
$this->result[count($this->result)-1] = in_array($this->result[count($this->result)-1], $value);
}

public function arg($value) {
$this->result[] = $value;
}
Expand Down
47 changes: 47 additions & 0 deletions tests/PseudoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,4 +546,51 @@ public function testNotWithPseudoInside() {
<div class="three">foo</div>
'), $this->stripTabs($template->output()->body));
}

public function testInOperator() {
// Issue #185 https://github.com/Level-2/Transphporm/issues/185
$xml = '<select name="teachers[]" id="teachers" multiple>
<option value="1">T1</option>
</select>';

$tss = '
select#teachers option {repeat: data(teachers); content: iteration(name)}
select#teachers option:attr(value) {content: iteration(id)}
select#teachers option:iteration[name in data(subject.teachers)]:attr(selected) { content: "selected"}
';

$data = json_decode('{
"subject": {
"id": 11,
"name": "Subject 5",
"year": 2,
"teachers": [ "Teacher 3", "Teacher 11"]
},
"teachers": [
{
"id": 1,
"name": "Teacher 1"
},
{
"id": 3,
"name": "Teacher 3"
},
{
"id": 11,
"name": "Teacher 11"
}
]
}');


$template = new \Transphporm\Builder($xml, $tss);
$this->assertEquals($this->stripTabs('
<select name="teachers[]" id="teachers" multiple>
<option value="1">Teacher 1</option>
<option value="3" selected>Teacher 3</option>
<option value="11" selected>Teacher 11</option>
</select>
'), $this->stripTabs($template->output($data)->body));
}
}
22 changes: 11 additions & 11 deletions tests/TransphpormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1673,9 +1673,9 @@ public function testRuleWithNewline() {
public function testCommentBlock() {

$tss = 'div {content: "foo"}
/*.comment {foo: bar} */
span {content: "bar"}
';

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

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

public function testDebugOutput() {
Expand Down Expand Up @@ -1824,7 +1824,7 @@ public function testGreater() {
$template = new Builder($xml, $tss);

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

}

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

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

}

public function testFillSelect() {
Expand All @@ -1863,7 +1863,7 @@ public function testFillSelect() {

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

}

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

$tss = 'select option {repeat: data(options); }
$tss = 'select option {repeat: data(options); }
select option { content: iteration(); }
select option:attr(value) { content: key(); }
';

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

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

}

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

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

}

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

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

}

Expand Down
29 changes: 29 additions & 0 deletions tests/ValueParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,35 @@ public function testReturnZero() {
$this->assertEquals([0], $result);
}

public function testInComparisonTrue() {

$stub = $this->getMockBuilder('TestData')->setMethods(['data'])
->getMock();

$stub->expects($this->any())->method('data')->with($this->equalTo('array'))->will($this->returnValue(['one', 'two']));

$value = new Value($stub);

$result = $value->parse('"one" in data(array)');

$this->assertEquals($result[0], true);
}


public function testInComparisonFalse() {

$stub = $this->getMockBuilder('TestData')->setMethods(['data'])
->getMock();

$stub->expects($this->any())->method('data')->with($this->equalTo('array'))->will($this->returnValue(['one', 'two']));

$value = new Value($stub);

$result = $value->parse('"three" in data(array)');

$this->assertEquals($result[0], false);
}

}


Expand Down

0 comments on commit 60adc65

Please sign in to comment.