From 3c3b4126b7aa8b7edd8a66b89bb5ae124ae7b088 Mon Sep 17 00:00:00 2001 From: Ori Hoch Date: Thu, 30 Nov 2017 16:08:48 +0200 Subject: [PATCH] change to addResource, getResource and removeResource --- README.md | 10 ++-- src/Datapackages/BaseDatapackage.php | 69 ++++++++++++++++------------ tests/DatapackageTest.php | 8 ++-- 3 files changed, 49 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index ed94292..4cdd25b 100644 --- a/README.md +++ b/README.md @@ -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" => [ @@ -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" => [ @@ -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 diff --git a/src/Datapackages/BaseDatapackage.php b/src/Datapackages/BaseDatapackage.php index 2f33f24..0b9443d 100644 --- a/src/Datapackages/BaseDatapackage.php +++ b/src/Datapackages/BaseDatapackage.php @@ -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) { diff --git a/tests/DatapackageTest.php b/tests/DatapackageTest.php index 6379e05..3412dc0 100644 --- a/tests/DatapackageTest.php +++ b/tests/DatapackageTest.php @@ -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) { @@ -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()); @@ -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') { @@ -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) {