Exemplo n.º 1
0
 /**
  * @param Field             $field
  * @param UniqueArrayObject $data
  * @return mixed
  * @throws SampleException
  */
 private function sampleField(Field $field, UniqueArrayObject $data)
 {
     $faker = $this->dogmatist->getFaker();
     $type = $field->getType();
     $value = null;
     if (Field::TYPE_FAKE === $type) {
         try {
             $value = $faker->format($field->getFakedType(), $field->getFakedOptions());
         } catch (\Exception $e) {
             throw new SampleException("Could not fake value of type {$field->getFakedType()}", 0, $e);
         }
     } elseif (Field::TYPE_VALUE === $type || Field::TYPE_SELECT === $type) {
         $value = $faker->randomElement($field->getSelection());
     } elseif (Field::TYPE_RELATION === $type) {
         $related = $field->getRelated();
         $value = $this->sample($related, $data);
         if ($related->hasParentLinks()) {
             $value = new ReplacableLink($value, $related->getParentLinks());
         }
     } elseif (Field::TYPE_LINK === $type) {
         $target = $field->getLinkTarget();
         if (is_array($target)) {
             $target = $faker->randomElement($target);
         }
         if ($this->dogmatist->getLinkManager()->hasUnlimitedSamples($target)) {
             $value = $this->dogmatist->sample($target);
         } else {
             $samples = $this->dogmatist->getLinkManager()->samples($target);
             $value = $faker->randomElement($samples);
         }
     } elseif (Field::TYPE_CALLBACK === $type) {
         $callback = $field->getCallback();
         $value = $callback($data, $this->dogmatist);
     } else {
         throw new SampleException("Could not generate data for field of type {$field->getType()}");
     }
     return $value;
 }