/** * @param $id * @param $className * @param callable $findOneCallable * @param callable $deleteCallable * * @return \Symfony\Component\HttpFoundation\Response */ public function get($id, $className, callable $findOneCallable, callable $deleteCallable) { try { $data = $findOneCallable(); if (empty($data)) { $mapping = $this->serializer->getTransformer()->getMappingByClassName($className); return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)])); } $deleteCallable(); return $this->resourceDeleted(); } catch (Exception $e) { return $this->errorResponse(new ErrorBag([new Error('Bad Request', 'Request could not be served.')])); } }
/** * @param string|int $id * @param string $className * @param callable $callable * * @return \Symfony\Component\HttpFoundation\Response */ public function get($id, $className, callable $callable) { try { QueryObject::assert($this->serializer, $this->fields, $this->included, new Sorting(), $this->errorBag, $className); $data = $callable(); if (empty($data)) { $mapping = $this->serializer->getTransformer()->getMappingByClassName($className); return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)])); } $response = $this->response($this->serializer->serialize($data, $this->fields, $this->included)); } catch (Exception $e) { $response = $this->getErrorResponse($e); } return $response; }
/** * @param $id * @param array $data * @param $className * @param callable $findOneCallable * @param callable $update * * @return \Symfony\Component\HttpFoundation\Response */ public function get($id, array $data, $className, callable $findOneCallable, callable $update) { try { DataObject::assertPut($data, $this->serializer, $className, $this->errorBag); $model = $findOneCallable(); if (empty($model)) { $mapping = $this->serializer->getTransformer()->getMappingByClassName($className); return $this->resourceNotFound(new ErrorBag([new NotFoundError($mapping->getClassAlias(), $id)])); } $values = DataObject::getAttributes($data, $this->serializer); $update($model, $values, $this->errorBag); $response = $this->resourceUpdated($this->serializer->serialize($model)); } catch (Exception $e) { $response = $this->getErrorResponse($e); } return $response; }
/** * @param JsonApiSerializer $serializer * @param Included $included * @param string $paramName */ protected function validateIncludeQueryParamsTypes($serializer, Included $included, $paramName) { if (false === $included->isEmpty()) { $validateFields = array_keys($included->get()); foreach ($validateFields as $key => $field) { $mapping = $serializer->getTransformer()->getMappingByAlias($field); if (null !== $mapping) { $properties = $this->getPropertiesFromMapping($mapping); $invalidProperties = array_diff($included->get()[$field], $properties); $this->addInvalidParameterMemberErrorsToErrorBag($invalidProperties, $paramName, $field); unset($validateFields[$key]); } } $this->addInvalidParameterErrorsToErrorBag($paramName, $validateFields); } }
public function testGetTransformerReturnsJsonApiTransformer() { $serializer = new JsonApiSerializer(new JsonApiTransformer(new Mapper(HelperMapping::complex()))); $this->assertInstanceOf(JsonApiTransformer::class, $serializer->getTransformer()); }
/** * @param array $relationshipData * @param JsonApiSerializer $serializer * @param ErrorBag $errorBag */ protected static function relationshipDataAssert($relationshipData, JsonApiSerializer $serializer, ErrorBag $errorBag) { //Has type member. if (empty($relationshipData[JsonApiTransformer::TYPE_KEY]) || !is_string($relationshipData[JsonApiTransformer::TYPE_KEY])) { $errorBag[] = new MissingTypeError(); return; } //Provided type value is supported. if (null === $serializer->getTransformer()->getMappingByAlias($relationshipData[JsonApiTransformer::TYPE_KEY])) { $errorBag[] = new InvalidTypeError($relationshipData[JsonApiTransformer::TYPE_KEY]); return; } //Validate if attributes passed in make sense. if (!empty($relationshipData[JsonApiTransformer::ATTRIBUTES_KEY])) { $mapping = $serializer->getTransformer()->getMappingByAlias($relationshipData[JsonApiTransformer::TYPE_KEY]); $properties = str_replace(array_keys($mapping->getAliasedProperties()), array_values($mapping->getAliasedProperties()), $mapping->getProperties()); foreach (array_keys($relationshipData[JsonApiTransformer::ATTRIBUTES_KEY]) as $property) { if (false === in_array($property, $properties, true)) { $errorBag[] = new InvalidAttributeError($property, $relationshipData[JsonApiTransformer::TYPE_KEY]); } } } }
/** * @param JsonApiSerializer $serializer * @param string $className * @param Sorting $sorting * @param ErrorBag $errorBag */ protected static function validateSortParams(JsonApiSerializer $serializer, $className, Sorting $sorting, ErrorBag $errorBag) { if (false === $sorting->isEmpty()) { if ($mapping = $serializer->getTransformer()->getMappingByClassName($className)) { $aliased = (array) $mapping->getAliasedProperties(); $sortsFields = str_replace(array_values($aliased), array_keys($aliased), $sorting->fields()); $invalidProperties = array_diff($sortsFields, $mapping->getProperties()); foreach ($invalidProperties as $extraField) { $errorBag[] = new InvalidSortError($extraField); } } } }
/** * @param array $data * @param JsonApiSerializer $serializer * @param ErrorBag $errorBag * * @throws DataException */ protected static function assertAttributesExists(array $data, JsonApiSerializer $serializer, ErrorBag $errorBag) { $inputAttributes = array_keys($data[JsonApiTransformer::ATTRIBUTES_KEY]); $mapping = $serializer->getTransformer()->getMappingByAlias($data[JsonApiTransformer::TYPE_KEY]); $properties = str_replace(array_keys($mapping->getAliasedProperties()), array_values($mapping->getAliasedProperties()), $mapping->getProperties()); $properties = array_diff($properties, $mapping->getIdProperties()); $properties = array_merge($properties, $mapping->getHiddenProperties()); $hasErrors = false; foreach ($inputAttributes as $property) { if (false === in_array($property, $properties)) { $hasErrors = true; $errorBag[] = new InvalidAttributeError($property, $data[JsonApiTransformer::TYPE_KEY]); } } if ($hasErrors) { throw new DataException(); } }