示例#1
0
文件: Ini.php 项目: bruensicke/radium
 /**
  * returns rendered content
  *
  * @param string $content input content
  * @param string $data field to retrieve from configuration
  * @param array $options an array with additional options
  * @return string content as given
  * @filter
  */
 public function get($content, $data = null, array $options = array())
 {
     $defaults = array('default' => null, 'flat' => false);
     $options += $defaults;
     $params = compact('content', 'data', 'options');
     return $this->_filter(__METHOD__, $params, function ($self, $params) {
         extract($params);
         try {
             $config = IniFormat::parse($content);
         } catch (IniFormatException $e) {
             return $options['default'];
         } catch (Exception $e) {
             return $options['default'];
         }
         if (empty($data)) {
             return $options['flat'] ? Set::flatten($config) : $config;
         }
         if (is_scalar($data) && isset($config[$data])) {
             return $config[$data];
         }
         $data = '/' . str_replace('.', '/', (string) $data) . '/.';
         $result = current(Set::extract((array) $config, $data));
         if (!empty($result)) {
             return $result;
         }
         return $options['default'];
     });
 }
示例#2
0
 public function testWeirdValues()
 {
     $data = "name=foo\n//slug=bar";
     $expected = array('name' => 'foo');
     $this->assertEqual($expected, IniFormat::parse($data));
     $data = "name=foo\n//strange comment with (brackets)";
     $expected = array('name' => 'foo');
     $this->assertEqual($expected, IniFormat::parse($data));
 }
示例#3
0
 /**
  * allows easy output of IniFormat into a property
  *
  * @param object $entity instance of current Record
  * @param string $field name of property to retrieve data for
  * @return array an empty array in case of errors or the saved data decoded
  * @filter
  */
 public function _ini($entity, $field)
 {
     $params = compact('entity', 'field');
     return $this->_filter(get_called_class() . '::_ini', $params, function ($self, $params) {
         extract($params);
         if (empty($entity->{$field})) {
             return array();
         }
         $data = IniFormat::parse($entity->{$field});
         if (!is_array($data)) {
             return array();
         }
         return $data;
     });
 }