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 #33 from kamil-klasicki/202010_fix_camelcasing
Browse files Browse the repository at this point in the history
202010 fix camelcasing issue inside Profile Model
  • Loading branch information
remstone7 authored Oct 14, 2020
2 parents 2f53894 + 20f4883 commit 3da6d90
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
## CHANGELOG

### 2.2.3
- Fix - ProfileModel file to convert specialAttributes properties to camel case before executing property_exists method inside jsonSerialize

### 2.2.2
- Fix - KlaviyoAPI file to handle json_decode correctly when empty string is returned
- Fix - Fix EventModel unix timestamp issue
- Fix - Fix EventModel unix timestamp issue
- Update - Add all Klaviyo special properties

### 2.2.1
Expand All @@ -26,4 +29,4 @@
- There is no compatibility on migrating from 1.* to 2.* and will need to follow the new 2.0.0 pattern

### 1.0
- As this changelog was created after 2.0.0, please refer to the README for version 1 https://github.com/klaviyo/php-klaviyo/tree/d388ca998dff55b2a7e420c2c7aa2cd221365d1c
- As this changelog was created after 2.0.0, please refer to the README for version 1 https://github.com/klaviyo/php-klaviyo/tree/d388ca998dff55b2a7e420c2c7aa2cd221365d1c
2 changes: 1 addition & 1 deletion 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.2.2';
const VERSION = '2.2.3';

/**
* Constructor for Klaviyo.
Expand Down
19 changes: 15 additions & 4 deletions src/Model/ProfileModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public function __construct( array $configuration ) {
protected function setAttributes(array $configuration ) {
foreach ( $configuration as $key => $value ) {
if ( $this->isSpecialAttribute($key) ) {
$this->{lcfirst(str_replace('_', '', ucwords(ltrim($key, '$'), '_')))} = $value;
$this->{$this->convertToCamelCase($key)} = $value;
}

}

$this->setCustomAttributes( $configuration );
Expand Down Expand Up @@ -150,14 +150,25 @@ public function getCustomAttributes() {
return $this->customAttributes;
}

/**
* The Special attributes array is using snake case, while class properties are camelCased.
* This means that variables such as first_name or last_name won't exist on the class object and will
* simply be missed inside jsonSerialize method. To accomodate for that, we convert all the keys to camelCase before running the comparison.
*
* @return string
*/
public function convertToCamelCase($key) {
return lcfirst(str_replace('_', '', ucwords(ltrim($key, '$'), '_')));
}

public function jsonSerialize() {
$properties = array_fill_keys($this::$specialAttributes, null);
foreach ($properties as $key => &$value) {
if (!property_exists($this, substr($key, 1))) {
if (!property_exists($this, $this->convertToCamelCase($key))) {
continue;
}

$value = $this->{substr($key, 1)};
$value = $this->{$this->convertToCamelCase($key)};
}

unset($properties['$email']);
Expand Down

0 comments on commit 3da6d90

Please sign in to comment.