Example #1
0
 /**
  * Tells whether a spefic IDocument instance is considered equal to an other given IDocument.
  *
  * @param IDocument $other
  *
  * @return boolean
  */
 public function isEqualTo(IDocument $other)
 {
     $is_equal = true;
     if ($other->getModule() !== $this->getModule()) {
         throw new Error\BadValueException("Only IDocument instances of the same module may be compared.");
     }
     foreach ($this->getModule()->getFields() as $field) {
         $lefthand_value = $this->getValue($field->getName(), false);
         $righthand_value = $other->getValue($field->getName(), false);
         if (!$lefthand_value->isEqualTo($righthand_value)) {
             $is_equal = false;
             break;
         }
     }
     return $is_equal;
 }
Example #2
0
 /**
  * Sets either given default value or value from option to the given field.
  *
  * @param Document $document the document to modify
  * @param string $fieldname the name of the field to set a value for
  * @param mixed $default_value Default value to set.
  * @param array $options Array containing a `fieldname => $mixed` entry.
  *                       $mixed is set as value instead of $default_value.
  *                       If $mixed is a closure it will be called and used.
  *                       $mixed may also be another callable like an array
  *                       `array($class, "$methodName")` or a string like
  *                       `'Your\Namespace\Foo::getStaticTrololo'`.
  *
  * @return void
  */
 protected function setValue(IDocument $document, IField $field, $default_value, array $options = array())
 {
     $fieldname = $field->getName();
     $fieldoptions = array();
     if (!empty($options[self::OPTION_FIELD_VALUES]) && is_array($options[self::OPTION_FIELD_VALUES])) {
         $fieldoptions = $options[self::OPTION_FIELD_VALUES];
     }
     if (empty($fieldoptions[$fieldname])) {
         $document->setValue($fieldname, $default_value);
     } else {
         $option = $fieldoptions[$fieldname];
         if (is_callable($option)) {
             $document->setValue($fieldname, call_user_func($option));
         } else {
             $document->setValue($fieldname, $option);
         }
     }
 }