Skip to content
This repository has been archived by the owner on Aug 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #24 from klaviyo/202009_remove_typing
Browse files Browse the repository at this point in the history
Support older versions of php (5.4-5.6) & 7.X
  • Loading branch information
remstone7 authored Sep 16, 2020
2 parents 8d73402 + e345d8c commit 07905cf
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 436 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## CHANGELOG

### 2.2.0
- Fix - EventModel configuration to handle null dates
- Fix - PHP compatibility >= 5.4

### 2.1.1
- Fix - EventModel configuration to use DateTime correctly
- Fix - ProfileModel configuration to set special attributes correctly
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
},
"require-dev": {
"phpunit/phpunit": "^9",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions src/Klaviyo.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Klaviyo
/**
* @var string
*/
const VERSION = '2.1.1';
const VERSION = '2.2.0';

/**
* Constructor for Klaviyo.
Expand All @@ -38,15 +38,15 @@ public function __construct($private_key, $public_key)
/**
* @return string
*/
public function getPrivateKey(): string
public function getPrivateKey()
{
return $this->private_key;
}

/**
* @return string
*/
public function getPublicKey(): string
public function getPublicKey()
{
return $this->public_key;
}
Expand Down
26 changes: 13 additions & 13 deletions src/KlaviyoAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,9 @@ private function request( $method, $path, $options, $isPublic = false, $isV1 = f
$curl = curl_init();
curl_setopt_array($curl, $setopt_array);

$response= curl_exec($curl);
$statusCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
$response = curl_exec($curl);
$phpVersionHttpCode = version_compare(phpversion(), '5.5.0', '>') ? CURLINFO_RESPONSE_CODE : CURLINFO_HTTP_CODE;
$statusCode = curl_getinfo($curl, $phpVersionHttpCode);
curl_close($curl);

return $this->handleResponse( $response, $statusCode, $isPublic );
Expand Down Expand Up @@ -329,7 +330,7 @@ private function decodeJsonResponse( $response )
* @param string $paramName Name of API Param to create
* @param $paramValue Value of API params to create
*/
protected function createParams( string $paramName, $paramValue )
protected function createParams( $paramName, $paramValue )
{
return [self::JSON =>
[$paramName => $paramValue]
Expand Down Expand Up @@ -379,12 +380,11 @@ protected function setSinceParameter( $since, $uuid )
* @param array $params
* @return array
*/
protected function filterParams( array $params )
protected function filterParams( $params )
{
return array_filter(
$params,
function ( $key ){ return !is_null( $key ); },
ARRAY_FILTER_USE_BOTH
function ( $key ){ return !is_null( $key ); }
);
}

Expand All @@ -394,7 +394,7 @@ function ( $key ){ return !is_null( $key ); },
* @param array $params
* @return array[]
*/
protected function createRequestBody( array $params )
protected function createRequestBody( $params )
{
return array(
'form_params' => $params
Expand All @@ -407,7 +407,7 @@ protected function createRequestBody( array $params )
* @param array $params
* @return array[]
*/
protected function createRequestJson( array $params)
protected function createRequestJson( $params )
{
return array(
'json' => $params
Expand All @@ -420,13 +420,13 @@ protected function createRequestJson( array $params)
* @param array $profiles
* @throws KlaviyoException
*/
protected function checkProfile( array $profiles )
protected function checkProfile( $profiles )
{
foreach ( $profiles as $profile ) {
if ( ! $profile instanceof ProfileModel ) {
throw new KlaviyoException( sprintf( " %s is not an instance of %s, You must identify the person by their email, using a \$email key, or a unique identifier, using a \$id.",
$profile['$email'],
ProfileModel::class )
throw new KlaviyoException( sprintf( " %s is not an instance of ProfileModel, You must identify the person by their email, using a \$email key, or a unique identifier, using a \$id.",
$profile['$email']
)
);
}
}
Expand Down Expand Up @@ -495,7 +495,7 @@ protected function getSpecificCurlOptions($options)
* @param array $headers
* @return array
*/
protected function formatCurlHeaders(array $headers)
protected function formatCurlHeaders( $headers )
{
$formatted = array();

Expand Down
57 changes: 28 additions & 29 deletions src/Lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class Lists extends KlaviyoAPI
/**
* List endpoint constants
*/
const EXCLUSIONS = 'exclusions';
const GROUP = 'group';
const LIST = 'list';
const LISTS = 'lists';
const MEMBERS = 'members';
const SEGMENT = 'segment';
const SUBSCRIBE = 'subscribe';
const ENDPOINT_EXCLUSIONS = 'exclusions';
const ENDPOINT_GROUP = 'group';
const ENDPOINT_LIST = 'list';
const ENDPOINT_LISTS = 'lists';
const ENDPOINT_MEMBERS = 'members';
const ENDPOINT_SEGMENT = 'segment';
const ENDPOINT_SUBSCRIBE = 'subscribe';

/**
* Lists API arguments
Expand All @@ -41,7 +41,7 @@ public function createList( $listName )
{
$options = $this->createParams(self::LIST_NAME, $listName);

return $this->v2Request( self::LISTS, $options, self::HTTP_POST );
return $this->v2Request( self::ENDPOINT_LISTS, $options, self::HTTP_POST );
}

/**
Expand All @@ -52,7 +52,7 @@ public function createList( $listName )
*/
public function getLists() {

return $this->v2Request( self::LISTS );
return $this->v2Request( self::ENDPOINT_LISTS );
}

/**
Expand All @@ -66,7 +66,7 @@ public function getLists() {
*/
public function getListDetails( $listId )
{
$path = sprintf( '%s/%s', self::LIST, $listId );
$path = sprintf( '%s/%s', self::ENDPOINT_LIST, $listId );
return $this->v2Request( $path );
}

Expand All @@ -88,7 +88,7 @@ public function updateListDetails( $listId, $list_name )
self::LIST_NAME => $list_name
) );

$path = sprintf( '%s/%s', self::LIST, $listId );
$path = sprintf( '%s/%s', self::ENDPOINT_LIST, $listId );

return $this->v2Request( $path, $params, self::HTTP_PUT );
}
Expand All @@ -104,7 +104,7 @@ public function updateListDetails( $listId, $list_name )
*/
public function deleteList( $listId )
{
$path = sprintf( '%s/%s', self::LIST, $listId );
$path = sprintf( '%s/%s', self::ENDPOINT_LIST, $listId );
return $this->v2Request( $path, [], self::HTTP_DELETE );
}

Expand All @@ -124,7 +124,7 @@ public function deleteList( $listId )
* @return bool|mixed
* @throws Exception\KlaviyoException
*/
public function subscribeMembersToList( $listId, array $profiles )
public function subscribeMembersToList( $listId, $profiles )
{
$this->checkProfile( $profiles );

Expand All @@ -134,7 +134,7 @@ function( $profile ) {
}, $profiles
);

$path = sprintf( '%s/%s/%s', self::LIST, $listId, self::SUBSCRIBE );
$path = sprintf( '%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_SUBSCRIBE );
$params = $this->createParams( self::PROFILES, $profiles );

return $this->v2Request( $path, $params, self::HTTP_POST );
Expand All @@ -159,7 +159,7 @@ function( $profile ) {
*
* @return bool|mixed
*/
public function checkListSubscriptions ($listId, array $emails = null, array $phoneNumbers = null, array $pushTokens = null )
public function checkListSubscriptions ($listId, $emails = null, $phoneNumbers = null, $pushTokens = null )
{
$params = $this->createRequestJson(
$this->filterParams(
Expand All @@ -171,7 +171,7 @@ public function checkListSubscriptions ($listId, array $emails = null, array $ph
)
);

$path = sprintf('%s/%s/%s', self::LIST, $listId, self::SUBSCRIBE );
$path = sprintf('%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_SUBSCRIBE );

return $this->v2Request( $path, $params, self::HTTP_GET );
}
Expand All @@ -188,7 +188,7 @@ public function checkListSubscriptions ($listId, array $emails = null, array $ph
*
* @return bool|mixed
*/
public function unsubscribeMembersFromList( $listId, array $emails )
public function unsubscribeMembersFromList( $listId, $emails )
{
$params = $this->createRequestJson(
$this->filterParams(
Expand All @@ -198,7 +198,7 @@ public function unsubscribeMembersFromList( $listId, array $emails )
)
);

$path = sprintf('%s/%s/%s', self::LIST, $listId, self::SUBSCRIBE );
$path = sprintf('%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_SUBSCRIBE );

return $this->v2Request( $path, $params, self::HTTP_DELETE );
}
Expand All @@ -220,7 +220,7 @@ public function unsubscribeMembersFromList( $listId, array $emails )
*
* @throws Exception\KlaviyoException
*/
public function addMembersToList( $listId, array $profiles )
public function addMembersToList( $listId, $profiles )
{
$this->checkProfile( $profiles );

Expand All @@ -230,7 +230,7 @@ function( $profile ) {
}, $profiles
);

$path = sprintf( '%s/%s/%s', self::LIST, $listId, self::MEMBERS );
$path = sprintf( '%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_MEMBERS );
$options = $this->createParams( self::PROFILES, $profiles );

return $this->v2Request( $path, $options, self::HTTP_POST );
Expand All @@ -255,7 +255,7 @@ function( $profile ) {
*
* @return bool|mixed
*/
public function checkListMembership( $listId, array $emails = null, array $phoneNumbers = null, array $pushTokens = null )
public function checkListMembership( $listId, $emails = null, $phoneNumbers = null, $pushTokens = null )
{
$params = $this->createRequestJson(
$this->filterParams(
Expand All @@ -267,7 +267,7 @@ public function checkListMembership( $listId, array $emails = null, array $phone
)
);

$path = sprintf('%s/%s/%s', self::LIST, $listId, self::MEMBERS );
$path = sprintf( '%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_MEMBERS );

return $this->v2Request( $path, $params, self::HTTP_GET );
}
Expand All @@ -284,7 +284,7 @@ public function checkListMembership( $listId, array $emails = null, array $phone
*
* @return bool|mixed
*/
public function removeMembersFromList( $listId, array $emails )
public function removeMembersFromList( $listId, $emails )
{
$params = $this->createRequestJson(
$this->filterParams(
Expand All @@ -294,7 +294,7 @@ public function removeMembersFromList( $listId, array $emails )
)
);

$path = sprintf('%s/%s/%s', self::LIST, $listId, self::MEMBERS );
$path = sprintf('%s/%s/%s', self::ENDPOINT_LIST, $listId, self::ENDPOINT_MEMBERS );

return $this->v2Request( $path, $params, self::HTTP_DELETE );
}
Expand All @@ -312,7 +312,7 @@ public function removeMembersFromList( $listId, array $emails )
*
* @return bool|mixed
*/
public function getAllExclusionsOnList( $listId, int $marker = null )
public function getAllExclusionsOnList( $listId, $marker = null )
{
$params = $this->createRequestBody(
$this->filterParams(
Expand All @@ -322,7 +322,7 @@ public function getAllExclusionsOnList( $listId, int $marker = null )
)
);

$path = sprintf('%s/%s/%s/%s',self::LIST, $listId, self::EXCLUSIONS, 'all' );
$path = sprintf( '%s/%s/%s/%s',self::ENDPOINT_LIST, $listId, self::ENDPOINT_EXCLUSIONS, 'all' );

return $this->v2Request( $path, $params );
}
Expand All @@ -340,7 +340,7 @@ public function getAllExclusionsOnList( $listId, int $marker = null )
*
* @return bool|mixed
*/
public function getGroupMemberIdentifiers( $groupId, int $marker = null )
public function getGroupMemberIdentifiers( $groupId, $marker = null )
{
$params = $this->createRequestBody(
$this->filterParams(
Expand All @@ -350,8 +350,7 @@ public function getGroupMemberIdentifiers( $groupId, int $marker = null )
)
);

$path = sprintf('%s/%s/%s/%s',self::GROUP, $groupId, self::MEMBERS, 'all' );

$path = sprintf( '%s/%s/%s/%s',self::ENDPOINT_GROUP, $groupId, self::ENDPOINT_MEMBERS, 'all' );
return $this->v2Request( $path, $params );
}
}
11 changes: 5 additions & 6 deletions src/Metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Metrics extends KlaviyoAPI
*
* @return mixed
*/
public function getMetrics( int $page = null, int $count = null )
public function getMetrics( $page = null, $count = null )
{
$params = $this->filterParams( array(
self::PAGE=> $page,
Expand Down Expand Up @@ -66,7 +66,7 @@ public function getMetrics( int $page = null, int $count = null )
*
* @return bool|mixed
*/
public function getMetricsTimeline($since = null, string $uuid = null, int $count = null, string $sort = null )
public function getMetricsTimeline( $since = null, $uuid = null, $count = null, $sort = null )
{
$params = $this->setSinceParameter( $since, $uuid );

Expand All @@ -80,7 +80,7 @@ public function getMetricsTimeline($since = null, string $uuid = null, int $coun

$path = sprintf( '%s/%s', self::METRICS, self::TIMELINE );

return $this->v1Request($path, $params );
return $this->v1Request( $path, $params );

}

Expand All @@ -107,7 +107,7 @@ public function getMetricsTimeline($since = null, string $uuid = null, int $coun
* @return bool|mixed
*
*/
public function getMetricTimeline( string $metricID, $since = null, $uuid = null, int $count = null, string $sort = null)
public function getMetricTimeline( $metricID, $since = null, $uuid = null, $count = null, $sort = null )
{
$params = $this->setSinceParameter( $since, $uuid );

Expand All @@ -120,7 +120,6 @@ public function getMetricTimeline( string $metricID, $since = null, $uuid = null
) );

$path = sprintf( '%s/%s/%s', self::METRIC, $metricID, self::TIMELINE );

return $this->v1Request( $path, $params );
}

Expand Down Expand Up @@ -172,7 +171,7 @@ public function exportMetricData( $metricID,
)
);

$path = sprintf('%s/%s/%s', self::METRIC, $metricID, self::EXPORT );
$path = sprintf( '%s/%s/%s', self::METRIC, $metricID, self::EXPORT );

return $this->v1Request( $path, $params );

Expand Down
Loading

0 comments on commit 07905cf

Please sign in to comment.