/Lib/googleads-php-lib/src/Google/Api/Ads/Dfp/Util/v201508/Pql.php

https://github.com/markvince/CakePHP-GAStats-Plugin · PHP · 174 lines · 89 code · 11 blank · 74 comment · 34 complexity · 459c8c5ddc14f50b82d27fbab948f179 MD5 · raw file

  1. <?php
  2. /**
  3. * A utility class for handling PQL objects.
  4. *
  5. * PHP version 5
  6. *
  7. * Copyright 2013, Google Inc. All Rights Reserved.
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. * @package GoogleApiAdsDfp
  22. * @subpackage Util
  23. * @category WebServices
  24. * @copyright 2013, Google Inc. All Rights Reserved.
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License,
  26. * Version 2.0
  27. */
  28. require_once 'Google/Api/Ads/Dfp/Util/v201508/DateTimeUtils.php';
  29. /**
  30. * A utility class for handling PQL objects.
  31. *
  32. * @package GoogleApiAdsDfp
  33. * @subpackage Util
  34. */
  35. class Pql {
  36. /**
  37. * {@link Pql} is meant to be used statically.
  38. */
  39. private function __construct() {}
  40. /**
  41. * Creates a {@link Value} from the value i.e., a {@link TextValue} for a
  42. * value of type {@code string}, {@link BooleanValue} for type {@code bool},
  43. * {@link NumberValue} for type {@code float}, or {@code int},
  44. * {@link DateTimeValue} for type {@link DfpDateTime}, {@link DateValue} for
  45. * type {@link Date}, and {@link SetValue} for type {@code array}. If the
  46. * value is a {@code Value}, the value is returned. If the value is
  47. * {@code null}, an empty {@link TextValue} is returned.
  48. *
  49. * @param mixed $value the value to convert
  50. * @return Value the constructed value of the appropriate type
  51. * @throws InvalidArgumentException if value cannot be converted
  52. */
  53. public static function CreateValue($value) {
  54. if ($value instanceof Value) {
  55. return $value;
  56. } else if ($value === null) {
  57. return new TextValue();
  58. } else {
  59. if (is_bool($value)) {
  60. return new BooleanValue($value);
  61. } else if (is_float($value) || is_int($value)) {
  62. return new NumberValue($value);
  63. } else if (is_string($value)) {
  64. return new TextValue($value);
  65. } else if ($value instanceof DfpDateTime) {
  66. return new DateTimeValue($value);
  67. } else if ($value instanceof Date) {
  68. return new DateValue($value);
  69. } else if (is_array($value)) {
  70. $setValue = new SetValue();
  71. $values = array();
  72. foreach ($value as $pqlValue) {
  73. $values[] = self::CreateValue($pqlValue);
  74. }
  75. $setValue->values = $values;
  76. return $setValue;
  77. } else if (class_exists('TargetingValue', false) &&
  78. $value instanceof Targeting) {
  79. return new TargetingValue($value);
  80. } else {
  81. throw new InvalidArgumentException(sprintf("Unsupported value type "
  82. . "[%s]", get_class($value)));
  83. }
  84. }
  85. }
  86. /**
  87. * Creates a String from the Value.
  88. *
  89. * @param Value $value the value to convert
  90. * @return string the string representation of the value
  91. * @throws InvalidArgumentException if value cannot be converted
  92. */
  93. public static function ToString(Value $value) {
  94. if ($value instanceof BooleanValue) {
  95. return ($value->value) ? 'true' : 'false';
  96. } else if ($value instanceof NumberValue || $value instanceof TextValue) {
  97. return strval($value->value);
  98. } else if ($value instanceof DateTimeValue) {
  99. return (isset($value->value))
  100. ? DateTimeUtils::ToStringWithTimeZone($value->value) : '';
  101. } else if ($value instanceof DateValue) {
  102. return DateTimeUtils::ToString($value->value);
  103. } else if ($value instanceof SetValue) {
  104. $pqlValues = $value->values;
  105. if (!isset($pqlValues)) {
  106. return '';
  107. } else {
  108. $valuesAsStrings = array();
  109. foreach ($pqlValues as $pqlValue) {
  110. $valuesAsStrings[] = self::ToString($pqlValue);
  111. }
  112. return implode(',', $valuesAsStrings);
  113. }
  114. } else {
  115. throw new InvalidArgumentException(sprintf("Unsupported Value type [%s]",
  116. get_class($value)));
  117. }
  118. }
  119. /**
  120. * Gets the column labels for the result set.
  121. *
  122. * @param ResultSet $resultSet the result set to get the column labels for
  123. * @return array the string list of column labels
  124. */
  125. public static function GetColumnLabels(ResultSet $resultSet) {
  126. $columnLabels = array();
  127. foreach ($resultSet->columnTypes as $columnType) {
  128. $columnLabels[] = $columnType->labelName;
  129. }
  130. return $columnLabels;
  131. }
  132. /**
  133. * Gets the values in a row of the result set in the form of a string list.
  134. *
  135. * @param Row $row the row to get the values for
  136. * @return array the string list of row labels
  137. */
  138. public static function GetRowStringValues(Row $row) {
  139. return array_map(array('Pql', 'ToString'), $row->values);
  140. }
  141. /**
  142. * Combines the first and second result sets, if and only if, the columns
  143. * of both result sets match.
  144. *
  145. * @throws InvalidArgumentException if the result sets to combine do not have
  146. * identical column headers
  147. */
  148. public static function CombineResultSets(ResultSet $first,
  149. ResultSet $second) {
  150. $firstColumns = self::GetColumnLabels($first);
  151. $secondColumns = self::GetColumnLabels($second);
  152. if ($firstColumns !== $secondColumns) {
  153. throw new InvalidArgumentException(sprintf("First result set columns "
  154. . "[%s] do not match second result set columns [%s]", implode(', ',
  155. $firstColumns), implode(', ', $secondColumns)));
  156. }
  157. $combinedRows = $first->rows;
  158. if (isset($second->rows)) {
  159. $combinedRows = array_merge($combinedRows, $second->rows);
  160. }
  161. return new ResultSet($first->columnTypes, $combinedRows);
  162. }
  163. }