-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProspect.class.php
134 lines (128 loc) · 3.75 KB
/
Prospect.class.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
<?php
/**
* This is a pet project of making some classes that interact with the Pardot API.
*
* @author stephenreid
*
* @since 7/13/2011
*/
class Prospect
{
private $connection = null;
//this stores our auto getters and setters.
private $data = array();
//a cached version of wht we go back to only save what has changed.
private $originalData = array();
/**
* Prospect()
* Constructs our prospect object
* This would typically be called $prospect = new Prospect();
* Can be called $prospect = new Prospect(array('email'=>'[email protected]",'first_name'=>'Stephen')); to create a prospect.
*
* @param unknown_type $arr
*/
public function __construct($arr = null)
{
if (is_array($arr)) { // they passed us all the variables for a prospect
foreach ($arr as $key => $value) {
$this->$key = $value;
}
}
//else we have a blank slate
}
/**
* getConnection
* Returns our cached connector if we have it
* Creates a new one otherwise.
*
* @return PardotConnector
*/
public function getConnection()
{
$connection = $this->connection;
if ($connection === null) {
$connection = new PardotConnector();
$connection->authenticate();
$this->connection = $connection;
}
return $connection;
}
/**
* save()
* Uses the current $data arr
* and stores the object to pardot.
*/
public function save()
{
//get our changeset
$changes = array_diff_assoc($this->data, $this->originalData);
if (!count($changes)) {
//if there are no changes then don't commit anything
return;
}
if (count($this->originalData)) {
//use an identifier if it already existed
$changes = array_merge(array('id' => $this->originalData['id']), $changes);
}
//authenticate
$connection = $this->getConnection();
//upsert
$connection->prospectUpsert($changes);
//update what our original data is
$this->originalData = $this->data;
}
/**
* fetchProspectByEmail.
*
* @desc Fetches a prospect from the Pardot API and returns a Prospect object;
*
* @param Str $email
*
* @return Prospect
* Ie: $prospect = prospect::fetchProspectByEmail('[email protected]');
* $prospect->company = 'New Company Value';
* $prospect->save();
*/
public function fetchProspectByEmail($email)
{
//authenticate
$conn = $this->getConnection();
//query
$p = $conn->prospectRead(array('email' => $email));
//localize
$prospect = new self();//initialize one of my own
$data = json_decode(json_encode($p), true);//convert everything to std
//store our previous cache
$this->originalData = $data;
//store our modifiable array;
$this->data = $data;
return $this;
}
/**
*setAssignedUser
*assigns this prospect to a user based on email or id.
*
*@param userIdentifier (an email address or an id)
*/
public function setAssignedUser($userIdentifier)
{
$connection = $this->getConnection();
$connection->assignProspect($this->id, $userIdentifier);
}
//These are all magic methods
public function __set($name, $value)
{
$this->data[$name] = $value;
}
public function __get($name)
{
if (isset($this->data[$name])) {
return $this->data[$name];
}
return;
}
public function __isset($name)
{
return isset($this->data[$name]);
}
}