Skip to content
This repository was archived by the owner on Jun 21, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/Notion/ObjectBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct($data, $notion)
{
$this->notion = $notion;

if (! $data) {
if (!$data) {
return;
}

Expand All @@ -55,8 +55,8 @@ protected function handleResponse($data)

protected function setProperties($data): void
{
$this->id = $data->id;
$this->created_time = $data->created_time;
$this->id = $data->id;
$this->created_time = $data->created_time;
$this->last_edited_time = $data->last_edited_time;

if (isset($data->archived)) {
Expand All @@ -65,7 +65,8 @@ protected function setProperties($data): void

if (property_exists($data, 'properties')) {
foreach ($data->properties as $label => $property) {
$this->properties[Str::camel($label)] = $this->createNewProperty($label, $property);
$this->properties[sha1($label)] = $this->createNewProperty($label, $property);
$this->propertiesCamelCaseAliases[Str::camel($label)] = sha1($label);
}
}

Expand Down
40 changes: 31 additions & 9 deletions src/Notion/Objects/Page.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Notion\Objects;

use Illuminate\Support\Str;
use Notion\ObjectBase;
use Notion\Http\Response;

Expand All @@ -9,6 +10,8 @@ class Page extends ObjectBase

protected $properties = [];

protected $propertiesCamelCaseAliases = [];

protected $parent;

protected $children = [];
Expand Down Expand Up @@ -65,7 +68,7 @@ public function prepareForRequest()
}
}

ray($data);
if (function_exists('ray')) ray($data);

return $data;
}
Expand All @@ -74,32 +77,51 @@ public function initProperties($data): self
{
$this->properties = $data;

// generate aliases for camelCase properties
foreach ($data as $sha1key => $property) {
$this->propertiesCamelCaseAliases[Str::camel($property->name)] = sha1($property->name);
}

return $this;
}

public function __get($property)
{
if (!isset($this->properties[$property])) {
return $this->$property;
// search propery by sha1 key
if (isset($this->properties[sha1($property)])) {
return $this->properties[sha1($property)]->value();
}

// fallback for camelCase alias
if(isset($this->propertiesCamelCaseAliases[$property]) && isset($this->properties[$this->propertiesCamelCaseAliases[$property]])) {
return $this->properties[$this->propertiesCamelCaseAliases[$property]]->value();
}

return $this->properties[$property]
->value();
// other cases- return class propery
return $this->$property;
}

public function __set($property, $value)
{
if (!isset($this->properties[$property])) {
$this->$property = $value;
// if we have this property in our sha1-keyed array
if (isset($this->properties[sha1($property)])) {
$this->properties[sha1($property)]->set($value);
return;
}

// else check if it is camelCase alias
if(isset($this->propertiesCamelCaseAliases[$property]) && isset($this->properties[$this->propertiesCamelCaseAliases[$property]])) {
$this->properties[$this->propertiesCamelCaseAliases[$property]]->set($value);
return;
}

$this->properties[$property]->set($value);
// set class property in other cases
$this->$property = $value;
}

public function __isset($property)
{
return isset($this->properties[$property]);
return isset($this->properties[sha1($property)]) || (isset($this->propertiesCamelCaseAliases[$property]) && isset($this->properties[$this->propertiesCamelCaseAliases[$property]]));
}

public function save()
Expand Down