/** * 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; }
/** * This method fills the given document with fake data. You may customize * the fake data generation used for each field by using the options array. * * Supported options: * - OPTION_LOCALE: Locale for fake data (e.g. 'en_UK', defaults to 'de_DE'). * - OPTION_MARK_CLEAN: Calls `$document->markClean()` at the end to prevent * change events to occur after faking data. Default is false. * - OPTION_FIELD_VALUES: array of `fieldname` => `value` pairs to customize * fake values per field of the given document. You can * either specify a direct value or provide a closure. The * closure must return the value you want to set on that field. * - OPTION_EXCLUDED_FIELDS: Array of fieldnames to excluded from filling * with fake data. * - OPTION_GUESS_PROVIDER_BY_NAME: Boolean true by default. Certain fieldnames * trigger different providers (e.g. firstname or email). * * @param IDocument $document an instance of the document to fill with fake data. * @param array $options array of options to customize fake data creation. * * @return void * * @throws \Dat0r\Core\Document\InvalidValueException in case of fake data being invalid for the given field * @throws \InvalidArgumentException in case of invalid locale option string * @throws \Dat0r\Core\Error\LogicException on AggregateField misconfiguration * @throws \Dat0r\Core\Error\InvalidImplementorException on various misconfigurations * @throws \Dat0r\Core\Error\ObjectImmutableException if an instance is frozen (closed to modifications) */ public function fake(IDocument $document, array $options = array()) { if (!empty($options[self::OPTION_LOCALE])) { $loc = $options[self::OPTION_LOCALE]; if (!is_string($loc) || !preg_match('#[a-z]{2,6}_[A-Z]{2,6}#', $loc)) { throw new \InvalidArgumentException('Given option "' . self::OPTION_LOCALE . '" is not a valid string. Use "languageCode_countryCode", e.g. "de_DE" or "en_UK".'); } $this->locale = $loc; $this->faker = Factory::create($this->locale); } $fields_to_exclude = array(); if (!empty($options[self::OPTION_EXCLUDED_FIELDS])) { $excluded = $options[self::OPTION_EXCLUDED_FIELDS]; if (!is_array($excluded)) { throw new \InvalidArgumentException('Given option "' . self::OPTION_EXCLUDED_FIELDS . '" is not an array. It should be an array of fieldnames.'); } $fields_to_exclude = $excluded; } $module = $document->getModule(); foreach ($module->getFields() as $fieldname => $field) { if (in_array($fieldname, $fields_to_exclude, true)) { continue; } $name = $this->getMethodNameFor($field); if (null !== $name && is_callable(array($this, $name))) { $this->{$name}($document, $field, $options); } else { $this->setValue($document, $field, $field->getDefaultValue(), $options); } } if (array_key_exists(self::OPTION_MARK_CLEAN, $options) && true === $options[self::OPTION_MARK_CLEAN]) { $document->markClean(); } }