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