示例#1
0
 public function __construct($model)
 {
     $this->model = $model;
     $this->meta = ManganelMeta::create($this->model);
     if (!empty($this->meta->type()->indexId) && false !== $this->meta->type()->indexId) {
         $this->isIndexable = true;
     }
     if ($this->isIndexable) {
         if (!isset($this->model->_id)) {
             throw new ManganelException(sprintf('Property `_id` is not set in model `%s`, this is required by Manganel', get_class($this->model)));
         }
         $this->manganel = Manganel::create($this->model);
     }
 }
 public function testIfWillFilter()
 {
     $model = new ExplicitlyNonIndexableField();
     $filter = new SearchFilter();
     $allowed = $filter->fromModel($model, ManganelMeta::create($model)->field('password'));
     $this->assertFalse($allowed, 'That password field was not indexed');
     $allowed = $filter->fromModel($model, ManganelMeta::create($model)->field('details'));
     $this->assertFalse($allowed, 'That details field was not indexed');
 }
 /**
  * This will be called when getting value.
  * This should return end user value.
  * @param AnnotatedInterface $model Document model which will be decorated
  * @param mixed $dbValues
  * @param string $transformatorClass Transformator class used
  * @return bool Return true if value should be assigned to model
  */
 public function read($model, &$dbValues, $transformatorClass = TransformatorInterface::class)
 {
     if (isset($dbValues[self::Key]) && isset($model->_id)) {
         $id = $dbValues[self::Key];
         // Need to sanitize value, as $dbValues contains raw value
         $sanitizer = new Sanitizer($model, SearchArray::class, ManganelMeta::create($model));
         $model->_id = $sanitizer->read('_id', $id);
         unset($dbValues[self::Key]);
     }
 }
示例#4
0
 public function testIfWillProperlyRemapSanitizer()
 {
     $model = new ModelWithSimpleTree();
     $model->parentId = new MongoId();
     $meta = ManganelMeta::create($model)->field('parentId');
     $this->assertTrue(!empty($meta->sanitizer), 'That has sanitizer explicitly set');
     $data = SearchArray::fromModel($model);
     $this->assertInternalType('string', $data['parentId']);
     $fromArray = SearchArray::toModel($data);
     $this->assertInternalType('string', $fromArray->parentId);
 }
示例#5
0
 /**
  * This will filter out:
  *
  * 1. Fields marked with:
  * ```
  * @Search(false)
  * ```
  * 2. Secret fields marked in any way with:
  * ```
  * @Secret
  * ```
  * 3. Non-persistent fields marked with:
  * ```
  * @Persistent(false)
  * ```
  *
  * @param AnnotatedInterface $model
  * @param DocumentPropertyMeta $fieldMeta
  * @return boolean
  */
 public function fromModel($model, DocumentPropertyMeta $fieldMeta)
 {
     $name = $fieldMeta->name;
     // Create Manganel meta instance of field
     $meta = ManganelMeta::create($model)->field($name);
     // Skip if explicitly not searchable
     if (false === $meta->searchable) {
         return false;
     }
     // Skip non-persistent fields
     if (false === $meta->persistent) {
         return false;
     }
     // Skip secret fields
     if ($meta->secret) {
         return false;
     }
     return true;
 }
示例#6
0
 protected static function getMeta(AnnotatedInterface $model)
 {
     return ManganelMeta::create($model);
 }
示例#7
0
 /**
  * @codeCoverageIgnore This is implicitly tested
  * @param AnnotatedInterface $model
  * @return static
  */
 public static function create(AnnotatedInterface $model)
 {
     $key = get_class($model);
     if (isset(self::$classToId[$key])) {
         $indexId = self::$classToId[$key];
     } else {
         $indexId = ManganelMeta::create($model)->type()->indexId;
         self::$classToId[$key] = $indexId;
     }
     return static::fly($indexId);
 }