Decorators for dynamically resolving field values
authorjollytoad <jollytoad>
Tue, 1 Mar 2005 10:40:15 +0000 (10:40 +0000)
committerjollytoad <jollytoad>
Tue, 1 Mar 2005 10:40:15 +0000 (10:40 +0000)
libraries/decorator.inc.php [new file with mode: 0644]

diff --git a/libraries/decorator.inc.php b/libraries/decorator.inc.php
new file mode 100644 (file)
index 0000000..a1d3ee1
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+// $Id: decorator.inc.php,v 1.1.2.1 2005/03/01 10:40:15 jollytoad Exp $
+
+// Field decorator
+function &field($fieldname, $default = null) {
+       $dec = new Decorator();
+       $dec->f = $fieldname;
+       if ($default !== null) $dec->d = $default;
+       return $dec;
+}
+
+// Merge arrays decorator
+function &merge() {
+       $dec = new Decorator();
+       $dec->m = func_get_args();
+       return $dec;
+}
+
+// Resolve a value
+function value(&$var, &$fields) {
+       if (is_a($var, 'Decorator')) {
+               if (isset($var->f)) {
+                       return isset($fields[$var->f]) ? $fields[$var->f] : (isset($var->d) ? $var->d : null);
+               }
+               if (isset($var->m)) {
+                       foreach ($var->m as $dec) {
+                               $val = value($dec, $fields);
+                               if (!isset($accum))
+                                       $accum = $val;
+                               else
+                                       $accum = array_merge($accum, $val);
+                       }
+                       return $accum;
+               }
+       } else
+               return $var;
+}
+
+// Resolve a value, and escape for an XML/HTML doc
+function value_xml(&$var, &$fields) {
+       return htmlentities(value($var, $fields));
+}
+
+// Resolve a value, and escape for a URL
+function value_url(&$var, &$fields) {
+       return urlencode(value($var, $fields));
+}
+
+// Resolve a value as an XML/HTML attribute
+function value_xml_attr($attr, &$var, &$fields) {
+       $value = value_xml($var, $fields);
+       if (!empty($value))
+               return " {$attr}=\"{$value}\"";
+       else
+               return '';
+}
+
+class Decorator
+{
+       function Decorator() {
+       }
+}
+
+?>