/** * Check the string for syntax errors * * @return bool * @param string $string * @param string $type */ private function isCorrectSyntax($string, $type) { // init vars $string = (string) $string; $type = SpoonFilter::getValue($type, array('cycle', 'iteration', 'option', 'variable'), 'variable', 'string'); // types switch ($type) { // cycle string case 'cycle': // the number of single qoutes should always be an even number if (!SpoonFilter::isEven(substr_count($string, "'"))) { return false; } break; // iteration string // iteration string case 'iteration': // the number of square opening/closing brackets should be equal if (substr_count($string, '[') != substr_count($string, ']')) { return false; } // the number of single qoutes should always be an even number if (!SpoonFilter::isEven(substr_count($string, "'"))) { return false; } // first charachter should not be a number if (SpoonFilter::isInteger(substr($string, 2, 1))) { return false; } // square bracket followed by a dot is NOT allowed eg {option:variable[0].var} if (substr_count($string, '].') != 0) { return false; } // dot followed by a square bracket is NOT allowed eg {option:variable.['test']} if (substr_count($string, '.[') != 0) { return false; } // empty brackets are NOT allowed if (substr_count($string, '[]') != 0) { return false; } break; // option string // option string case 'option': // the number of square opening/closing brackets should be equal if (substr_count($string, '[') != substr_count($string, ']')) { return false; } // the number of single qoutes should always be an even number if (!SpoonFilter::isEven(substr_count($string, "'"))) { return false; } // square bracket followed by a dot is NOT allowed eg {option:variable[0].var} if (substr_count($string, '].') != 0) { return false; } // dot followed by a square bracket is NOT allowed eg {option:variable.['test']} if (substr_count($string, '.[') != 0) { return false; } // empty brackets are NOT allowed if (substr_count($string, '[]') != 0) { return false; } break; // variable string // variable string case 'variable': // the number of square opening/closing brackets should be equal if (substr_count($string, '[') != substr_count($string, ']')) { return false; } // the number of single qoutes should always be an even number if (!SpoonFilter::isEven(substr_count($string, "'"))) { return false; } // first charachter should not be a number if (SpoonFilter::isInteger(substr($string, 2, 1))) { return false; } // square bracket followed by a dot is NOT allowed eg {$variable[0].var} if (substr_count($string, '].') != 0) { return false; } // dot followed by a square bracket is NOT allowed eg {$variable.['test']} if (substr_count($string, '.[') != 0) { return false; } // empty brackets are NOT allowed if (substr_count($string, '[]') != 0) { return false; } break; } return true; }
public function testIsEven() { $this->assertTrue(SpoonFilter::isEven(0)); $this->assertFalse(SpoonFilter::isEven(1)); $this->assertTrue(SpoonFilter::isEven(10901920)); $this->assertFalse(SpoonFilter::isEven(-1337)); // I don't know man, semantically speaking, this bull shit, but it does // adhere to PHP's idiosyncratic casting rules. $this->assertTrue(SpoonFilter::isEven(array())); $this->assertFalse(SpoonFilter::isEven(array(2))); }
public function testIsEven() { $this->assertTrue(SpoonFilter::isEven(0)); $this->assertFalse(SpoonFilter::isEven(1)); $this->assertTrue(SpoonFilter::isEven(10901920)); $this->assertFalse(SpoonFilter::isEven(-1337)); }