/** * @param string $className * @return ValidationRule[] */ private function createValidationRules(string $className) : array { $validationRules = []; $metadata = $this->metadataRepository->getMetadata($className); $properties = $metadata->getProperties(); foreach ($properties as $name => $property) { $rules = []; $this->addIntegerRule($property, $rules); } return $validationRules; }
public function serialize(EntityModel $model) : array { $objectMetadata = $this->metadataRepository->getMetadata($model); $properties = $objectMetadata->getProperties(); $result = []; foreach ($properties as $property) { $name = $property->getName(); $getter = $property->getGetter(); $type = $property->getType(); $value = $model->{$getter}(); $result[$name] = $this->serializeProperty($type, $value); } return $result; }
/** * @param string $className * @param array $data * @param bool $fullHydration * * @return mixed * * @throws \InvalidArgumentException * @throws \RuntimeException */ public function hydrate(string $className, array $data, bool $fullHydration = false) { $objectMetadata = $this->metadataRepository->getMetadata($className); $target = $objectMetadata->getReflectionClass()->newInstance(); foreach ($data as $key => $value) { $property = $objectMetadata->getProperty($key); if ($property && ($fullHydration || !$property->isReadOnly())) { $type = $property->getType(); $value = array_key_exists($key, $data) ? $data[$key] : null; $hydratedValue = $this->hydrateProperty($type, $value); $objectMetadata->setValue($target, $key, $hydratedValue); } } return $target; }
/** * @param EntityModel $model * @throws ValidationException */ public function validate(EntityModel $model) { $metadata = $this->metadataRepository->getMetadata($model); $metadata->validate($model); }