CWIS Developer Documentation
VocabularyFactory.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: VocabularyFactory.php
4 #
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2007-2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis/
8 #
9 
14 
15  # ---- PUBLIC INTERFACE --------------------------------------------------
16 
21  public function __construct($Path)
22  {
23  $this->Path = $Path;
24  }
25 
31  public function GetVocabularies()
32  {
33  # load vocabularies (if any)
34  $Vocabularies = array();
35  $VocFileNames = $this->GetFileList();
36  foreach ($VocFileNames as $FileName)
37  {
38  $Vocabularies[] = new Vocabulary($FileName);
39  }
40 
41  # sort vocabularies by name
42  $SortFunction = create_function('$VocA, $VocB', '
43  $NameA = $VocA->Name();
44  $NameB = $VocB->Name();
45  return ($NameA == $NameB) ? 0 : (($NameA < $NameB) ? -1 : 1);
46  ');
47  usort($Vocabularies, $SortFunction);
48 
49  # return array of vocabularies to caller
50  return $Vocabularies;
51  }
52 
59  public function GetVocabularyByHash($Hash)
60  {
61  # for each available vocabulary file
62  $Vocab = NULL;
63  $VocFileNames = $this->GetFileList();
64  foreach ($VocFileNames as $FileName)
65  {
66  # if hash for vocabulary file matches specified hash
67  if (Vocabulary::HashForFile($FileName) == $Hash)
68  {
69  # load vocabulary and stop searching file list
70  $Vocab = new Vocabulary($FileName);
71  break;
72  }
73  }
74 
75  # return matching vocabulary (if any) to caller
76  return $Vocab;
77  }
78 
79 
80  # ---- PRIVATE INTERFACE -------------------------------------------------
81 
83  private $Path;
84 
89  private function GetFileList()
90  {
91  $VocFiles = array();
92  if (is_dir($this->Path))
93  {
94  $AllFiles = scandir($this->Path);
95  foreach ($AllFiles as $FileName)
96  {
97  if (preg_match("/\\.voc\$/i", $FileName))
98  {
99  $VocFiles[] = realpath($this->Path."/".$FileName);
100  }
101  }
102  }
103  return $VocFiles;
104  }
105 }
106 
static HashForFile($FileName=NULL)
Get hash string for specified vocabulary file name.
Definition: Vocabulary.php:57
GetVocabularies()
load vocabulary objects from files
Controlled vocabulary.
Definition: Vocabulary.php:13
GetVocabularyByHash($Hash)
retrieve vocabulary object based on hash string
Factory for manipulating Vocabulary objects.
__construct($Path)
object constructor