Skip to content

Commit

Permalink
change to addResource, getResource and removeResource
Browse files Browse the repository at this point in the history
  • Loading branch information
OriHoch committed Nov 30, 2017
1 parent 0c3e92b commit 3c3b412
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 38 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ The package object has some useful methods to access and manipulate the resource
```php
$package = Package::load("tests/fixtures/multi_data_datapackage.json");
$package->resources(); // array of resource name => Resource object (see below for Resource class reference)
$package->resource("first-resource"); // Resource object matching the given name
$package->deleteResource("first-resource");
$package->getResource("first-resource"); // Resource object matching the given name
$package->removeResource("first-resource");
// add a tabular resource
$package->resource("tabular-resource-name", [
$package->addResource("tabular-resource-name", [
"profile" => "tabular-data-resource",
"schema" => [
"fields" => [
Expand All @@ -101,7 +101,7 @@ $package = Package::create([
"profile" => "tabular-data-package"
]);
// add a resource
$package->resource("resource-name", [
$package->addResource("resource-name", [
"profile" => "tabular-data-resource",
"schema" => [
"fields" => [
Expand All @@ -126,7 +126,7 @@ $package->save("datapackage.zip");
Resource objects can be accessed from a Package as described above

```php
$resource = $package->resource("resource-name")
$resource = $package->getResource("resource-name")
```

or instantiated directly
Expand Down
69 changes: 40 additions & 29 deletions src/Datapackages/BaseDatapackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,43 +76,54 @@ public function resources()
return $resources;
}

public function resource($name, $resource = null)
public function getResource($name)
{
if ($resource) {
if (is_a($resource, 'frictionlessdata\\datapackage\\Resources\\BaseResource')) {
$resource = $resource->descriptor();
} else {
$resource = Utils::objectify($resource);
}
$resource->name = $name;
$resourceDescriptors = [];
$gotMatch = false;
foreach ($this->descriptor->resources as $resourceDescriptor) {
if ($resourceDescriptor->name == $resource->name) {
$resourceDescriptors[] = $resource;
$gotMatch = true;
} else {
$resourceDescriptors[] = $resourceDescriptor;
}
foreach ($this->descriptor->resources as $resourceDescriptor) {
if ($resourceDescriptor->name == $name) {
return $this->initResource($resourceDescriptor);
}
if (!$gotMatch) {
}
throw new \Exception("couldn't find matching resource with name = '{$name}'");
}

public function addResource($name, $resource)
{
if (is_a($resource, 'frictionlessdata\\datapackage\\Resources\\BaseResource')) {
$resource = $resource->descriptor();
} else {
$resource = Utils::objectify($resource);
}
$resource->name = $name;
$resourceDescriptors = [];
$gotMatch = false;
foreach ($this->descriptor->resources as $resourceDescriptor) {
if ($resourceDescriptor->name == $resource->name) {
$resourceDescriptors[] = $resource;
$gotMatch = true;
} else {
$resourceDescriptors[] = $resourceDescriptor;
}
$this->descriptor->resources = $resourceDescriptors;
if (!$this->skipValidations) {
$this->revalidate();
}
}
if (!$gotMatch) {
$resourceDescriptors[] = $resource;
}
$this->descriptor->resources = $resourceDescriptors;
if (!$this->skipValidations) {
$this->revalidate();
}
}

// TODO: remove this function and use the getResource / addResource directly (will need to modify a lot of tests code)
public function resource($name, $resource = null)
{
if ($resource) {
$this->addResource($name, $resource);
} else {
foreach ($this->descriptor->resources as $resourceDescriptor) {
if ($resourceDescriptor->name == $name) {
return $this->initResource($resourceDescriptor);
}
}
throw new \Exception("couldn't find matching resource with name = '{$name}'");
return $this->getResource($name);
}
}

public function deleteResource($name)
public function removeResource($name)
{
$resourceDescriptors = [];
foreach ($this->descriptor->resources as $resourceDescriptor) {
Expand Down
8 changes: 4 additions & 4 deletions tests/DatapackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function testDatapackageResources()
));
// delete resource
$this->assertCount(2, $datapackage->resources());
$datapackage->deleteResource('resource-name');
$datapackage->removeResource('resource-name');
$this->assertCount(1, $datapackage->resources());
$i = 0;
foreach ($datapackage as $resource) {
Expand All @@ -272,7 +272,7 @@ public function testDatapackageResources()

// add a resource
$this->assertCount(1, $datapackage->resources());
$datapackage->resource('new-resource', [
$datapackage->addResource('new-resource', [
'path' => ['tests/fixtures/foo.txt', 'tests/fixtures/baz.txt'],
]);
$this->assertCount(2, $datapackage->resources());
Expand Down Expand Up @@ -378,7 +378,7 @@ public function testCreateEditDatapackageDescriptor()
file_put_contents($defaultSecondPath, 'BAHHH');

$tabularDataFilename = tempnam(sys_get_temp_dir(), 'datapackage-php-tests-').'.csv';
$package->resource('my-renamed-tabular-resource')->descriptor()->path[] = $tabularDataFilename;
$package->getResource('my-renamed-tabular-resource')->descriptor()->path[] = $tabularDataFilename;

foreach ($package as $resource) {
if ($resource->name() == 'my-default-resource') {
Expand All @@ -405,7 +405,7 @@ public function testCreateEditDatapackageDescriptor()

// now you can access the data normally
$i = 0;
foreach ($package->resource('my-default-resource') as $row) {
foreach ($package->getResource('my-default-resource') as $row) {
if ($i == 0) {
$this->assertEquals('foo', $row);
} elseif ($i == 1) {
Expand Down

0 comments on commit 3c3b412

Please sign in to comment.