Ejemplo n.º 1
0
 /**
  * Load defined relations
  * @param \Spot\Entity\EntityInterface $entity
  * @param string $name
  * @param bool $reload
  * @return \Spot\Entity\EntityInterface
  * @throws \InvalidArgumentException
  */
 public function loadRelation($entity, $name, Mapper $mapper, $reload = false)
 {
     $entityName = $entity instanceof ResultsetInterface ? $entity->getEntityName() : $entity->toString();
     if (empty($entityName)) {
         throw new \InvalidArgumentException("Cannot load relation with a null \$entityName");
     }
     $relations = $this->entityManager->getRelations($entityName);
     if (isset($relations[$name])) {
         return $this->loadRelationObject($entity, $name, $relations[$name], $mapper);
     }
     return $entity;
 }
Ejemplo n.º 2
0
 /**
  * Replace entity value placeholders on relation definitions
  * Currently replaces ':entity.[col]' with the field value from the passed entity object
  * @param \Spot\Entity\EntityInterface|\Spot\Entity\ResultsetInterface $entity
  * @param array $conditions
  * @param string $replace
  * @return array
  */
 public function resolveEntityConditions($entity, array $conditions, $replace = ':entity.')
 {
     // Load foreign keys with data from current row
     // Replace ':entity.[col]' with the field value from the passed entity object
     if ($conditions) {
         $sourceTable = $this->entityManager->getTable($this->sourceEntity());
         $relationTable = $this->entityManager->getTable($this->relationEntityName());
         foreach ($conditions as $relationCol => $col) {
             if (is_string($relationCol) && false !== strpos($relationCol, ':relation.')) {
                 unset($conditions[$relationCol]);
                 $relationCol = str_replace(':relation.', $relationTable . '.', $relationCol);
             }
             if (is_string($col) && false !== strpos($col, $replace)) {
                 $col = str_replace($replace, '', $col);
                 if ($entity instanceof EntityInterface) {
                     $conditions[$relationCol] = $entity->{$col};
                 } else {
                     if ($entity instanceof ResultsetInterface) {
                         $conditions[$relationCol] = array_unique($entity->toArray($col));
                     }
                 }
             }
         }
     }
     return $conditions;
 }
Ejemplo n.º 3
0
 /**
  * Run set validation rules on fields
  * @param \Spot\Entity\EntitInterface $entity
  * @return bool
  * @todo A LOT more to do here... More validation, break up into classes with rules, etc.
  */
 public function validate(EntityInterface $entity)
 {
     return true;
     $entityName = $entity->toString();
     $v = new \Valitron\Validator($entity->data());
     // Check validation rules on each feild
     foreach ($this->entityManager->fields($entityName) as $field => $fieldAttrs) {
         // Required field
         if (isset($fieldAttrs['required']) && true === $fieldAttrs['required']) {
             $v->rule('required', $field);
         }
         // Unique field
         if (isset($fieldAttrs['unique']) && true === $fieldAttrs['unique']) {
             if ($this->first($entityName, [$field => $entity->{$field}]) !== false) {
                 $entity->error($field, "" . ucwords(str_replace('_', ' ', $field)) . " '" . $entity->{$field} . "' is already taken.");
             }
         }
         // Valitron validation rules
         if (isset($fieldAttrs['validation']) && is_array($fieldAttrs['validation'])) {
             foreach ($fieldAttrs['validation'] as $rule => $ruleName) {
                 $params = [];
                 if (is_string($rule)) {
                     $params = $ruleName;
                     $ruleName = $rule;
                 }
                 $params = array_merge([$ruleName, $field], $params);
                 call_user_func_array([$v, 'rule'], $params);
             }
         }
     }
     !$v->validate() && $entity->errors($v->errors(), false);
     // Return error result
     return !$entity->hasErrors();
 }