Commit a748fb1e authored by Stephen Rees's avatar Stephen Rees
Browse files

Changes after practical implementation effort

parent 8ec6ce85
# ZenodoPHP
A PHP library for utilizing the Zenodo API. Requires >= PHP 7.1.
A PHP library for utilizing the Zenodo API. Requires >= PHP 7.0.
Zenodo is part of CERN and provides permanent archive and Digital Object Identifier (DOI) capabilities (through DataCite) for free. You can sign up separately for their production environment at https://zenodo.org and for their sandbox environment at https://sandbox.zenodo.org
......@@ -11,25 +11,28 @@ Licensing is under review at this time. The intention is to open-source this lib
# Example 1 - Retrieve all existing depositions
```php
require_once(__DIR__ . '/includes/zenodoAPI.php');
require_once('zenodoAPI.php');//autoloader
use zenodoAPI\zenodoConnection;
use zenodoAPI\zenodoDepositionCollection;
$sandboxtoken = '';//your sandbox token
$connection = new zenodoConnection($sandboxtoken, 'sandbox');
$collection_obj = new ZenodoDepositionCollection($connection);
$collection_obj = new zenodoDepositionCollection($connection);
try{
$collection = $collection_obj->list_depositions();//returns an array of deposition objects
}
catch (RuntimeException $e) {
//you should only have to catch the runtime exception the first time you request data from Zenodo
catch (RuntimeException $e) {//thrown when cURL experiences a failure
echo $e->getMessage();
echo var_export($connection->last_response());//this is where you will find details on any errors
return;
}
catch (InvalidArgumentException $e) {//thrown when invalid arguments are detected by the library
echo $e->getMessage();
echo var_export($connection->last_response());//this is where you will find details on any errors
return;
}
catch (LogicException $e) {
//you should catch a logic exception every time you request data from Zenodo.
catch (LogicException $e) {//thrown when the API endpoint complains about something
echo $e->getMessage();
echo var_export($connection->last_response());
return;
......@@ -39,18 +42,19 @@ echo var_export($collection);
# Example 2 - Retrieve a specific deposition
```php
//leaving off the exception handling this time
require_once(__DIR__ . '/includes/zenodoAPI.php');
require_once('zenodoAPI.php');
use zenodoAPI\zenodoConnection;
use zenodoAPI\zenodoDeposition;
$sandboxtoken = '';//your sandbox token
$retrieve_deposition_id = 0;//deposition ID you wish to retrieve
$connection = new zenodoConnection('your_sandbox_token', 'sandbox');
$deposition = new zenodoDeposition($connection);
$deposition->retrieve_deposition(12345);//whatever your deposition ID is...
echo var_export($deposition->clean());//clean returns a cloned object stripped of extraneous data such as the connection
$deposition->retrieve_deposition($retrieve_deposition_id);
echo var_export($deposition->clean());//clean returns a cloned object stripped of extraneous data such as the connection information
```
# Example 3 - Create a deposition
# Example 3 - Create a deposition (old file API, < 100MB)
```php
require_once(__DIR__ . '/includes/zenodoAPI.php');
......@@ -63,7 +67,7 @@ $deposition = new zenodoDeposition($connection);
$deposition->create_deposition();//deposition object is now created with Zenodo and ready to be populated with metadata
$deposition->add_file('path_to_file');//file should be uploaded to Zenodo with this call
//Or if you want to rename it...
//if you want to rename it...
$zenodo_file = $deposition->add_file('path_to_file');
$zenodo_file->filename('new name');
$zenodo_file->update_file();
......@@ -77,7 +81,6 @@ $creator->name = 'Family name, Given names';
$deposition->metadata->creators([$creator]);
$deposition->metadata->description('This is an example of how to create a deposition with the Zenodo API');
$deposition->metadata->access_right('closed');
$deposition->metadata->keywords(['first', 'example']);
$deposition->update_deposition();
//Want to publish it? This locks it against changes and deletion permanently
......
......@@ -50,10 +50,12 @@ class zenodoConnection
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $this->access_token,
],
CURLOPT_SSL_VERIFYPEER => true,
// CURLOPT_SSL_VERIFYPEER => true,//not until PHP 7.1
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_POSTREDIR => 3,
CURLINFO_HEADER_OUT => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
];
if (!empty($_content_type)) {
$options[CURLOPT_HTTPHEADER][] = "Content-Type: $_content_type";
......@@ -68,7 +70,14 @@ class zenodoConnection
null,
basename($_data)
);//TODO: do we want to include mimetype as part of this?
$_data = ['file' => $file];
$_data = ['file' => $file, 'name' => basename($_data)];
//PHP7.4.3 has a bug with cURL and servers that don't accept chunked mode...
//https://bugs.php.net/bug.php?id=79013
//If this affects you, you'll need to figure out the content length
//and add the following two headers
//$size = 0;//figure out how to come up with content length?
//$options[CURLOPT_HTTPHEADER][] = "Transfer-Encoding: ";
//$options[CURLOPT_HTTPHEADER][] = "Content-Length: $size";
}
$options[CURLOPT_POSTFIELDS] = $_data;
}
......@@ -78,7 +87,7 @@ class zenodoConnection
$curl_error = curl_error($ch);
curl_close($ch);
$this->last_response = [
'data' => $_data,
'sent' => $_data,
'response' => $response,
'curl_info' => $curl_info,
'curl_error' => $curl_error,
......@@ -88,7 +97,8 @@ class zenodoConnection
'Curl Failed. Check last_response() for details.'
);
}
$response_data = json_decode($response);
$response_data = json_decode($response);
$this->last_response['decoded'] = $response_data;
//responses, including errors, are returned as jSON objects -- with the exception of collections, which are returned as an array of objects
if (is_object(
$response_data
......
......@@ -77,9 +77,7 @@ class zenodoDeposition
if (isset($this->$_name)) {
return $this->$_name;
} else {
throw new \InvalidArgumentException(
'Attempted to get non-existent value from object'
);
return NULL;
}
}
......@@ -94,7 +92,7 @@ class zenodoDeposition
$this->created = $_setValue;
}
private function doi(string $_setValue)
public function doi(string $_setValue)
{
$this->doi = $_setValue;
}
......@@ -253,7 +251,11 @@ class zenodoDeposition
$_deposition
) || $_deposition instanceof zenodoDepositionMetadata) {
if (is_null($_deposition)) {
$deposition = new \stdClass();
if ($this->metadata->clean() == new \stdClass()) {
$deposition = new \stdClass();
} else {
$deposition = ['metadata' => $this->metadata->clean()];
}
} else {
$deposition = ['metadata' => $_deposition->clean()];
}
......@@ -319,13 +321,13 @@ class zenodoDeposition
public function add_file(string $_file): zenodoDepositionFile
{
if (file_exists(realpath($_file))) {
if (is_readable(realpath($_file))) {
$newfile = new zenodoDepositionFile($this->connection, $this->id);
$newfile->create_file($_file);
$this->files[] = $newfile;
return $newfile;
} else {
throw new \InvalidArgumentException('File cannot be found');
throw new \InvalidArgumentException('File cannot be read');
}
}
......
......@@ -32,9 +32,7 @@ class zenodoDepositionFile
if (isset($this->$_name)) {
return $this->$_name;
} else {
throw new \InvalidArgumentException(
'Attempted to get non-existent value from object'
);
return null;
}
}
......
......@@ -96,14 +96,14 @@ class zenodoDepositionMetadata
public function __construct()
{
$this->upload_type = '';
//$this->upload_type = '';
// $this->publication_type = '';
// $this->image_type = '';
$this->publication_date = '';
$this->title = '';
$this->creators = [];//objects
$this->description = '';//HTML allowed
$this->access_right = '';
//$this->publication_date = '';
//$this->title = '';
//$this->creators = [];//objects
//$this->description = '';//HTML allowed
//$this->access_right = '';
// $this->license = '';
// $this->embargo_date = '';//date
// $this->access_conditions = '';//HTML allowed
......@@ -147,12 +147,15 @@ class zenodoDepositionMetadata
if (isset($this->$_name)) {
return $this->$_name;
} else {
throw new \InvalidArgumentException(
'Attempted to get non-existent value from object'
);
return null;
}
}
public function __isset(string $_name)
{
return isset($this->$_name);
}
public function load(\stdClass $_jsonObject)
{
foreach ($_jsonObject as $key => $value) {
......@@ -173,7 +176,7 @@ class zenodoDepositionMetadata
return $clean_obj;
}
public function allowed_upload_types(): array
public static function allowed_upload_types(): array
{
return [
'publication' => 'Publication',
......@@ -199,7 +202,7 @@ class zenodoDepositionMetadata
}
}
public function allowed_publication_types(): array
public static function allowed_publication_types(): array
{
return [
'annotationcollection' => 'Annotation collection',
......@@ -225,7 +228,10 @@ class zenodoDepositionMetadata
public function publication_type(string $_setValue)
{
if (array_key_exists($_setValue, $this->allowed_publication_types())) {
if (array_key_exists(
$_setValue,
$this->allowed_publication_types()
) || empty($_setValue)) {
$this->publication_type = $_setValue;
} else {
throw new \InvalidArgumentException(
......@@ -234,7 +240,7 @@ class zenodoDepositionMetadata
}
}
public function allowed_image_types(): array
public static function allowed_image_types(): array
{
return [
'figure' => 'Figure',
......@@ -248,7 +254,10 @@ class zenodoDepositionMetadata
public function image_type(string $_setValue)
{
if (array_key_exists($_setValue, $this->allowed_image_types())) {
if (array_key_exists(
$_setValue,
$this->allowed_image_types()
) || empty($_setValue)) {
$this->image_type = $_setValue;
} else {
throw new \InvalidArgumentException(
......@@ -285,6 +294,12 @@ class zenodoDepositionMetadata
*/
public function creators(array $_setValue)
{
if (count($_setValue) < 1) {
throw new \InvalidArgumentException(
'There must be at least one creator'
);
}
$this->creators = [];
foreach ($_setValue as $value) {
if (is_object($value)) {
$creator = new \stdClass();
......@@ -311,12 +326,33 @@ class zenodoDepositionMetadata
public function description(string $_setValue)
{
if (strlen($_setValue) < 3) {
throw new \InvalidArgumentException(
'Description must have a minimum of 3 characters'
);
}
$this->description = $_setValue;
}
public static function allowed_access_rights(): array
{
return [
'open' => 'Open Access',
'embargoed' => 'Embargoed Access',
'restricted' => 'Restricted Access',
'closed' => 'Closed Access',
];
}
public function access_right(string $_setValue)
{
$this->access_right = $_setValue;
if (array_key_exists($_setValue, $this->allowed_access_rights())) {
$this->access_right = $_setValue;
} else {
throw new \InvalidArgumentException(
'Invalid access_right provided. See allowed_access_rights()'
);
}
}
public function license(string $_setValue)
......@@ -374,6 +410,7 @@ class zenodoDepositionMetadata
public function keywords(array $_setValue)
{
$this->keywords = [];
foreach ($_setValue as $value) {
if (is_string($value)) {
$this->keywords[] = $value;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment