Skip to content

Commit

Permalink
Make sure fields names are underscore format
Browse files Browse the repository at this point in the history
  • Loading branch information
jguittard committed Sep 12, 2015
1 parent f7cce98 commit d053eda
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Service/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,12 @@ public function create($data)
$data = ArrayUtils::iteratorToArray($data);
}

$data = (array)$data;

foreach ($data as $key => $value) {
$data[$this->fromCamelCase($key)] = $value;
}

$json = json_encode($data);

if (false === $json) {
Expand Down Expand Up @@ -369,6 +375,11 @@ public function update($id, $data)
if (!is_array($data)) {
// throw 422
}

foreach ($data as $key => $value) {
$data[$this->fromCamelCase($key)] = $value;
}

$fields = http_build_query($data);

$result = $this->request(self::ZOHO_API_ENDPOINT . $this->getPath() . '/' . $id, 'PUT', $fields);
Expand All @@ -386,4 +397,14 @@ public function delete($id)
$this->request(self::ZOHO_API_ENDPOINT . $this->getPath() . '/' . $id, 'DELETE');
return true;
}

protected function fromCamelCase($input)
{
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
$ret = $matches[0];
foreach ($ret as &$match) {
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
}
return implode('_', $ret);
}
}

0 comments on commit d053eda

Please sign in to comment.