/
Fields.php
168 lines (143 loc) · 3.52 KB
/
Fields.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Project: google-ads.
* User: Edujugon
* Email: edujugon@gmail.com
* Date: 10/2/17
* Time: 9:07
*/
namespace Edujugon\GoogleAds\Reports;
use Edujugon\GoogleAds\Session\AdwordsSession;
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\v201702\cm\ReportDefinitionService;
class Fields
{
/**
* Adwords Session
*
* @var mixed|null
*/
protected $session;
/**
* @var AdWordsServices
*/
protected $adWordsServices;
/**
* @var \Google\AdsApi\Common\AdsSoapClient|\Google\AdsApi\Common\SoapClient
*/
protected $reportDefinitionService;
/**
* @var
*/
protected $reportDefinitionFields;
/**
* Fields with type and accepted values.
*
* @var \stdClass
*/
protected $obj;
/**
* Fields constructor.
* @param null $session
*/
function __construct($session = null)
{
$this->adWordsServices = new AdWordsServices();
$this->session = $session ? $session : (new AdwordsSession())->oAuth()->build();
$this->reportDefinitionService = $this->adWordsServices->get($this->session, ReportDefinitionService::class);
$this->obj = new \stdClass();
}
/**
* Load the fields of the passed reportType
*
* @param $reportType
* @return $this
*/
public function of($reportType)
{
$this->validateReportType($reportType);
$this->reportDefinitionFields = $this->reportDefinitionService->getReportFields($reportType);
$this->obj = $this->createObj($this->reportDefinitionFields);
return $this;
}
/**
* Get fields as object
*
* The properties will be the fields name.
* Each property is an array with type and accepted values.
*
* @return \stdClass
*/
public function asObj()
{
return $this->obj;
}
/**
* Get array of fields names.
*
* @return array
*/
public function asList()
{
return array_keys(get_object_vars($this->obj));
}
/**
* Get a query selector of all filters.
* E.g. Id,Name,Description...
*
* @return string
*/
public function asQuerySelector(){
return implode(',',$this->asList());
}
/**
* Get all report types
*
* @return array
*/
public function reportTypes()
{
return ReportTypes::getList();
}
/**
* Pull fields from the list.
*
* @param array $list
* @return $this
*/
public function except(array $list)
{
foreach ($list as $field)
{
if(property_exists($this->obj,$field))
unset($this->obj->$field);
}
return $this;
}
/**
* Create a stdClass object with filter data.
*
* @param $reportDefinitionFields
* @return \stdClass
*/
private function createObj($reportDefinitionFields)
{
$obj = new \stdClass();
foreach ($reportDefinitionFields as $reportDefinitionField) {
$name = $reportDefinitionField->getFieldName();
$obj->$name = [
'type' => $reportDefinitionField->getFieldType(),
'values' => ($reportDefinitionField->getEnumValues() !== null) ? $reportDefinitionField->getEnumValues() : null,
];
}
return $obj;
}
/**
* @param $type
*/
private function validateReportType($type)
{
if(!ReportTypes::exist($type))
ReportTypes::invalidType();
}
}