protected function hydrateToOneRelationship($rel, $model, $data) { $method = 'doSet' . NameUtils::toStudlyCase($rel) . 'Id'; if (method_exists($this, $method) && isset($data['id']) && !empty($data['id'])) { $this->{$method}($model, $data['id']); } }
public function validate($model) { $builder = new ValidatorBuilder(); $builder->setTranslator($this->service->getTranslator()); $validator = $builder->getValidator(); $validations = $this->getValidations(); $this->violations = new ConstraintViolationList(); foreach ($validations as $column => $validation) { $method = 'get' . NameUtils::toStudlyCase($column); if (method_exists($model, $method)) { $value = $model->{$method}(); $constraints = []; foreach ($validation as $options) { $name = $options['constraint']; unset($options['constraint']); $constraints[] = $this->getConstraint($name, $options); } $violations = $validator->validate($value, $constraints); $this->violations->addAll($violations); } } return (bool) (!(count($this->violations) > 0)); }
/** * @param mixed $query * @param mixed $filter * @return void */ protected function applyFilter($query, $filter) { if (is_array($filter)) { // filter by fields if (isset($filter['fields'])) { foreach ($filter['fields'] as $column => $value) { $pos = strpos($column, '.'); if ($pos !== false) { $rel = NameUtils::toStudlyCase(substr($column, 0, $pos)); $col = substr($column, $pos + 1); $method = 'use' . $rel . 'Query'; if (method_exists($query, $method)) { $sub = $query->{$method}(); $this->applyFilter($sub, ['fields' => [$col => $value]]); $sub->endUse(); } } else { $method = 'filterBy' . NameUtils::toStudlyCase($column); if (method_exists($query, $method)) { $query->{$method}($value); } } } } // filter by features if (isset($filter['features'])) { $features = new Text($filter['features']); if ($features->contains('random')) { $query->addAscendingOrderByColumn('RAND()'); } } } if (method_exists($this, 'filter')) { $this->filter($query, $filter); } }
/** * @param mixed $query * @param mixed $filter * @return void */ protected function applyFilter($query, $filter) { foreach ($filter as $column => $value) { $pos = strpos($column, '.'); if ($pos !== false) { $rel = NameUtils::toStudlyCase(substr($column, 0, $pos)); $col = substr($column, $pos + 1); $method = 'use' . $rel . 'Query'; if (method_exists($query, $method)) { $sub = $query->{$method}(); $this->applyFilter($sub, [$col => $value]); $sub->endUse(); } } else { $method = 'filterBy' . NameUtils::toStudlyCase($column); if (method_exists($query, $method)) { $query->{$method}($value); } } } }