/** * Parses supplied INI contents in to a PHP array. * * @param string $contents INI contents to parse. * @return array * @static */ public static function parse($contents) { return \October\Rain\Parse\Ini::parse($contents); }
public function testComplex() { $path = __DIR__ . '/../fixtures/parse/complex.ini'; $this->assertFileExists($path); $content = $this->getContents($path); $vars = ['firstLevelValue' => 'relax', 'firstLevelArray' => ['foo', 'bar'], 'someComponent' => ['secondLevelArray' => ['hello', 'world'], 'name' => ['title' => 'column_name_name', 'validation' => ['required' => ['message' => 'column_name_required'], 'regex' => ['pattern' => '^[0-9_a-z]+$', 'message' => 'column_validation_title']]], 'type' => ['title' => 'column_name_type', 'type' => 'dropdown', 'options' => ['integer' => 'Integer', 'smallInteger' => 'Small Integer', 'bigInteger' => 'Big Integer', 'date' => 'Date', 'time' => 'Time', 'dateTime' => 'Date and Time', 'timestamp' => 'Timestamp', 'string' => 'String', 'text' => 'Text', 'binary' => 'Binary', 'boolean' => 'Boolean', 'decimal' => 'Decimal', 'double' => 'Double'], 'validation' => ['required' => ['message' => 'column_type_required']]], 'modes' => ['title' => 'column_name_type', 'type' => 'checkboxlist', 'options' => [12, 34, 56, 78, 99]], 'security' => ['title' => 'column_name_security', 'type' => 'radio', 'options' => ['all' => ['All', 'Everyone'], 'users' => ['Users', 'Users only'], 'guests' => ['Guests', 'Guests only']]], 'length' => ['title' => 'column_name_length', 'validation' => ['regex' => ['pattern' => '(^[0-9]+$)|(^[0-9]+,[0-9]+$)', 'message' => 'column_validation_length']]], 'unsigned' => ['title' => 'column_name_unsigned', 'type' => 'checkbox'], 'allow_null' => ['title' => 'column_name_nullable', 'type' => 'checkbox'], 'auto_increment' => ['title' => 'column_auto_increment', 'type' => 'checkbox'], 'primary_key' => ['title' => 'column_auto_primary_key', 'type' => 'checkbox', 'width' => '50px'], 'default' => ['title' => 'column_default']]]; $parser = new IniParser(); $result = $parser->parse($content); $this->assertEquals($vars, $result); $result = $parser->render($vars); $this->assertEquals($content, $result); }
/** * Parses a CMS object file content. * The expected file format is following: * <pre> * INI settings section * == * PHP code section * == * Twig markup section * </pre> * If the content has only 2 sections they are considered as settings and Twig. * If there is only a single section, it is considered as Twig. * @param string $content Specifies the file content. * @return array Returns an array with the following indexes: 'settings', 'markup', 'code'. * The 'markup' and 'code' elements contain strings. The 'settings' element contains the * parsed INI file as array. If the content string doesn't contain a section, the corresponding * result element has null value. */ public static function parse($content, $options = []) { extract(array_merge(['isCompoundObject' => true], $options)); $result = ['settings' => [], 'code' => null, 'markup' => null]; if (!$isCompoundObject || !strlen($content)) { return $result; } $iniParser = new Ini(); $sections = preg_split('/^={2,}\\s*/m', $content, -1); $count = count($sections); foreach ($sections as &$section) { $section = trim($section); } if ($count >= 3) { $result['settings'] = @$iniParser->parse($sections[0], true) ?: [self::ERROR_INI => $sections[0]]; $result['code'] = $sections[1]; $result['code'] = preg_replace('/^\\s*\\<\\?php/', '', $result['code']); $result['code'] = preg_replace('/^\\s*\\<\\?/', '', $result['code']); $result['code'] = preg_replace('/\\?\\>\\s*$/', '', $result['code']); $result['code'] = trim($result['code'], PHP_EOL); $result['markup'] = $sections[2]; } elseif ($count == 2) { $result['settings'] = @$iniParser->parse($sections[0], true) ?: [self::ERROR_INI => $sections[0]]; $result['markup'] = $sections[1]; } elseif ($count == 1) { $result['markup'] = $sections[0]; } return $result; }