/** * @param string $configFile * @return Google\Analytics\QueryConfiguration */ public static function createFromXML($configFile) { if (!file_exists($configFile)) { throw new InvalidArgumentException($configFile . ' does not exist.'); } $xmlContent = file_get_contents($configFile); if ($xmlContent === false) { throw new RuntimeException('Unable to load XML from ' . $configFile . '.'); } $xml = new \DOMDocument(); if (!$xml->loadXML($xmlContent)) { throw new RuntimeException('Encountered error while parsing XML content.'); } $globalArgs = array(); $xPath = new \DOMXPath($xml); $result = $xPath->query('/conf'); if ($result->length != 1) { throw new UnexpectedValueException('The XML configuration file must contain exactly one <conf> ' . 'element at the outermost level.'); } $conf = $result->item(0); $result = $xPath->query('queries', $conf); if ($result->length != 1) { throw new UnexpectedValueException('The <conf> element in the XML configuration file must ' . 'contain exactly one <queries> element.'); } $queries = $xPath->query('query', $result->item(0)); $queryCount = $queries->length; if (!$queryCount) { throw new UnexpectedValueException('The <queries> element in the XML configuration must ' . 'contain at least one <query> element.'); } $config = new self(); // Any attribute on the conf element is a global configuration value foreach ($conf->attributes as $attName => $attVal) { $globalArgs[$attName] = $attVal->nodeValue; } // This isn't allowed, obviously if (isset($globalArgs['conf'])) { throw new UnexpectedValueException('Cannot specify an XML configuration file within an XML ' . 'configuration file.'); } $config->_validateArgs($globalArgs); /* Merge the global arguments with each query's arguments as we go through and build the queries. */ if ($queryCount == 1) { foreach ($queries->item(0)->attributes as $attName => $attVal) { $globalArgs[$attName] = $attVal->nodeValue; } $config->_query = self::_getQuery($globalArgs); } else { $r = new \ReflectionClass(__NAMESPACE__ . '\\GaDataQueryCollection'); $queryList = array(); for ($i = 0; $i < $queryCount; $i++) { $args = $globalArgs; foreach ($queries->item($i)->attributes as $attName => $attVal) { $args[$attName] = $attVal->nodeValue; } $queryList[] = self::_getQuery($args); } $config->_query = $r->newInstanceArgs($queryList); if (isset($globalArgs['group-name'])) { $config->_query->setName($globalArgs['group-name']); } } return $config; }