Ejemplo n.º 1
0
 /**
  * Create validator based on config.
  * @param AnnotatedInterface $model
  * @param ValidatorMeta $validatorMeta
  * @return ValidatorInterface Validator instance
  */
 public static function create(AnnotatedInterface $model, ValidatorMeta $validatorMeta, $fieldName = null)
 {
     $mn = Mangan::fromModel($model);
     // Resolve validator class
     if ($validatorMeta->proxy && empty($validatorMeta->class)) {
         if (isset($mn->validators[$validatorMeta->proxy])) {
             $validatorMeta->class = $mn->validators[$validatorMeta->proxy];
         } else {
             if (empty($fieldName)) {
                 $fieldName = '<not provided>';
             }
             $args = [get_class($model), $validatorMeta->proxy, $fieldName];
             $msg = vsprintf("Could not resolve validator class from proxy. For model `%s`. Proxy class: `%s`. Model field: `%s`", $args);
             throw new InvalidArgumentException($msg);
         }
     }
     if (empty($validatorMeta->class)) {
         $args = [get_class($model)];
         $msg = vsprintf("Empty validator class for model `%s`", $args);
         throw new InvalidArgumentException($msg);
     }
     $config = (array) $validatorMeta;
     unset($config['proxy']);
     $di = $mn->getDi();
     return $di->apply($config);
 }
Ejemplo n.º 2
0
 public function __construct($scenario = 'insert', $lang = '')
 {
     parent::__construct($scenario, $lang);
     $this->_id = new MongoId();
     $mangan = Mangan::fromModel($this);
     $this->_db = $mangan->getDbInstance();
 }
Ejemplo n.º 3
0
 public function __construct(AnnotatedInterface $model = null)
 {
     $this->model = $model;
     if (!$model) {
         $this->mn = Mangan::fly();
         return;
     }
     $this->mn = Mangan::fromModel($model);
 }
Ejemplo n.º 4
0
 public static function toModel($transformerClass, AnnotatedInterface $model)
 {
     $plugins = PluginFactory::fly()->instance(Mangan::fromModel($model)->finalizers, $transformerClass, ModelFinalizerInterface::class);
     foreach ($plugins as $finalizer) {
         /* @var $finalizer ArrayFinalizerInterface */
         $finalizer->toModel($model);
     }
     return $model;
 }
Ejemplo n.º 5
0
 public function testIfManganHasProperlyConfiguredFilters()
 {
     $model = new ExplicitlyNonIndexableField();
     // Check if mangan has properly configured filters
     $mangan = Mangan::fromModel($model);
     $filterConfig = $mangan->filters;
     $this->assertTrue(array_key_exists(SearchArray::class, $filterConfig), 'That SearchArray filters are configured');
     $this->assertTrue(in_array(SearchFilter::class, $filterConfig[SearchArray::class]), 'That SearchFilter is configured');
 }
Ejemplo n.º 6
0
 public function __construct($model)
 {
     // This is to use get/set
     foreach ($this->_getOptionNames() as $name) {
         PropertyMaker::defineProperty($this, $name, $this->_defaults);
     }
     foreach (ManganMeta::create($model)->type()->clientFlags as $name => $value) {
         $this->_values[$name] = $value;
     }
     $this->_mangan = Mangan::fromModel($model);
 }
Ejemplo n.º 7
0
 /**
  * Constructor
  *
  * @param object $model Model instance
  * @param EntityManagerInterface $em
  * @param Mangan $mangan
  */
 public function __construct($model, $em = null, $mangan = null)
 {
     $this->model = $model;
     $this->sm = new ScopeManager($model);
     if (null === $mangan) {
         $mangan = Mangan::fromModel($model);
     }
     $this->em = $em ?: EntityManager::create($model, $mangan);
     $this->mn = $mangan;
     $this->withCursor($this->mn->useCursor);
 }
Ejemplo n.º 8
0
 public function index()
 {
     if (!$this->isIndexable) {
         return;
     }
     // NOTE: Transformer must ensure that _id is string, not MongoId
     $body = SearchArray::fromModel($this->model);
     if (array_key_exists('_id', $body)) {
         $config = Mangan::fromModel($this->model)->sanitizersMap;
         if (!array_key_exists(SearchArray::class, $config)) {
             throw new UnexpectedValueException(sprintf('Mangan is not properly configured for Manganel. Signals must be generated or add configuration manually from `%s::getDefault()`', ConfigManager::class));
         } else {
             throw new UnexpectedValueException(sprintf('Cannot index `%s`, as it contains _id field. Either use MongoObjectId sanitizer on it, or rename.', get_class($this->model)));
         }
     }
     // In some cases $value *might* still be mongoId type,
     // see https://github.com/Maslosoft/Addendum/issues/43
     $func = function ($value) {
         if ($value instanceof MongoId) {
             return (string) $value;
         }
         return $value;
     };
     $filtered = filter_var($body, \FILTER_CALLBACK, ['options' => $func]);
     // Create proper elastic search request array
     $params = ['body' => $filtered];
     try {
         $this->getClient()->index($this->getParams($params));
     } catch (BadRequest400Exception $e) {
         // Throw previous exception,
         // as it holds more meaningfull information
         $previous = $e->getPrevious();
         $message = sprintf('Exception while indexing `%s`@`%s`: %s', get_class($this->model), $this->manganel->indexId, $previous->getMessage());
         throw new BadRequest400Exception($message);
     }
 }
Ejemplo n.º 9
0
 /**
  * Create entity manager
  * @param AnnotatedInterface $model
  * @param Mangan $mangan
  * @throws ManganException
  */
 public function __construct(AnnotatedInterface $model, Mangan $mangan = null)
 {
     $this->model = $model;
     $this->sm = new ScopeManager($model);
     $this->options = new EntityOptions($model);
     $this->collectionName = CollectionNamer::nameCollection($model);
     $this->meta = ManganMeta::create($model);
     $this->validator = new Validator($model);
     if (null === $mangan) {
         $mangan = Mangan::fromModel($model);
     }
     if (!$this->collectionName) {
         throw new ManganException(sprintf('Invalid collection name for model: `%s`', $this->meta->type()->name));
     }
     $this->_collection = new MongoCollection($mangan->getDbInstance(), $this->collectionName);
 }
Ejemplo n.º 10
0
 public function __construct(AnnotatedInterface $model = null)
 {
     parent::__construct($model);
     $this->available = new CommandProxyStorage($this, Mangan::fromModel($model)->connectionId);
 }