-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathBaseExample.php
More file actions
144 lines (132 loc) · 3.78 KB
/
BaseExample.php
File metadata and controls
144 lines (132 loc) · 3.78 KB
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
<?php
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Include the Ad Exchange Buyer service class and the HTML generation
* functions.
*/
require_once "../../src/Contrib/apiAdexchangebuyerService.php";
require_once "htmlHelper.php";
/**
* Base class for all examples, contains helper methods to support examples
* input and rendering results.
*
* @author David Torres <david.t@google.com>
*/
abstract class BaseExample {
protected $service;
/**
* Inject the dependency.
* @param apiAdexchangebuyerService $adExchangeBuyerService an authenticated
* instance of apiAdexchangebuyerService
*/
public function __construct(apiAdexchangebuyerService $service) {
$this->service = $service;
}
/**
* Contains the logic of the example.
*/
abstract protected function run();
/**
* Executes the example, checks if the examples requires parameters and
* request them before invoking run.
*/
public function execute() {
if (count($this->getInputParameters())) {
if ($this->isSubmitComplete()) {
$this->formValues = $this->getFormValues();
$this->run();
} else {
$this->renderInputForm();
}
} else {
$this->run();
}
}
/**
* Gives a display name of the example.
* To be implemented in the specific example class.
*/
abstract public function getName();
/**
* Returns the list of input parameters of the example.
* To be overriden by examples that require parameters.
*/
protected function getInputParameters() {
return array();
}
/**
* Renders an input form to capture the example parameters.
*/
protected function renderInputForm() {
$parameters = $this->getInputParameters();
if (count($parameters)) {
printf('<h2>Enter %s parameters</h2>', $this->getName());
print '<form method="POST"><fieldset>';
foreach ($parameters as $parameter) {
$name = $parameter['name'];
$display = $parameter['display'];
$currentValue = isset($_POST[$name]) ? $_POST[$name] : '';
printf('%s: <input name="%s" value="%s">', $display, $name,
$currentValue);
if ($parameter['required']) {
print '*';
}
print '</br>';
}
print '</fieldset>*required<br/>';
print '<input type="submit" name="submit" value="Submit"/>';
print '</form>';
}
}
/**
* Checks if the form has been submitted and all required parameters are
* set.
*/
protected function isSubmitComplete() {
if (!isset($_POST['submit'])) {
return false;
}
foreach ($this->getInputParameters() as $parameter) {
if ($parameter['required'] &&
empty($_POST[$parameter['name']])) {
return false;
}
}
return true;
}
/**
* Retrieves the submitted form values.
*/
protected function getFormValues() {
$input = array();
foreach ($this->getInputParameters() as $parameter) {
if (isset($_POST[$parameter['name']])) {
$input[$parameter['name']] = $_POST[$parameter['name']];
}
}
return $input;
}
/**
* Prints out the given result object.
* @param Array $result
*/
protected function printResult($result) {
printf('<pre>');
print_r($result);
printf('</pre>');
}
}