Exemplo n.º 1
0
 public function testIfWillSkipNonIndexableFields()
 {
     $model = new ExplicitlyNonIndexableField();
     $model->title = 'blah';
     $model->password = '******';
     $model->details = 'secret details';
     $model->garbage = 'some non persistend garbadge';
     $meta = ManganMeta::create($model);
     $filter = new Filter($model, SearchArray::class, $meta);
     $passwordFilter = $filter->getFor('password');
     $data = SearchArray::fromModel($model);
     codecept_debug($data);
     $this->assertTrue(array_key_exists('title', $data), 'That title *is* stored');
     $this->assertSame('blah', $data['title']);
     $this->assertFalse(array_key_exists('password', $data), 'That password is *not* stored');
     $this->assertFalse(array_key_exists('details', $data), 'That details is *not* stored');
     $this->assertFalse(array_key_exists('garbage', $data), 'That garbage is *not* stored');
 }
Exemplo n.º 2
0
 /**
  * Create document from array
  *
  * @param mixed[] $data
  * @param string|object $className
  * @param AnnotatedInterface $instance
  * @return AnnotatedInterface
  * @throws TransformatorException
  */
 public static function toModel($data, $className = null, AnnotatedInterface $instance = null)
 {
     $data = (array) $data;
     if (is_object($className)) {
         $className = get_class($className);
     }
     if (!$className) {
         if (array_key_exists('_class', $data)) {
             $className = $data['_class'];
         } else {
             if (null !== $instance) {
                 $className = get_class($instance);
             } else {
                 throw new TransformatorException('Could not determine document type');
             }
         }
     }
     if ($instance) {
         $model = $instance;
     } else {
         $model = new $className();
     }
     $meta = static::getMeta($model);
     $calledClass = get_called_class();
     $decorator = new Decorator($model, $calledClass, $meta);
     $md = new ModelDecorator($model, $calledClass, $meta);
     $sanitizer = new Sanitizer($model, $calledClass, $meta);
     $filter = new Filter($model, $calledClass, $meta);
     foreach ($meta->fields() as $name => $fieldMeta) {
         /* @var $fieldMeta DocumentPropertyMeta */
         if (array_key_exists($name, $data)) {
             // Value is available in passed data
             $value = $data[$name];
         } elseif (!empty($instance)) {
             // Take value from existing instance
             // NOTE: We could `continue` here but value should be sanitized anyway
             $value = $model->{$name};
         } else {
             // As a last resort set to default
             $value = $fieldMeta->default;
         }
         if (!$filter->toModel($model, $fieldMeta)) {
             continue;
         }
         $decorator->read($name, $value);
         $model->{$name} = $sanitizer->read($name, $model->{$name});
     }
     $md->read($data);
     return FinalizingManager::toModel(static::class, $model);
 }