/** * @param string $filename * @param string $configJson * @param bool|null $merge */ public function setConfig($filename, $configJson, $merge = null) { $merge = (bool) $merge; $configJson = (object) CM_Util::jsonDecode($configJson); $configFile = new CM_File(DIR_ROOT . 'resources/config/' . $filename . '.php'); $config = new CM_Config_Node(); if ($merge && $configFile->exists()) { $config->extendWithFile($configFile); } $config->extendWithConfig($configJson); $configStr = $config->exportAsString('$config'); $indentation = ' '; $indent = function ($content) use($indentation) { return preg_replace('/(:?^|[\\n])/', '$1' . $indentation, $content); }; $configFile->ensureParentDirectory(); $configFile->write(join(PHP_EOL, ['<?php', '// This is autogenerated config file. You should not change it manually.', '', 'return function (CM_Config_Node $config) {', $indent($configStr), '};', ''])); $this->_getStreamOutput()->writeln('Created `' . $configFile->getPath() . '`'); }
/** * @param string $value * @return mixed * @throws CM_Exception_Invalid */ public static function jsonDecode($value) { return CM_Util::jsonDecode($value); }
/** * @param string $conversionJson * @return CMService_AdWords_Conversion */ public static function fromJson($conversionJson) { $conversionJson = (string) $conversionJson; return self::fromArray(CM_Util::jsonDecode($conversionJson)); }
/** * @expectedException CM_Exception_Invalid */ public function testJsonDecodeInvalid() { CM_Util::jsonDecode('{[foo:bar)}'); }
/** * @param string $key * @param mixed $value * @return mixed * @throws CM_Exception_Invalid * @throws CM_Model_Exception_Validation */ public function decodeField($key, $value) { $key = (string) $key; if ($this->hasField($key)) { $schemaField = $this->_schema[$key]; if (null !== $value) { $type = isset($schemaField['type']) ? $schemaField['type'] : null; if (null !== $type) { switch ($type) { case 'integer': case 'int': $value = (int) $value; break; case 'float': $value = (double) $value; break; case 'string': $value = (string) $value; break; case 'boolean': case 'bool': $value = (bool) $value; break; case 'array': break; case 'DateTime': $value = DateTime::createFromFormat('U', $value); break; default: if (!class_exists($type)) { throw new CM_Model_Exception_Validation('Field type is not a valid class/interface', null, ['type' => $type]); } $className = $type; if (is_a($className, 'CM_Model_Abstract', true)) { /** @var CM_Model_Abstract $type */ if ($this->_isJson($value)) { $value = CM_Util::jsonDecode($value); } $id = $value; if (!is_array($id)) { $id = ['id' => $id]; } $value = CM_Model_Abstract::factoryGeneric($type::getTypeStatic(), $id); } elseif (is_subclass_of($className, 'CM_ArrayConvertible', true)) { /** @var CM_ArrayConvertible $className */ $value = CM_Util::jsonDecode($value); $value = $className::fromArray($value); } else { throw new CM_Model_Exception_Validation('Class is neither CM_Model_Abstract nor CM_ArrayConvertible', null, ['className' => $className]); } if (!$value instanceof $className) { throw new CM_Model_Exception_Validation('Value is not an instance of the class', null, ['value' => CM_Util::var_line($value), 'className' => $className]); } } } } } return $value; }