diff --git a/core/lib/Thelia/Tests/Type/IntToCombinedStringsListTypeTest.php b/core/lib/Thelia/Tests/Type/IntToCombinedStringsListTypeTest.php new file mode 100755 index 000000000..de308694f --- /dev/null +++ b/core/lib/Thelia/Tests/Type/IntToCombinedStringsListTypeTest.php @@ -0,0 +1,64 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Tests\Type; + +use Thelia\Type\IntToCombinedStringsListType; + +/** + * + * @author Etienne Roudeix + * + */ +class IntToCombinedStringsListTypeTest extends \PHPUnit_Framework_TestCase +{ + public function testIntToCombinedStringsListType() + { + $type = new IntToCombinedStringsListType(); + $this->assertTrue($type->isValid('1: foo & bar | (fooo &baar), 4: *, 67: (foooo & baaar)')); + $this->assertFalse($type->isValid('1,2,3')); + } + + public function testFormatJsonType() + { + $type = new IntToCombinedStringsListType(); + $this->assertEquals( + $type->getFormattedValue('1: foo & bar | (fooo &baar), 4: *, 67: (foooo & baaar)'), + array( + 1 => array( + "values" => array('foo','bar','fooo','baar'), + "expression" => 'foo&bar|(fooo&baar)', + ), + 4 => array( + "values" => array('*'), + "expression" => '*', + ), + 67 => array( + "values" => array('foooo','baaar'), + "expression" => '(foooo&baaar)', + ), + ) + ); + $this->assertNull($type->getFormattedValue('foo')); + } +} diff --git a/core/lib/Thelia/Type/IntToCombinedStringsListType.php b/core/lib/Thelia/Type/IntToCombinedStringsListType.php new file mode 100755 index 000000000..985ef8ff6 --- /dev/null +++ b/core/lib/Thelia/Type/IntToCombinedStringsListType.php @@ -0,0 +1,122 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Type; + +/** + * + * @author Etienne Roudeix + * + */ + +class IntToCombinedStringsListType implements TypeInterface +{ + public function getType() + { + return 'Int to combined strings list type'; + } + + public function isValid($values) + { + foreach(explode(',', $values) as $intToCombinedStrings) { + $parts = explode(':', $intToCombinedStrings); + if(count($parts) != 2) + return false; + if(filter_var($parts[0], FILTER_VALIDATE_INT) === false) + return false; + + if(false === $this->checkLogicalFormat($parts[1])) + return false; + } + $x = 3; + return true; + } + + public function getFormattedValue($values) + { + if( $this->isValid($values) ) { + $return = ''; + + $values = preg_replace('#[\s]#', '', $values); + foreach(explode(',', $values) as $intToCombinedStrings) { + $parts = explode(':', $intToCombinedStrings); + + $return[trim($parts[0])] = array( + "values" => preg_split( "#(&|\|)#", preg_replace('#[\(\)]#', '', $parts[1])), + "expression" => $parts[1], + ); + } + + return $return; + } else { + return null; + } + } + + protected function checkLogicalFormat($string) + { + /* delete all spaces and parentheses */ + $noSpaceString = preg_replace('#[\s]#', '', $string); + $noParentheseString = preg_replace('#[\(\)]#', '', $noSpaceString); + + if(!preg_match('#^([a-zA-Z0-9_\-]+([\&\|][a-zA-Z0-9_\-]+)*|\*)$#', $noParentheseString)) + return false; + + /* check parenteses use */ + $openingParenthesesCount = 0; + $closingParenthesesCount = 0; + + for($i=0; $i