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); } } } }
/** * @param string $name * @return Startgroup */ private function getStartgroup($name) { $name = $this->translate($name); // some special cases $competition = null; if ($name == 'Junior Expert (male)' || $name == 'Expert (male)') { $competition = $this->competitions->get('Individual Freestyle (male)'); $startgroupName = str_replace(' (male)', '', $name); } else { if ($name == 'Junior Expert (female)' || $name == 'Expert (female)') { $competition = $this->competitions->get('Individual Freestyle (female)'); $startgroupName = str_replace(' (female)', '', $name); } } if ($competition === null) { $words = new Set(); foreach (array_values($this->translations) as $names) { $words->addAll(Text::create($names)->split(' ')); } $startgroupName = trim(str_replace($words->toArray(), '', $name)); $words = Text::create($startgroupName)->split(' '); $competitionName = preg_replace('/\\s\\s+/', ' ', trim(str_replace($words->toArray(), '', $name))); if (!$this->competitions->has($competitionName)) { throw new \Exception('Cannot find competition for ' . $competitionName); } $competition = $this->competitions->get($competitionName); } $startgroup = new Startgroup(); $startgroup->setName($startgroupName); $startgroup->setSlug(NameUtils::dasherize(strtolower($startgroupName))); $startgroup->setCompetition($competition); return $startgroup; }