protected function validate(Model $model) { $attributes = $model->attributesToArray(); $checkable = $model->getCheckable(); if ($checkable === null) { return; } $result = $checkable->check($attributes); if ($result->failed()) { throw new FailedCheckException($checkable, $result); } }
/** * Validate and discover all the relationships on a model. * * @return Relation[] */ protected function discoverRelations() { if ($this->relations === null) { $this->relations = []; Std::each(function ($name) { // Check that the relationship method is there. if (!method_exists($this->model, $name)) { throw new LackOfCoffeeException(vsprintf('The model declares a relationship named "%s" but' . ' there is no method with that name.', [$name])); } $relationReflector = new ReflectionMethod($this->model, $name); if (!$relationReflector->isPublic()) { throw new LackOfCoffeeException(vsprintf('The method for the relationship named "%s" should' . ' be public.', [$name])); } $argumentCount = $relationReflector->getNumberOfParameters(); if ($argumentCount > 0) { throw new LackOfCoffeeException(vsprintf('The method for the relationship named "%s" should' . ' take 0 arguments. However, it requires %d.', [$name, $argumentCount])); } $relation = $this->model->{$name}(); if (!$relation instanceof Relation) { throw new LackOfCoffeeException(vsprintf('The method for the relationship named "%s" should' . ' return an instance of %s. Got %s.', [$name, Relation::class, TypeHound::fetch($relation)])); } $this->relations[$name] = $relation; }, $this->model->getRelated()); } return $this->relations; }
/** * @inheritdoc */ public function newFromBuilder($attributes = [], $connection = null) { $model = parent::newFromBuilder($attributes, $connection = null); foreach ($this->getJsonFields() as $field) { $model->{$field} = json_decode($model->{$field}, true); } return $model; }