Skip to content

Commit

Permalink
Implemented dot setters & getters
Browse files Browse the repository at this point in the history
  • Loading branch information
joostbaptist committed Dec 6, 2018
1 parent 4d26092 commit e49a8ad
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/Requests/AddInteractionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@ public function getPayload(): ?array

public function setAccountId(int $accountId): self
{
return $this->setData('account_id', $accountId);
return $this->setData('account.id', $accountId);
}

public function getAccountId(): int
{
return $this->getData('account_id');
return $this->getData('account.id');
}

public function setSubjectId(int $subjectId): self
{
return $this->setData('subject_id', $subjectId);
return $this->setData('subject.id', $subjectId);
}

public function getSubjectId(): int
{
return $this->getData('subject_id');
return $this->getData('subject.id');
}

public function setSubjectTitle(string $subjectTitle): self
{
return $this->setData('subject_title', $subjectTitle);
return $this->setData('subject.title', $subjectTitle);
}

public function getSubjectTitle(): string
{
return $this->getData('subject_title');
return $this->getData('subject.title');
}

public function setDescription(string $description): self
Expand Down Expand Up @@ -89,11 +89,11 @@ public function getFinished(): bool

public function setAssignedUserId(int $assignedUserId): self
{
return $this->setData('assigned_user_id', $assignedUserId);
return $this->setData('assigned_user.id', $assignedUserId);
}

public function getAssignedUserId(): int
{
return $this->getData('assigned_user_id');
return $this->getData('assigned_user.id');
}
}
46 changes: 41 additions & 5 deletions src/Requests/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,56 @@ abstract class Request extends BaseRequest
protected $data = [];

/**
* @var mixed $data
* @param mixed $value
*/
public function setData(string $key, $data): self
public function setData(string $key, $value): self
{
$this->data[$key] = $data;
if ($key === null) {
$this->data = $value;

return $this;
}

$data = &$this->data;
$keys = explode('.', $key);

while (count($keys) > 1) {
$key = array_shift($keys);

if (!isset($data[$key]) || !is_array($data[$key])) {
$data[$key] = [];
}

$data = &$data[$key];
}

$data[array_shift($keys)] = $value;

return $this;
}

/**
* @return mixed
*/
public function getData(string $key)
public function getData(string $key = null)
{
return $this->data[$key] ?? null;
if ($key === null) {
return $this->data;
}

$data = $this->data;
$keys = explode('.', $key);

while (count($keys) > 0) {
$key = array_shift($keys);

if (isset($data[$key]) && (is_array($data[$key]) || count($keys) == 0)) {
$data = $data[$key];
} else {
$data = null;
}
}

return $data;
}
}

0 comments on commit e49a8ad

Please sign in to comment.