예제 #1
0
파일: Store.php 프로젝트: michaldudek/knit
 /**
  * Constructor.
  *
  * @param array                $config         Connection config.
  * @param CriteriaParser       $criteriaParser Knit criteria parser for MongoDB.
  * @param LoggerInterface|null $logger         [optional] Logger.
  *
  * @SuppressWarnings(PHPMD.CyclomaticComplexity,PHPMD.NPathComplexity)
  */
 public function __construct(array $config, CriteriaParser $criteriaParser, LoggerInterface $logger = null)
 {
     // check for all required info
     if (!ArrayUtils::checkValues($config, ['hostname', 'database'])) {
         throw new \InvalidArgumentException('Invalid config passed to ' . __CLASS__ . '.');
     }
     $this->criteriaParser = $criteriaParser;
     $this->logger = $logger ? $logger : new NullLogger();
     try {
         // define MongoClient options
         $options = ['db' => $config['database'], 'connect' => true];
         if (ArrayUtils::checkValues($config, ['username', 'password'])) {
             $options['username'] = $config['username'];
             $options['password'] = $config['password'];
         }
         if (isset($config['replica_set'])) {
             $options['replicaSet'] = $config['replica_set'];
         }
         // define the DSN or connection definition string
         $dsn = 'mongodb://' . $config['hostname'] . (isset($config['port']) && $config['port'] ? ':' . $config['port'] : '');
         // also add any additional hosts
         if (isset($config['hosts'])) {
             if (!is_array($config['hosts'])) {
                 throw new \InvalidArgumentException(__CLASS__ . ' config option "hosts" must be an array of hosts.');
             }
             foreach ($config['hosts'] as $host) {
                 if (!ArrayUtils::checkValues($host, ['hostname'])) {
                     throw new \InvalidArgumentException(__CLASS__ . ' config option "hosts" must be an array of array hosts definitions' . ' with at least "hostname" key.');
                 }
                 $dsn .= ',' . $host['hostname'] . (isset($host['port']) && $host['port'] ? ':' . $host['port'] : '');
             }
         }
         $this->dsn = $dsn;
         $this->options = $options;
         $this->databaseName = $config['database'];
     } catch (MongoException $e) {
         throw new StoreQueryErrorException($e->getMessage(), $e->getCode(), $e);
     }
 }
예제 #2
0
 public function testCheckValues()
 {
     $this->assertTrue(ArrayUtils::checkValues(array('key' => 'val', 'key1' => 'val1'), array('key', 'key1')));
     $this->assertFalse(ArrayUtils::checkValues(array('key' => 'val', 'key1' => ''), array('key', 'key1')));
     $this->assertFalse(ArrayUtils::checkValues(array('key' => 'val', 'key1' => 'val1'), array('key', 'key1', 'key2')));
     $this->assertFalse(ArrayUtils::checkValues(array('key' => 'val', 'key1' => null), array('key', 'key1')));
     $this->assertFalse(ArrayUtils::checkValues(array('key' => 'val', 'key1' => array()), array('key', 'key1')));
     $this->assertTrue(ArrayUtils::checkValues(array('key' => 'val', 'key1' => true), array('key', 'key1')));
     $this->assertTrue(ArrayUtils::checkValues(array('key' => 'val', 'key1' => false), array('key', 'key1')));
     $this->assertFalse(ArrayUtils::checkValues(array('key' => 'val', 'key1' => '   '), array('key', 'key1')));
     $this->assertTrue(ArrayUtils::checkValues(array('key' => 'val', 'key1' => 0), array('key', 'key1')));
     $this->assertTrue(ArrayUtils::checkValues(array('key' => 'val', 'key1' => '0'), array('key', 'key1')));
 }