CWIS Developer Documentation
Vocabulary.php
Go to the documentation of this file.
1 <?PHP
2 #
3 # FILE: Vocabulary.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 
13 class Vocabulary {
14 
15  # ---- PUBLIC INTERFACE --------------------------------------------------
16 
22  public function __construct($FileName)
23  {
24  # save file name
25  $this->FileName = $FileName;
26 
27  # attempt to load vocabulary from file
28  $this->Xml = simplexml_load_file($FileName);
29 
30  # set error code if load failed
31  $this->StatusString = ($this->Xml === FALSE) ? "XML Load Failed" : "OK";
32  $this->Xml = isset($this->Xml->vocabulary) ? $this->Xml->vocabulary : $this->Xml;
33  }
34 
38  public function Status()
39  {
40  return $this->StatusString;
41  }
42 
47  public function Hash()
48  {
49  return self::HashForFile($this->FileName);
50  }
51 
57  static function HashForFile($FileName = NULL)
58  {
59  return strtoupper(md5($FileName));
60  }
61 
66  public function Name()
67  {
68  return $this->XmlVal("name");
69  }
70 
75  public function Description()
76  {
77  return $this->XmlVal("description");
78  }
79 
84  public function Url()
85  {
86  return $this->XmlVal("url");
87  }
88 
93  public function Version()
94  {
95  return $this->XmlVal("version");
96  }
97 
102  public function HasQualifier()
103  {
104  return (strlen($this->QualifierName())
105  && (strlen($this->QualifierNamespace())
106  || strlen($this->QualifierUrl()))) ? TRUE : FALSE;
107  }
108 
114  public function QualifierName()
115  {
116  return isset($this->Xml->qualifier->name)
117  ? (string)$this->Xml->qualifier->name : "";
118  }
119 
125  public function QualifierNamespace()
126  {
127  return isset($this->Xml->qualifier->namespace)
128  ? (string)$this->Xml->qualifier->namespace : "";
129  }
130 
136  public function QualifierUrl()
137  {
138  return isset($this->Xml->qualifier->url)
139  ? (string)$this->Xml->qualifier->url : "";
140  }
141 
146  public function OwnerName()
147  {
148  return isset($this->Xml->owner->name)
149  ? (string)$this->Xml->owner->name : "";
150  }
151 
156  public function OwnerUrl()
157  {
158  return isset($this->Xml->owner->url)
159  ? (string)$this->Xml->owner->url : "";
160  }
161 
166  public function TermArray()
167  {
168  $Terms = $this->ExtractTermSet($this->Xml);
169 
170  # return array of terms to caller
171  return $Terms;
172  }
173 
178  public function TermList()
179  {
180  $TermTree = $this->TermArray();
181  $Terms = $this->BuildTermList("", $TermTree);
182  return $Terms;
183  }
184 
185  # ---- PRIVATE INTERFACE -------------------------------------------------
186 
187  private $FileName;
188  private $StatusString;
189  private $Xml;
190 
195  private function XmlVal($ValueName)
196  {
197  return isset($this->Xml->{$ValueName})
198  ? (string)$this->Xml->{$ValueName} : "";
199  }
200 
206  private function ExtractTermSet($Tree)
207  {
208  # make sure a valid SimpleXMLElement was given and return an empty
209  # array if not
210  if (!($Tree instanceof SimpleXMLElement))
211  {
212  return array();
213  }
214 
215  $Terms = array();
216  foreach ($Tree->term as $Term)
217  {
218  if (isset($Term->value))
219  {
220  $Terms[(string)$Term->value] = $this->ExtractTermSet($Term);
221  }
222  else
223  {
224  $Terms[(string)$Term] = array();
225  }
226  }
227  return $Terms;
228  }
229 
236  private function BuildTermList($Prefix, $TermTree)
237  {
238  $Terms = array();
239  foreach ($TermTree as $Term => $Children)
240  {
241  $Term = trim($Term);
242  $NewTerm = strlen($Prefix) ? $Prefix." -- ".$Term : $Term;
243  $Terms[] = $NewTerm;
244  $Terms = array_merge($Terms, $this->BuildTermList($NewTerm, $Children));
245  }
246  return $Terms;
247  }
248 }
static HashForFile($FileName=NULL)
Get hash string for specified vocabulary file name.
Definition: Vocabulary.php:57
Status()
Get string indicate status of last action.
Definition: Vocabulary.php:38
Description()
Get vocabulary description.
Definition: Vocabulary.php:75
HasQualifier()
Get whether vocabulary has associated qualifier.
Definition: Vocabulary.php:102
OwnerName()
Get name of owning (maintaining) organization.
Definition: Vocabulary.php:146
QualifierName()
Get qualifier name.
Definition: Vocabulary.php:114
QualifierNamespace()
Get qualifier namespace.
Definition: Vocabulary.php:125
TermArray()
Get vocabulary terms as multi-dimensional array.
Definition: Vocabulary.php:166
Controlled vocabulary.
Definition: Vocabulary.php:13
Hash()
Get hash string for vocabulary (generated from file name).
Definition: Vocabulary.php:47
Name()
Get vocabulary name.
Definition: Vocabulary.php:66
OwnerUrl()
Get primary URL for owning (maintaining) organization.
Definition: Vocabulary.php:156
TermList()
Get vocabulary terms as flat array with double-dash separators.
Definition: Vocabulary.php:178
Url()
Get URL attached to vocabulary.
Definition: Vocabulary.php:84
QualifierUrl()
Get qualifier URL.
Definition: Vocabulary.php:136
Version()
Get version number for vocabulary.
Definition: Vocabulary.php:93
__construct($FileName)
Object constructor.
Definition: Vocabulary.php:22