/** * @param string|ModelInterface $nameOrModel The script/resource process, or a view model * @param null|array|\ArrayAccess $values Values to use during rendering * @return string The script output. */ public function render($nameOrModel, $values = null) { $port = $this->requestUri->getPort(); $host = $this->requestUri->getHost(); $host .= $port ? ':' . $port : ''; return $nameOrModel->getFormattedApiBlueprint($this->requestUri->getScheme(), $host); }
/** * Compares this product id with the provided $id * * $id can be either an integer, ProductId or Product * * @param mixed|ModelIdInterface|ModelInterface $id * * @return bool */ public function equals($id) { if ($id instanceof ModelIdInterface) { return $id->getValue() === $this->value; } elseif (is_a($id, $this->getModelClass(), true)) { $model = $id; if (!$model instanceof ModelInterface) { throw new \RuntimeException(sprintf('Model class %s must implement ModelInterface', get_class($model))); } return $this->equals($model->id()->getValue()); } return $this->getValue() === (string) $id; }
/** * @return void */ private function sanitize() { foreach ($this->model->rules() as $attr => $rules) { $filterBy = isset(self::$filterSanitize[$rules[0]]) ? self::$filterSanitize[$rules[0]] : self::$filterSanitize['string']; $this->model->{$attr} = filter_var($this->model->{$attr}, $filterBy); } }
/** * Get a message to show to the user * @todo Use the $escape parameter * @param \ModelInterface|string $model The model (or type) to show a message for * @param string $action The action that will be performed (softDelete, hardDelete, create or edit) * @param string $status The message's status (confirm, error or success) * @return string */ private function getMessage($model, $action, $status, $escape = true) { if ($model instanceof Model) { $type = strtolower($model->getTypeForHumans()); if ($model instanceof NamedModel) { // Twig will not escape the message on confirmation forms $name = $model->getName(); if ($status == 'confirm') { $name = Model::escape($name); } $messages = $this->getMessages($type, $name); return $messages[$action][$status]['named']; } else { $messages = $this->getMessages($type); return $messages[$action][$status]['unnamed']; } } else { $messages = $this->getMessages(strtolower($model)); return $messages[$action][$status]; } }
/** * Toggle a boolean value in a record * @param ModelInterface $model the model to toggle a property of * @param string $field_name the field to toggle * @throws \OutOfBoundsException */ public function toggleBoolean(ModelInterface $object, $field_name) { $sql = "UPDATE\n `{$this->tableName}`\n SET\n `{$field_name}` = (1 - `{$field_name}`)\n WHERE\n `{$this->uniqueReferenceField}`=:unique_reference\n "; $statement = $this->databaseEngine->prepare($sql); $value = $object->getUniqueReferenceValue(); $statement->bindParam(':unique_reference', $value, 'int' == $this->uniqueReferenceFieldType ? \PDO::PARAM_INT : \PDO::PARAM_STR); $statement->execute(); if (1 !== $statement->rowCount()) { throw new \OutOfBoundsException("Record in table '{$this->table}' with reference of '{$value}' was not found in the database when toggling the " . $field_name . " field"); } }
/** * Delete the model from the database * @param ModelInterface $model * @return void * @throws \OutOfBoundsException */ public function delete(ModelInterface $object) { $sql = "DELETE FROM\n `{$this->tableName}`\n WHERE\n `{$this->uniqueReferenceField}`=:unique_reference\n LIMIT\n 1"; $statement = $this->databaseEngine->prepare($sql); $statement->bindParam(':unique_reference', $object->getUniqueReferenceValue(), 'int' == $this->uniqueReferenceFieldType ? \PDO::PARAM_INT : \PDO::PARAM_STR); $statement->execute(); if (1 == $statement->rowCount()) { throw new \OutOfBoundsException("Record in table '{$this->tableName}' with reference of '{$object->getUniqueReferenceValue()}' was not found in the database when deleting"); } }