diff --git a/lib/Alternc/Diagnostic/Format/Abstract.php b/lib/Alternc/Diagnostic/Format/Abstract.php index 58001499..e1e0f012 100644 --- a/lib/Alternc/Diagnostic/Format/Abstract.php +++ b/lib/Alternc/Diagnostic/Format/Abstract.php @@ -83,6 +83,32 @@ class Alternc_Diagnostic_Format_Abstract { return $this->directory; } + /** + * Checks a file reference is ok + * + * @param string $file_name + * @return boolean + * @throws \Exception + */ + function checkIsFileReadable($file_name){ + + $file_path = $this->directory->getFile_path() .DIRECTORY_SEPARATOR.$file_name; + if( ! is_file( $file_path)){ + throw new \Exception("Invalid file: $file_path does not exist."); + } + + if( !is_readable( $file_path)){ + throw new \Exception("Invalid file: $file_path cannot be read."); + } + return true; + } + function getFileContent( $file_reference ){ + + $file_path = $this->directory->getFile_path() .DIRECTORY_SEPARATOR.$file_reference; + return file_get_contents($file_path); + + } + } diff --git a/lib/Alternc/Diagnostic/Format/Factory.php b/lib/Alternc/Diagnostic/Format/Factory.php new file mode 100644 index 00000000..128047d0 --- /dev/null +++ b/lib/Alternc/Diagnostic/Format/Factory.php @@ -0,0 +1,47 @@ +file_name = $file_name; + $this->directory = $directory; + + } + + /** + * Reads file and converts into data + * + * @return Alternc_Diagnostic_Data + */ + function build(){ + + $extension = pathinfo($this->file_name, PATHINFO_EXTENSION); + $class_name = "Alternc_Diagnostic_Format_".ucfirst(strtolower($extension)); + $format = new $class_name($this->directory); + $dataInstance = $format->read($this->file_name); + return $dataInstance; + + } + + +} \ No newline at end of file diff --git a/lib/Alternc/Diagnostic/Format/Interface.php b/lib/Alternc/Diagnostic/Format/Interface.php index 662bcb15..439135e3 100644 --- a/lib/Alternc/Diagnostic/Format/Interface.php +++ b/lib/Alternc/Diagnostic/Format/Interface.php @@ -14,7 +14,7 @@ interface Alternc_Diagnostic_Format_Interface{ /** * Writes a Data object to file * - * @return boolean + * @return string file_name */ function write(); diff --git a/lib/Alternc/Diagnostic/Format/Json.php b/lib/Alternc/Diagnostic/Format/Json.php index ccd77039..2a9a98fb 100644 --- a/lib/Alternc/Diagnostic/Format/Json.php +++ b/lib/Alternc/Diagnostic/Format/Json.php @@ -21,6 +21,22 @@ class Alternc_Diagnostic_Format_Json */ function read( $file_reference ){ + // Attempts to check file is ok + $this->checkIsFileReadable($file_reference); + + // Attempts to retrieve file content + $file_content = $this->getFileContent($file_reference); + + // Attempts to convert string to json + $arrayData = json_decode($file_content,true); + + // Exits if error + if(json_last_error()){ + throw new \Exception("Failed to convert file $file_reference from JSON with PHP JSON_ERROR #". json_last_error()); + } + + // Returns data object + return $this->convertJsonToData( $arrayData ); } @@ -40,8 +56,28 @@ class Alternc_Diagnostic_Format_Json if( ! file_put_contents($filename, $file_content) ){ throw new \Exception("Failed to write in json format to file $filename for data".serialize($this->getData())); } - return true; + return $filename; } + /** + * Operates the conversion recursively + * + * @param array $arrayData + * @return \Alternc_Diagnostic_Data + */ + function convertJsonToData( $arrayData ){ + + $dataInstance = new Alternc_Diagnostic_Data($arrayData["type"]); + $dataInstance->setMetadata($arrayData["metadata"]); + if( Alternc_Diagnostic_Data::TYPE_SECTION === $arrayData["type"] ){ + $dataInstance->setData($arrayData["data"]); + return $dataInstance; + } + foreach($arrayData["data"] as $key => $value){ + $dataInstance->addData($key, $this->convertJsonToData($value)); + } + return $dataInstance; + } + } \ No newline at end of file diff --git a/lib/Alternc/Diagnostic/Format/Resolver.php b/lib/Alternc/Diagnostic/Format/Resolver.php new file mode 100644 index 00000000..6b9fe1a4 --- /dev/null +++ b/lib/Alternc/Diagnostic/Format/Resolver.php @@ -0,0 +1,60 @@ +directoryInstance = $directoryInstance; + + } + /** + * Attempts to convert an int or a string to a file reference + * + * @param int|string $file_reference + * @return string + * @throws \Exception + * + */ + public function resolve( $file_reference ){ + if(is_int($file_reference) or preg_match("/\d+/",$file_reference)){ + $fileList = $this->directoryInstance->getList(); + if(array_key_exists($file_reference, $fileList)){ + $file_reference = $fileList[$file_reference]; + } + else{ + throw new \Exception("Invalid file reference $file_reference"); + } + } + return $file_reference; + } + + + /** + * Finds a file by reference or name + * + * @param mixed $file_reference + * @throws \Exception + * @return Alternc_Diagnostic_Data Resulting data + */ + function getDataInstance( $file_reference){ + + // Attempts to retrieve the file name + $file_reference = $this->resolve($file_reference); + // Uses factory to resolve the format + $factoryInstance = new Alternc_Diagnostic_Format_Factory( $file_reference, $this->directoryInstance); + return $factoryInstance->build(); + + } +}