public static function defineClass($className, $extends = '') { // $namespace = 'Eddmash\PowerOrm\Migration\Model'; $namespace = ''; $use = ''; $extendedClass = ''; if (empty($extends) || Model::isModelBase($extends)) { $extends = Model::getFullClassName(); } else { $extendedClass = sprintf('%s%s', ClassHelper::getFormatNamespace($namespace, true), $extends); $use = sprintf('use %s;', $extendedClass); $extends = trim(substr($extends, strripos($extends, '\\')), '\\'); } if (!StringHelper::isEmpty($extendedClass) && !ClassHelper::classExists($extendedClass, $namespace)) { self::$deferedClasses[$extends][] = ['class' => $className, 'extends' => $extends]; return false; } $extends = ClassHelper::getNameFromNs($extends, $namespace); $class = sprintf(self::getTemplate(), $namespace, $use, $className, $extends); $className = sprintf('%s%s', ClassHelper::getFormatNamespace($namespace, true), $className); if (ArrayHelper::hasKey(self::$deferedClasses, $className)) { foreach (self::$deferedClasses[$className] as $deferedClass) { self::defineClass($deferedClass['class'], $deferedClass['extends']); } } if (!ClassHelper::classExists($className, $namespace)) { eval($class); } return $className; }
public function toLevel($level) { $level = $level === null ? static::ERROR : $level; if (is_string($level)) { $level = ArrayHelper::getValue($this->levelsMap, strtolower($level)); } return $level; }
public function __construct($kwargs) { if (!isset($kwargs['rel']) || isset($kwargs['rel']) && $kwargs['rel'] === null) { $kwargs['rel'] = ManyToManyRel::createObject(['fromField' => $this, 'to' => ArrayHelper::getValue($kwargs, 'to'), 'through' => ArrayHelper::getValue($kwargs, 'through'), 'throughFields' => ArrayHelper::getValue($kwargs, 'throughFields'), 'dbConstraint' => ArrayHelper::getValue($kwargs, 'dbConstraint', true)]); } $this->hasNullKwarg = ArrayHelper::hasKey($kwargs, 'null'); parent::__construct($kwargs); }
public function __construct($kwargs) { $kwargs['unique'] = true; if (!isset($kwargs['rel']) || isset($kwargs['rel']) && $kwargs['rel'] == null) { $kwargs['rel'] = OneToOneRel::createObject(['fromField' => $this, 'to' => ArrayHelper::getValue($kwargs, 'to'), 'toField' => ArrayHelper::getValue($kwargs, 'toField'), 'parentLink' => ArrayHelper::getValue($kwargs, 'parentLink'), 'onDelete' => ArrayHelper::getValue($kwargs, 'onDelete', Delete::CASCADE)]); } parent::__construct($kwargs); }
public function __construct($kwargs) { if (!isset($kwargs['rel']) || isset($kwargs['rel']) && $kwargs['rel'] == null) { $kwargs['rel'] = ManyToOneRel::createObject(['fromField' => $this, 'to' => ArrayHelper::getValue($kwargs, 'to'), 'toField' => ArrayHelper::getValue($kwargs, 'toField'), 'parentLink' => ArrayHelper::getValue($kwargs, 'parentLink'), 'onDelete' => ArrayHelper::getValue($kwargs, 'onDelete', Delete::CASCADE)]); } $this->toField = ArrayHelper::getValue($kwargs, 'toField'); $this->fromField = 'this'; parent::__construct($kwargs); }
/** * {@inheritdoc} */ public function updateState($state) { /** @var $modelState ModelState */ $modelState = $state->modelStates[$this->name]; $meta = $modelState->meta ? $modelState->meta : []; $meta = array_replace($meta, $this->meta); foreach (self::$alterableOptions as $alterableOption) { if (!ArrayHelper::hasKey($this->meta, $alterableOption) && ArrayHelper::hasKey($meta, $alterableOption)) { unset($meta[$alterableOption]); } } $modelState->meta = $meta; }
public function getRegisteredModel($name) { $model = ArrayHelper::getValue($this->allModels, $name); if ($model == null) { throw new LookupError(sprintf("Model '%s' not registered.", $name)); } return $model; }
/** * Configures an object with the initial property values. * * @param object $object the object to be configured * @param array $properties the property initial values given in terms of name-value pairs * @param array $map if set the the key should be a key on the $properties and the value should a a property on * the $object to which the the values of $properties will be assigned to * * @return object the object itself */ public static function configure($object, $properties, $map = []) { if (empty($properties)) { return $object; } foreach ($properties as $name => $value) { if (ArrayHelper::hasKey($map, $name)) { $name = $map[$name]; } if (property_exists($object, $name)) { $object->{$name} = $value; } } return $object; }
/** * Given a node, returns a list of which dependent nodes (dependencies) must be unapplied,ending with the node itself. * * i.e All nodes that depend on the existence of this node. * * This is the list you would follow if removing the migrations from a database. * * This puts the last child as the first element on the returned array while the node becomes the last. * * @param $node * * @return mixed * * @throws NodeNotFoundError */ public function getDecedentsTree($node) { if (!ArrayHelper::hasKey($this->nodes, $node)) { throw new NodeNotFoundError(sprintf('Migration with the name %s does not exist', $node)); } return $this->getNodeFamilyTree($node)->getDescendants(); }
/** * {@inheritdoc} */ public function getConstructorArgs() { $kwargs = parent::getConstructorArgs(); if (ArrayHelper::hasKey($kwargs, 'onDelete')) { $kwargs['onDelete'] = $this->relation->onDelete; } if (is_string($this->relation->toModel)) { $kwargs['to'] = $this->relation->toModel; } else { $name = $this->relation->toModel->getFullClassName(); $kwargs['to'] = ClassHelper::getNameFromNs($name, BaseOrm::getModelsNamespace()); } if ($this->relation->parentLink) { $kwargs['parentLink'] = $this->relation->parentLink; } return $kwargs; }
/** * Migrates the database up to the given targets. * * @param $targets * @param $plan * @param $fake * * @since 1.1.0 * * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**> */ public function migrate($targets, $plan, $fake) { if (empty($plan)) { $plan = $this->getMigrationPlan($targets); } $migrationsToRun = $this->getMigrationsFromPlan($plan); // the full plan that would be executed if we to run on a new database $fullPlan = $this->getMigrationsFromPlan($this->getMigrationPlan($this->loader->graph->getLeafNodes(), true)); // Holds all states right before a migration is applied // if the migration is being run. $states = []; $state = ProjectState::createObject(); //Phase 1 -- create all project states before a migration is (un)applied /** @var $migration Migration */ foreach ($fullPlan as $migName => $migration) { // we use the migration to mutate state // after we mutate we remove the migration from the $migrationsToRun list. // so if we get to a point where we dont have any more $migrationsToRun break // this is to avoid any further mutations by other migrations not in the list. if (empty($migrationsToRun)) { break; } $run = ArrayHelper::hasKey($migrationsToRun, $migName); if ($run) { $states[$migName] = $state->deepClone(); unset($migrationsToRun[$migName]); } // $run will be false if the migration is not in the $migrationsToRun list // so there is not need to preserve state else if its in the list we need to we will get a new state object // that has been altered by the migration. // we do this because we need the object stored in the states array in the condition it was right before // the migration was applied. // remember in PHP objects are passed by reference. $state = $migration->updateState($state, $run); } // Phase 2 -- Run the migrations foreach ($plan as $mName => $migrationMeta) { if ($migrationMeta['unapply']) { $this->unApplyMigration($states[$mName], $migrationMeta['migration'], $fake); } else { $this->applyMigration($states[$mName], $migrationMeta['migration'], $fake); } } }
/** * Gives the user an option to choose from. Giving '?' as an input will show * a list of options to choose from and their explanations. * * @param string $prompt the prompt message * @param array $options Key-value array of options to choose from * * @return string An option character the user chose */ public static function select($prompt, $options = []) { top: static::stdout("{$prompt} [" . implode(',', array_keys($options)) . ',?]: '); $input = static::stdin(); if ($input === '?') { foreach ($options as $key => $value) { static::output(" {$key} - {$value}"); } static::output(' ? - Show help'); goto top; } elseif (!ArrayHelper::hasKey($options, $input)) { goto top; } return $input; }
/** * Add the current object to the passed in object. * * @param string $propertyName the name map the current object to, in the class object passed in * @param Model $classObject the object to attach the current object to * * @since 1.1.0 * * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**> */ public function contributeToClass($propertyName, $classObject) { $classObject->{$propertyName} = $this; $this->modelName = $this->getName($classObject->getFullClassName()); $this->scopeModel = $classObject; // override with the configs now. foreach (static::$DEFAULT_NAMES as $defaultName) { if (ArrayHelper::hasKey($this->overrides, $defaultName)) { $this->{$defaultName} = $this->overrides[$defaultName]; } } if ($this->dbTable == null) { $this->dbTable = $this->_getTableName(); } $vName = $this->verboseName; $this->verboseName = empty($vName) ? ucwords(StringHelper::camelToSpace($this->modelName)) : $vName; }
public function __construct($config = []) { // max_length=254 to be compliant with RFCs 3696 and 5321 $config['maxLength'] = ArrayHelper::getValue($config, 'maxLength', 254); parent::__construct($config); }
/** * @dataProvider providerHasKey * * @param $array * @param $key * @param $expectedValue * * @since 1.1.0 * * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**> */ public function testArrayHasKey($array, $key, $expectedValue) { $this->assertEquals($expectedValue, ArrayHelper::hasKey($array, $key)); }
/** * This method will try to update the model. If the model was updated (in the sense that an update query was done * and a matching row was found from the DB) the method will return True. * * @param $records * @param $pkValue * @param $forceUpdate * * @return bool|int * * @since 1.1.0 * * @author Eddilbert Macharia (http://eddmash.com) <*****@*****.**> */ private function doUpdate($records, $pkValue, $forceUpdate) { $filtered = $this->objects()->filter([$this->meta->primaryKey->name => $pkValue]); // We can end up here when saving a model in inheritance chain where // update_fields doesn't target any field in current model. In that // case we just say the update succeeded. Another case ending up here // is a model with just PK - in that case check that the PK still // exists. if (ArrayHelper::isEmpty($records)) { return $filtered->exists(); } if (!$forceUpdate) { // It may happen that the object is deleted from the DB right after // this check, causing the subsequent UPDATE to return zero matching // rows. The same result can occur in some rare cases when the // database returns zero despite the UPDATE being executed // successfully (a row is matched and updated). In order to // distinguish these two cases, the object's existence in the // database is again checked for if the UPDATE query returns 0. if ($filtered->exists()) { return $filtered->_update($records) || $filtered->exists(); } else { return false; } } return $filtered->_update($records); }
public function execute() { if (ArrayHelper::isEmpty(ArrayHelper::getValue($this->qb->getQueryParts(), 'select', null))) { $this->qb->select('*'); } if (ArrayHelper::isEmpty(ArrayHelper::getValue($this->qb->getQueryParts(), 'from', null))) { $this->qb->from($this->model->meta->dbTable); } return $this->qb->execute()->fetchAll(PDO::FETCH_ASSOC); }
public function getFieldByName($name) { if (ArrayHelper::hasKey($this->fields, $name)) { return ArrayHelper::getValue($this->fields, $name); } throw new ValueError(sprintf('No field called [ %s ] on model [ %s ]', $name, $this->name)); }