10 changed files with 246 additions and 165 deletions
@ -1,35 +1,39 @@ |
|||||||
<?php |
<?php |
||||||
/** |
|
||||||
* Created by IntelliJ IDEA. |
|
||||||
* User: ppound |
|
||||||
* Date: 2018-04-30 |
|
||||||
* Time: 11:25 AM |
|
||||||
*/ |
|
||||||
|
|
||||||
namespace Drupal\islandora_herbarium_object\DataParser; |
namespace Drupal\islandora_herbarium_object\DataParser; |
||||||
|
|
||||||
|
/** |
||||||
|
* Abstract implementation of DataParserInterface base on arrays. |
||||||
|
*/ |
||||||
|
abstract class DataParser implements DataParserInterface { |
||||||
|
|
||||||
abstract class DataParser implements DataParserInterface |
|
||||||
{ |
|
||||||
public $data; |
public $data; |
||||||
public $data_arr = array(); |
|
||||||
|
|
||||||
|
public $dataArray = []; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
public function getRemoteData($uri, $returnAsVariable = FALSE) { |
public function getRemoteData($uri, $returnAsVariable = FALSE) { |
||||||
$client = \Drupal::httpClient(); |
$client = \Drupal::httpClient(); |
||||||
$request = $client->get(trim($uri)); |
$request = $client->get(trim($uri)); |
||||||
$data = $request->getBody()->getContents(); |
$data = $request->getBody()->getContents(); |
||||||
if(empty($data)) { |
if (empty($data)) { |
||||||
throw new Exception("Empty dataset"); |
throw new Exception("Empty dataset"); |
||||||
} |
} |
||||||
if(!$returnAsVariable) { |
if (!$returnAsVariable) { |
||||||
$this->data = $data; |
$this->data = $data; |
||||||
} else { |
} |
||||||
|
else { |
||||||
return $data; |
return $data; |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
public function getValue($key) { |
public function getValue($key) { |
||||||
return isset($this->data_arr[$key]) ? $this->data_arr[$key] : ""; |
return isset($this->dataArray[$key]) ? $this->dataArray[$key] : ""; |
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
@ -1,23 +1,53 @@ |
|||||||
<?php |
<?php |
||||||
/** |
|
||||||
* Created by IntelliJ IDEA. |
|
||||||
* User: ppound |
|
||||||
* Date: 2018-04-30 |
|
||||||
* Time: 1:10 PM |
|
||||||
*/ |
|
||||||
|
|
||||||
namespace Drupal\islandora_herbarium_object\DataParser; |
namespace Drupal\islandora_herbarium_object\DataParser; |
||||||
|
|
||||||
|
use Drupal\Core\Entity\EntityInterface; |
||||||
|
|
||||||
interface DataParserInterface |
/** |
||||||
{ |
* Common functions for herbarium taxon and location data. |
||||||
|
*/ |
||||||
public function getRemoteData($uri); |
interface DataParserInterface { |
||||||
|
|
||||||
|
/** |
||||||
|
* Retrieve data from a remote URI. |
||||||
|
* |
||||||
|
* @param string $uri |
||||||
|
* The request uri. |
||||||
|
* @param bool $returnAsVariable |
||||||
|
* If TRUE return the request data, otherwise set a class data variable(s). |
||||||
|
* |
||||||
|
* @return mixed |
||||||
|
* Either NULL or the request data, determined by the value of |
||||||
|
* $returnAsVariable. |
||||||
|
*/ |
||||||
|
public function getRemoteData($uri, $returnAsVariable = FALSE); |
||||||
|
|
||||||
|
/** |
||||||
|
* Parse the Data stored in a local data structure. |
||||||
|
* |
||||||
|
* The local data structure is usually populated by the getRemoteData() |
||||||
|
* function. |
||||||
|
*/ |
||||||
public function parseData(); |
public function parseData(); |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns a value from a local data structure based on the corresponding key. |
||||||
|
* |
||||||
|
* @param string $key |
||||||
|
* The key to use to lookup the data. |
||||||
|
* |
||||||
|
* @return string |
||||||
|
* The value if the key exists otherwise an empty string. |
||||||
|
*/ |
||||||
public function getValue($key); |
public function getValue($key); |
||||||
|
|
||||||
public function updateEntity($entity); |
/** |
||||||
|
* Update a Drupal Entity using the local data. |
||||||
|
* |
||||||
|
* @param Drupal\Core\Entity\EntityInterface $entity |
||||||
|
* A Drupal Entity. |
||||||
|
*/ |
||||||
|
public function updateEntity(EntityInterface $entity); |
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
@ -1,18 +1,22 @@ |
|||||||
<?php |
<?php |
||||||
/** |
|
||||||
* Created by IntelliJ IDEA. |
|
||||||
* User: ppound |
|
||||||
* Date: 2018-05-04 |
|
||||||
* Time: 1:16 PM |
|
||||||
*/ |
|
||||||
|
|
||||||
namespace Drupal\islandora_herbarium_object\DataParser; |
namespace Drupal\islandora_herbarium_object\DataParser; |
||||||
|
|
||||||
|
/** |
||||||
|
* Provides functions for simple XML data. |
||||||
|
*/ |
||||||
|
abstract class XmlDataParser extends DataParser { |
||||||
|
|
||||||
abstract class XmlDataParser extends DataParser |
/** |
||||||
{ |
* Retrieves the value of the first xml node matching element_name. |
||||||
public function getXmlNodeValue($xml_doc, $element_name) |
* |
||||||
{ |
* @param \DOMDocument $xml_doc |
||||||
$this->data_arr[$element_name] = $xml_doc->getElementsByTagName($element_name)[0]->nodeValue; |
* An xml document. |
||||||
|
* @param string $element_name |
||||||
|
* The xml node to lookup. |
||||||
|
*/ |
||||||
|
public function getXmlNodeValue(\DOMDocument $xml_doc, $element_name) { |
||||||
|
$this->dataArray[$element_name] = $xml_doc->getElementsByTagName($element_name)[0]->nodeValue; |
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue