/** * Helper method to create a single query instance from an associative * array of command-line arguments or XML configuration values. * * @param array $args * @return Google\Analytics\GaDataQuery */ private static function _getQuery(array $args) { if (!isset($args['profile-name']) && !isset($args['profile-id'])) { throw new InvalidArgumentException('A profile name or ID must be specified.'); } if (!isset($args['metric'])) { throw new InvalidArgumentException('At least one metric must be specified.'); } if (!isset($args['start-date']) || !isset($args['end-date'])) { throw new InvalidArgumentException('A start date and an end date must be specified.'); } // See whether we have to resolve date shortcuts $dateKeys = array('start-date', 'end-date'); foreach ($dateKeys as $key) { if (preg_match('/^[A-Z]+_[_A-Z]+_[A-Z]+$/', $args[$key])) { $r = new \ReflectionClass(__NAMESPACE__ . '\\GaDataQuery'); try { $args[$key] = $r->getConstant($args[$key]); } catch (\ReflectionException $e) { throw new InvalidArgumentException('"' . $args[$key] . '" is not a valid date shortcut.'); } } } if (isset($args['split-queries-by'])) { $interval = strtoupper($args['split-queries-by']); if ($interval != 'DAY' && $interval != 'WEEK' && $interval != 'MONTH' && $interval != 'YEAR') { throw new InvalidArgumentException('Queries may only be split by day, week, month, or year.'); } $q = new DateRangeGaDataQuery(null, $args['start-date'], $args['end-date'], new \DateInterval('P1' . $interval[0])); $q->setIterativeName(ucfirst(strtolower($interval))); if (isset($args['date-format-string'])) { $q->setFormatString($args['date-format-string']); } } else { $q = new GaDataQuery(); $q->setStartDate($args['start-date']); $q->setEndDate($args['end-date']); } if (isset($args['name'])) { $q->setName($args['name']); } if (isset($args['profile-id'])) { $q->setProfile($args['profile-id']); } else { $q->setProfileName($args['profile-name']); } $q->setMetrics(explode(',', $args['metric'])); if (isset($args['dimension'])) { $q->setDimensions(explode(',', $args['dimension'])); } if (isset($args['sort'])) { $sort = new GaDataSortOrder(); $sortStrings = explode(',', $args['sort']); foreach ($sortStrings as $sortString) { if (!strlen($sortString)) { continue; } if ($sortString[0] == '-') { $order = SORT_DESC; $sortString = substr($sortString, 1); } else { $order = SORT_ASC; } $sort->addField($sortString, $order); } $q->setSort($sort); } if (isset($args['limit'])) { $q->setTotalResults($args['limit']); } if (isset($args['filter'])) { $q->setFilter($args['filter']); } if (isset($args['segment'])) { $q->setSegment($args['segment']); } if (isset($args['sampling-level'])) { $q->setSamplingLevel(strtoupper($args['sampling-level'])); } return $q; }
/** * Tests to make sure the email subject line reflects the name property * where present. */ public function testEmailSubject() { $q = new GaDataQuery(); $q->setStartDate('2013-07-28'); $q->setEndDate('2013-11-03'); $profile = new ProfileSummary(); $profile->setID('123'); $profile->setName('Foo'); $q->setProfile($profile); $this->assertEquals('Google Analytics report for profile "Foo" for 2013-07-28 through 2013-11-03', $q->getEmailSubject()); $q->setName('Bar'); $this->assertEquals('Google Analytics report "Bar" for 2013-07-28 through 2013-11-03', $q->getEmailSubject()); }