Example #1
0
 /**
  * @param Builder $builder
  * @return object|array
  */
 public function sample(Builder $builder, UniqueArrayObject $parent = null)
 {
     $data = new UniqueArrayObject($parent);
     $faker = $this->dogmatist->getFaker();
     foreach ($builder->getFields() as $field) {
         if (!$field->isType(Field::TYPE_NONE)) {
             $generate = $field->isSingular() ? 1 : $faker->numberBetween($field->getMin(), $field->getMax());
             $samples = [];
             $mark = false;
             for ($i = 0; $i < $generate; $i++) {
                 if ($field->isUnique()) {
                     $val = $this->sampleUniqueField($field, $data, $builder);
                 } else {
                     $val = $this->sampleField($field, $data);
                 }
                 if ($val instanceof ReplacableLink) {
                     $mark = true;
                 }
                 $samples[] = $val;
             }
             if ($field->isSingular()) {
                 $samples = $samples[0];
             } elseif ($mark) {
                 $samples = new ReplacableArray($samples);
             }
             $data[$field->getName()] = $samples;
         }
     }
     if ($builder instanceof ConstructorBuilder) {
         return $data->getArrayCopy();
     } else {
         $result = $this->insertInObject($data->getArrayCopy(), $builder);
         foreach ($builder->getListeners() as $listener) {
             call_user_func($listener, $result);
         }
         return $result;
     }
 }