public function createAction() { $model = new Evaluation(); $input = Input::all(); $validator = $model->validateInput($input); if ($validator->fails()) { app()->abort(422, $validator->errors()->first()); } $audits = Input::get("auditResult"); foreach ($audits as $audit) { DB::transaction(function () use($model, $audit) { $assertion = new Assertion(["date" => new Carbon(), "mode" => $audit["mode"], "test_id" => $audit["test"]["@id"], "test_type" => $audit["test"]["@type"], "result_type" => $audit["result"]["@type"], "result_outcome" => $audit["result"]["outcome"]]); /** @var Assertor $assertor */ $assertor = Assertor::find(LDModel::getIdFromLdId(Input::get("creator.@id"))); $model->fill(["date" => new Carbon()]); $model->creator()->associate($assertor); $model->save(); /** @var Webpage $subject */ $subject = Webpage::find(LDModel::getIdFromLdId($audit["subject"])); if (!$subject) { app()->abort(422, "Subject not found"); } $assertion->assertor()->associate($assertor); $assertion->subject()->associate($subject); $assertion->evaluation()->associate($model); $assertion->save(); }); } return $this->response($model); }
public function getAction($id) { return $this->response(Assertor::find($id)); }
public function validateInput(array $values) { Validator::extend("check_context", function ($atts, $value, $parameters) use($values) { preg_match("~^.+/([a-zA-Z]+).jsonld\$~", $value, $matches); if (count($matches) == 0) { return false; } $model = ucfirst(str_singular($matches[1])); //0 = entire string $allowed_models = ["Webpage", "Evaluation", "Assertion", "Assertor"]; $class = "App\\Models\\" . $model; if (!class_exists($class) || !in_array($model, $allowed_models)) { return false; } /** @var LDModel $instance */ $instance = new $class(); if (!method_exists($instance, "getContext")) { return false; } return true; }); Validator::extend('private_key', function ($attr, $value, $parameters) use($values) { if (!isset($parameters[0])) { return false; } $ldid = array_get($values, $parameters[0]); $id = $this->getIdFromLdId($ldid); if (!$id) { return false; } $result = Assertor::where(['id' => $id, 'key_id' => $value])->first(); return $result != null; }); Validator::extend('ldid_exists', function ($attr, $value, $parameters) { preg_match("~^id:([a-z]+)/([0-9]+)\$~", $value, $matches); if (count($matches) == 0) { preg_match("~^utt:([a-z]+)/([0-9]+)\$~", $value, $matches); } if (count($matches) == 0) { preg_match("~^" . url() . "/([a-z]+)/([0-9]+)\$~", $value, $matches); } if (count($matches) != 3) { return false; } $id = $matches[2]; $model = "App\\Models\\" . str_singular(ucfirst($matches[1])); if (!class_exists($model)) { return false; } $result = $model::where(['id' => $id])->first(); return $result != null; }); Validator::extend('ldid_model', function ($attr, $value, $parameters) { preg_match("~^id:([a-z]+)/([0-9]+)\$~", $value, $matches); if (count($matches) == 0) { preg_match("~^utt:([a-z]+)/([0-9]+)\$~", $value, $matches); } if (count($matches) == 0) { preg_match("~^" . url() . "/([a-z]+)/([0-9]+)\$~", $value, $matches); } if (count($matches) != 3) { return false; } //limit results to specific model if (!isset($parameters[0])) { return false; } $invalidModel = $matches[1] != $parameters[0]; if ($invalidModel) { return false; } return true; }); return Validator::make($values, $this->getValidationRules()); }