/** * {@inheritdoc} * * Source format: CSV * * <code> * $options = [ * 'delimiter' => ',', // typically ,|;|\t * 'enclosure' => '"', // typically "|' * 'escape' => '\\', // how the above is to be escaped * 'pivot' => false // pivot input array * ]; * </code> * * @param mixed $resource * @param array $options */ public function fromArray($resource, array $options = []) { $options = array_merge(['delimiter' => ',', 'enclosure' => '"', 'escape' => '\\', 'pivot' => false], $options); $resource = parent::getRealResource($resource, $options); if ($options['pivot']) { $resource = ArrayUtils::pivot($resource); } $dataDepth = self::verifyDataDepthLimit($resource, 2); if ($dataDepth === 1) { $resource = [$resource]; } else { if ($dataDepth > 2) { $msg = '$resource must have one or two levels'; if ($options['pivot']) { $msg .= ' after it has been pivoted'; } throw new JigException($msg); } } $csv = ''; foreach ($resource as $valueArr) { $line = ''; foreach ($valueArr as $value) { $line .= StringUtils::quote($value, $options) . $options['delimiter']; } $csv .= rtrim($line, $options['delimiter']) . PHP_EOL; } return $csv; }
/** * Use this to pass a bunch of data at once to the object * * @param $data, can be an array or a file of the types yaml, json or ini, PHP will be executed! * @param bool $override, by default all existing data are replaced */ public static function init($data, $override = true) { $settingsObj = self::getInstance(); if (is_string($data)) { ob_start(); include $data; switch (FsUtils::getFileExtension($data)) { case 'yaml': case 'yml': $data = Yaml::parse(ob_get_clean()); break; case 'json': $data = json_decode(ob_get_clean(), 1); break; case 'ini': $data = parse_ini_string(ob_get_clean()); break; } } if ($override) { $settingsObj->data = $data; } else { $settingsObj->data = ArrayUtils::arrayMergeRecursiveDistinct($settingsObj->data, $data); } }
/** * Decode a INI string to an array * * @param mixed $resource * @param array $options * @return array */ public function toArray($resource, array $options = []) { $options = array_merge(['process_sections' => true, 'scanner_mode' => INI_SCANNER_RAW], $options); return ArrayUtils::typecast(parse_ini_string(self::getRealResource($resource, $options), $options['process_sections'], $options['scanner_mode'])); }
/** * reads data from settings * * @code * self::read('foo.bar') * @endcode * * @param string $name the name of the variable to read * @return mixed the content of the requested variable or false if the variable does not exist */ public static function read($name) { $settingsObj = self::getInstance(); $value = ArrayUtils::getValue($settingsObj->data, $name); return is_null($value) ? false : $value; }
/** * Pivot an array * * @param array $resource * @return array */ public function fromArray(array $resource) { return ArrayUtils::pivot($resource); }