/** * @param FREST\Resource $resource * @return array */ private function generateTableDeleteSpecs($resource) { $tableDeleteSpecs = array(); $tableSettings = $resource->getTableSettings(); /** @var Setting\Table $tableSetting */ foreach ($tableSettings as $tableSetting) { $tableDeleteSpec = new Spec\TableDelete($tableSetting->getTable()); $tableDeleteSpecs[] = $tableDeleteSpec; } if (count($tableDeleteSpecs) > 0) { return $tableDeleteSpecs; } return NULL; }
/** * @param string $alias * @param string $resourceName * @param array $parameters * @param bool $default */ public function __construct($alias, $resourceName, $parameters, $default = FALSE) { $this->alias = $alias; $this->resourceName = $resourceName; $this->parameters = $parameters; $this->default = $default; foreach ($parameters as $field => $parameter) { $injectedAlias = Resource::aliasFromInjectedValue($parameter); if (isset($injectedAlias)) { $this->requiredAliases[$field] = $injectedAlias; } } }
/** * @throws FREST\Exception */ protected function setupResourceFunction() { $resourceFunctions = $this->resource->getResourceFunctions(); // check if valid Func name if (!isset($resourceFunctions) || !isset($resourceFunctions[$this->resourceFunctionName])) { throw new FREST\Exception(FREST\Exception::ResourceFunctionDoesntExist, "Function name: '{$this->resourceFunctionName}'"); } /** @var Func\Resource $resourceFunction */ $resourceFunction = $resourceFunctions[$this->resourceFunctionName]; $resourceFunctionParameters = $resourceFunction->getParameters(); // check method $requiredMethod = $resourceFunction->getMethod(); $currentMethod = $this->frest->getMethod(); if ($requiredMethod != $currentMethod) { $currentMethodString = Type\Method::getString($currentMethod); $requiredMethodString = Type\Method::getString($requiredMethod); throw new FREST\Exception(FREST\Exception::MismatchingResourceFunctionMethod, "Requires '{$requiredMethodString}' but using '{$currentMethodString}'"); } // check for invalid parameters and build parameter list for Func $functionParameters = array(); foreach ($this->parameters as $parameterName => $value) { $isValidMiscParam = isset($this->miscParameters[$parameterName]); if (!$isValidMiscParam) { if (!isset($resourceFunctionParameters[$parameterName])) { throw new FREST\Exception(FREST\Exception::InvalidFunctionParameter, "Parameter name: '{$parameterName}'"); } $functionParameters[$parameterName] = $value; } } if (isset($resourceFunctionParameters) && count($resourceFunctionParameters) > 0) { // check for all required parameters /** @var Func\FunctionParam $parameter */ $missingParameterNames = array(); foreach ($resourceFunctionParameters as $parameterName => $parameter) { // check type if (isset($functionParameters[$parameterName])) { $variableType = $parameter->getVariableType(); $value = $functionParameters[$parameterName]; $castedValue = Type\Variable::castValue($value, $variableType); if (!isset($castedValue)) { $typeString = Type\Variable::getString($variableType); throw new FREST\Exception(FREST\Exception::InvalidType, "Expecting parameter '{$parameterName}' to be of type '{$typeString}' but received '{$value}'"); } $functionParameters[$parameterName] = $castedValue; } else { if ($parameter->getRequired()) { $missingParameterNames[] = $parameterName; } } } if (count($missingParameterNames) > 0) { $missingString = implode(', ', $missingParameterNames); throw new FREST\Exception(FREST\Exception::MissingRequiredFunctionParameter, "Parameter name: '{$missingString}'"); } } // Check for Func implementation existence if (!method_exists($this->resource, $this->resourceFunctionName)) { throw new FREST\Exception(FREST\Exception::ResourceFunctionMissing, "Function name: '{$this->resourceFunctionName}', resource: '{$this->resource->getName()}'"); } if ($resourceFunction->getRequiresResourceID()) { $functionParameters['resourceID'] = $this->resourceID; } $this->resourceFunction = $resourceFunction; $this->resourceFunctionParameters = $functionParameters; }
/** * @param \FREST\Resource $resource */ private function setupResource($resource) { $resource->setDefaultLimit($this->config->getDefaultLimit()); $resource->setMaxLimit($this->config->getMaxLimit()); $resource->setAllowWildcards($this->config->getAllowWildcards()); $resource->setAllowFieldsParameter($this->config->getAllowFieldsParameter()); $resource->setAllowPartialSyntax($this->config->getAllowPartialSyntax()); $resource->setup(); // this is where Settings are created by custom class }
/** * @param FREST\Resource $resource * @param array $queryParameterSpecs * @return int * @throws FREST\Exception */ private function generateLimit($resource, &$queryParameterSpecs) { $limit = isset($this->parameters['limit']) ? $this->parameters['limit'] : $resource->getDefaultLimit(); $castedLimit = Type\Variable::castValue($limit, Type\Variable::INT); if (!isset($castedLimit)) { $typeString = Type\Variable::getString(Type\Variable::INT); throw new FREST\Exception(FREST\Exception::InvalidType, "Expecting offset to be of type '{$typeString}' but received '{$limit}'."); } $maxLimit = $resource->getMaxLimit(); if ($castedLimit > $maxLimit) { throw new FREST\Exception(FREST\Exception::InvalidValue, "The limit for this resource must not exceed {$maxLimit}. A limit of {$castedLimit} was supplied."); } $limitQueryParameter = new Spec\QueryParameter('limit', ':_limit', $castedLimit, Type\Variable::INT); $queryParameterSpecs['_limit'] = $limitQueryParameter; return $castedLimit; }
/** * @param FREST\Resource $resource * @param array $createSpecs * @return array|NULL */ protected function generateTableCreateSpecs($resource, $createSpecs) { $tableCreateSpecs = array(); $tablesAndTheirCreateSpecs = array(); /** @var Spec\Create $createSpec */ foreach ($createSpecs as $createSpec) { $alias = $createSpec->getAlias(); $field = $resource->getFieldForAlias($alias); $table = $resource->getTableForField($field); $tablesAndTheirCreateSpecs[$table][] = $createSpec; } foreach ($tablesAndTheirCreateSpecs as $table => $createSpecs) { $tableCreateSpec = new Spec\TableCreate($table, $createSpecs); $tableCreateSpecs[] = $tableCreateSpec; } if (count($tableCreateSpecs) > 0) { return $tableCreateSpecs; } return NULL; }
/** * @param FREST\Resource $loadedResource * @param Setting\Read $responsibleReadSetting * * @return array */ protected function getLoadedResourceReadSettings($loadedResource, $responsibleReadSetting) { $resourceName = $loadedResource->getName(); if (!isset($this->loadedResourceReadSettings[$resourceName])) { $parameters = array(); if ($responsibleReadSetting instanceof Setting\SingularResourceRead) { $aliasesToRead = $responsibleReadSetting->getAliasesToRead(); if (isset($aliasesToRead)) { $parameters['fields'] = implode(',', $aliasesToRead); } } else { if ($responsibleReadSetting instanceof Setting\ComputedRead) { $requiredAliases = $responsibleReadSetting->getRequiredAliases(); if (isset($requiredAliases)) { $parameters['fields'] = implode(',', $requiredAliases); } } } $readSettings = $this->generateReadSettings($loadedResource, $parameters); $this->loadedResourceReadSettings[$resourceName] = $readSettings; } else { $readSettings = $this->loadedResourceReadSettings[$resourceName]; } return $readSettings; }
/** * @param FREST\Resource $resource * @param array $updateSpecs * @return array|NULL */ protected function generateTableUpdateSpecs($resource, $updateSpecs) { $tableUpdateSpecs = array(); $tablesAndTheirUpdateSpecs = array(); /** @var Spec\Update $updateSpec */ foreach ($updateSpecs as $updateSpec) { $alias = $updateSpec->getAlias(); $field = $resource->getFieldForAlias($alias); $table = $resource->getTableForField($field); $tablesAndTheirUpdateSpecs[$table][] = $updateSpec; } foreach ($tablesAndTheirUpdateSpecs as $table => $updateSpecs) { $tableUpdateSpec = new Spec\TableUpdate($table, $updateSpecs); $tableUpdateSpecs[] = $tableUpdateSpec; } return $tableUpdateSpecs; }