/** * @param string $fieldName * @param FieldDefinition $type * @throws InvalidArgumentException */ private function addFieldDefinition(string $fieldName, FieldDefinition $type) { if (array_key_exists(mb_strtolower($fieldName), $this->definitions)) { throw InvalidArgumentException::fieldNameAlreadyExists($fieldName, $this->name); } $this->definitions[mb_strtolower($fieldName)] = $type; }
/** * @param $serializedValue * * @return float * @throws InvalidArgumentException */ public function deserialize($serializedValue) { if (null === $serializedValue && $this->isNullable()) { return $this->default === null ? null : $this->default; } if (!is_string($serializedValue)) { throw InvalidArgumentException::expected('string', $serializedValue); } return (double) $serializedValue; }
/** * @param string $serializedValue * * @return null|Association * @throws InvalidArgumentException */ public function deserialize($serializedValue) { if (null === $serializedValue && $this->isNullable()) { return null; } if (!is_string($serializedValue)) { throw InvalidArgumentException::expected('string', $serializedValue); } return new Association($this->schema, $this->typeSchema, ['id' => $serializedValue]); }
/** * @param TypeSchema $definition * @return bool * @throws InvalidArgumentException */ public function isValid(TypeSchema $definition) : bool { if (mb_strtolower($definition->name()) !== mb_strtolower($this->typeSchemaName())) { throw InvalidArgumentException::unexpectedModel($definition->name(), $this->typeSchemaName()); } foreach ($this->fields as $fieldName => $value) { $field = $definition->getFieldDefinition($fieldName); if (!$field->isValid($value)) { return false; } } return true; }