diff --git a/library/Director/CustomVariable/CustomVariable.php b/library/Director/CustomVariable/CustomVariable.php new file mode 100644 index 00000000..bfa6e617 --- /dev/null +++ b/library/Director/CustomVariable/CustomVariable.php @@ -0,0 +1,119 @@ +key = $key; + $this->setValue($value); + } + + public function is($type) + { + return $this->getType() === $type; + } + + public function getType() + { + if ($this->type === null) { + $parts = explode('\\', get_class($this)); + $class = end($parts); + // strlen('CustomVariable') === 9 + $this->type = substr(end($parts), 9); + } + + return $this->type; + } + + public function setValue($value) + { + if ($value instanceof CustomVariable) { + if (! $this->equals($value)) { + $this->reallySet($value); + } + } elseif ($value !== $this->value) { + $this->reallySet($value); + } + + return $this; + } + + protected function reallySetValue($value) + { + $this->modified = true; + $this->value = $value; + } + + public function hasBeenModified() + { + return $this->modified; + } + + public function setUnmodified() + { + $this->modified = false; + $this->storedValue = clone($this->value); + return $this; + } + + abstract public function equals(CustomVariable $var); + + abstract public function toConfigString(); + + public function differsFrom(CustomVariable $var) + { + return ! $this->equals($var); + } + + public static function create($key, $value) + { + if (is_string($value)) { + + return new CustomVariableString($key, $value); + + } elseif (is_array($value)) { + + foreach (array_keys($value) as & $key) { + if (! is_int($key) || ctype_digit($key)) { + return new CustomVariableDictionary($key, $value); + } + } + + return new CustomVariableArray($key, array_values($value)); + + } elseif (is_object($value)) { + // TODO: check for specific class/stdClass/interface? + return new CustomVariableDictionary($key, $value); + + } else { + throw new ProgrammingError(); + } + } + + public function __toString() + { + try { + return $this->toConfigString(); + } catch (Exception $e) { + trigger_error($e); + $previousHandler = set_exception_handler(function () {}); + restore_error_handler(); + call_user_func($previousHandler, $e); + die(); + } + } +} diff --git a/library/Director/CustomVariable/CustomVariableArray.php b/library/Director/CustomVariable/CustomVariableArray.php new file mode 100644 index 00000000..d21f5e62 --- /dev/null +++ b/library/Director/CustomVariable/CustomVariableArray.php @@ -0,0 +1,15 @@ +getValue() === $this->getValue(); + } +} diff --git a/library/Director/CustomVariable/CustomVariableDictionary.php b/library/Director/CustomVariable/CustomVariableDictionary.php new file mode 100644 index 00000000..35ef431d --- /dev/null +++ b/library/Director/CustomVariable/CustomVariableDictionary.php @@ -0,0 +1,51 @@ +listKeys(); + $foreignKeys = $var->listKeys(); + if ($myKeys !== $foreignKeys) { + return false; + } + + foreach ($this->value as $key => $value) { + if ($this->$key->differsFrom($value)->$key) { + return false; + } + } + + return true; + } + + public function listKeys() + { + $keys = array_keys($this->value); + ksort($keys); + return $keys; + } + + public function count() + { + return count($this->value); + } + + public function __clone() + { + foreach ($this->value as $key => $value) { + $this->value->$key = clone($value); + } + } + + public function __get($key) + { + // ... + } +} diff --git a/library/Director/CustomVariable/CustomVariableString.php b/library/Director/CustomVariable/CustomVariableString.php new file mode 100644 index 00000000..58c59847 --- /dev/null +++ b/library/Director/CustomVariable/CustomVariableString.php @@ -0,0 +1,21 @@ +getValue() === $this->getValue(); + } + + public function toConfigString() + { + return c::renderKeyValue( + c::escapeIfReserved($this->getKey()), + c::renderString($this->getValue() + ); + } +} diff --git a/library/Director/CustomVariable/CustomVariables.php b/library/Director/CustomVariable/CustomVariables.php new file mode 100644 index 00000000..d06ca8a6 --- /dev/null +++ b/library/Director/CustomVariable/CustomVariables.php @@ -0,0 +1,93 @@ +$key); + } + + if ($value === $this->get($key)) { + return $this; + } + + $this->vars[$key] = $value; + $this->modified = true; + + return $this; + } + + public function hasBeenModified() + { + return $this->modifiec + } + + public function setUnmodified() + { + $this->modified = false; + $this->storedVars = $this->vars; + return $this; + } + + public function __get($key) + { + return $this->get($key); + } + + /** + * Magic setter + * + * @param string $key Key + * @param mixed $val Value + * + * @return void + */ + public function __set($key, $val) + { + $this->set($key, $val); + } + + /** + * Magic isset check + * + * @return boolean + */ + public function __isset($key) + { + return array_key_exists($key, $this->properties); + } + + /** + * Magic unsetter + * + * @return void + */ + public function __unset($key) + { + if (! array_key_exists($key, $this->properties)) { + throw new Exception('Trying to unset invalid key'); + } + $this->properties[$key] = $this->defaultProperties[$key]; + } + + +}