Beispiel #1
0
 /**
  * @param array $database
  * @return array
  * @throws Exceptions\BootstrapException
  */
 private function handleDatabaseOptions(array $database)
 {
     isset($database['connections']) && is_array($database['connections']) && count($database['connections']) ?: Error::dropBootstrapException("Missed mandatory 'connections' option!");
     isset($database['default']) || isset($database['connections']['default']) ?: Error::dropBootstrapException("Unable to define 'default' connection!");
     if (!isset($database['default']) && isset($database['connections']['default'])) {
         $database['default'] = 'default';
     }
     isset($database['connections'][$database['default']]) ?: Error::dropBootstrapException("Missed mandatory 'connections.{$database['default']}' option!");
     $database['fetch'] = \PDO::FETCH_CLASS;
     return $database;
 }
Beispiel #2
0
 /**
  * @param array $validate [ [attr, value, rules] ... ]
  * @throws \Enjoin\Exceptions\ValidationException
  */
 public function validate(array $validate)
 {
     $data = [];
     $rules = [];
     foreach ($validate as $it) {
         $data[$it[0]] = $it[1];
         $rules[$it[0]] = $it[2];
     }
     $validator = Factory::getValidator()->make($data, $rules);
     if ($validator->fails()) {
         $out = [];
         foreach ($validator->messages()->toArray() as $attr => $list) {
             $out[] = join(' ', $list);
         }
         Error::dropValidationException(join("\n", $out));
     }
 }
Beispiel #3
0
 /**
  * @param stdClass $parent
  * @param stdClass $child
  */
 private function handleRelation(stdClass $parent, stdClass $child)
 {
     $where = ['relatedKey' => $child->key];
     if ($child->as) {
         $where['as'] = $child->as;
     }
     $relation = Extras::findWhere($parent->Model->getDefinition()->getRelations(), $where);
     $missedErr = "Unable to find relation between '{$parent->Model->getUnique()}' " . "and '{$child->Model->getUnique()}', foreign key: '{$child->key}'.";
     $relation ?: Error::dropModelException($missedErr);
     $child->relation = $relation;
     $child->foreignKey = $relation->foreignKey;
     if ($relation->type === Extras::HAS_MANY) {
         $this->hasMany = true;
     }
     # Handle child `as`:
     if (!$child->as) {
         if ($relation->type === Extras::HAS_ONE || $relation->type === Extras::BELONGS_TO) {
             # On `hasOne`, `belongsTo`:
             $child->as = Inflector::singularize($child->Model->getTableName());
         } else {
             # On `hasMany`:
             $child->as = Inflector::pluralize($child->Model->getTableName());
         }
         $child->asProp = Inflector::camelize($child->as);
     } else {
         $child->asProp = $child->as;
     }
     # Handle child `prefix`:
     if (!$child->prefix) {
         $child->prefix = $parent->prefix ? $parent->prefix . Extras::GLUE_CHAR . $child->as : $child->as;
     }
 }
Beispiel #4
0
 /**
  * @param mixed $it
  * @param array $idxList
  * @return mixed
  * @throws \Enjoin\Exceptions\BuilderException
  */
 protected function findNode($it, array &$idxList)
 {
     $list = $this->tree->children;
     foreach ($idxList as $idx) {
         $list = $list[$idx]->children;
     }
     $as = null;
     if (is_array($it)) {
         $model = $it['model'];
         !isset($it['as']) ?: ($as = $it['as']);
     } else {
         $model = $it;
     }
     foreach ($list as $idx => $node) {
         if ($node->Model->getUnique() === $model->getUnique() && (!$as || $node->as === $as)) {
             $idxList[] = $idx;
             return $node;
         }
     }
     $errCtx = $this instanceof Group ? 'group' : 'order';
     Error::dropBuilderException("Invalid '{$errCtx}' model: '{$model->getUnique()}'");
 }
Beispiel #5
0
 /**
  * @param \Enjoin\Record\Record $Record
  * @param array $volume
  * @return mixed
  */
 private static function saveNonPersistent(Record $Record, array $volume)
 {
     $scope = $Record->scope();
     $Model = Enjoin::get($scope->modelName);
     $DB = $Model->connection();
     return $DB->transaction(function () use($volume, $scope, $Model, $DB) {
         $scope->type = self::PERSISTENT;
         if ($volume) {
             $Model->queryBuilder()->insert($volume) ?: Error::dropRecordException('Unable to insert record!');
         } else {
             $DB->insert($Model->dialectify()->getInsertEmptyQuery()) ?: Error::dropRecordException('Unable to insert empty record!');
         }
         $Model->cache()->flush();
         $id = $DB->getPdo()->lastInsertId($Model->dialectify()->getIdSequence());
         return (int) $id;
     });
 }
Beispiel #6
0
 /**
  * @return \Enjoin\Dialectify\Dialectify|null
  */
 public function dialectify()
 {
     if (!$this->Dialectify) {
         $map = ['mysql' => 'MySql', 'pgsql' => 'PostgreSql'];
         $driver = $this->connection()->getDriverName();
         isset($map[$driver]) ?: Error::dropModelException("Unknown dialectify driver: '{$driver}'!");
         $dialect = '\\Enjoin\\Dialectify\\' . $map[$driver];
         $this->Dialectify = new $dialect($this);
     }
     return $this->Dialectify;
 }