Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialise all properties when a new model is constructed #826

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions src/XeroPHP/Remote/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ public function __construct(Application $application = null)
$this->_dirty = [];
$this->_data = [];
$this->_associated_objects = [];

foreach (static::getProperties() as $property => $meta) {
if ($meta[self::KEY_IS_ARRAY]) {
$this->_data[$property] = new Collection();
} else {
$this->_data[$property] = null;
}
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Remote/Model/ModelWithCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public static function getRootNodeName()

public function getEarningsLines()
{
return isset($this->_data['EarningsLines']) ? $this->_data['EarningsLines'] : null;
return $this->_data['EarningsLines'];
}

public function getModelID()
{
return isset($this->_data['ModelID']) ? $this->_data['ModelID'] : null;
return $this->_data['ModelID'];
}
}
11 changes: 7 additions & 4 deletions tests/Remote/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
use GuzzleHttp\Psr7\Response;
use XeroPHP\Application;
use XeroPHP\Remote\Collection;
use XeroPHP\Remote\Model;
use XeroPHP\Tests\Remote\Model\ModelWithCollection;

Expand Down Expand Up @@ -64,7 +64,8 @@ public function testSetsEmptyCollection()

$model = $app->loadByGUID(ModelWithCollection::class, 'test');
$this->assertSame('test', $model->getModelID());
$this->assertNull($model->getEarningsLines());
$this->assertInstanceOf(Collection::class, $model->getEarningsLines());
$this->assertEquals(0, count($model->getEarningsLines()));
}
}

Expand All @@ -77,7 +78,9 @@ public static function getGUIDProperty()

public static function getProperties()
{
return ['test'];
return [
'test' => [false, self::PROPERTY_TYPE_STRING, null, false, false],
];
}

public static function getSupportedMethods()
Expand Down Expand Up @@ -105,7 +108,7 @@ public static function getAPIStem()
*/
public function getTest()
{
return isset($this->_data['test']) ? $this->_data['test'] : null;
return $this->_data['test'];
}

/**
Expand Down