/** * @param string $name * @param DynamicFieldKind $kind * @param mixed $value */ private function __construct($name, DynamicFieldKind $kind, $value) { Assertion::betweenLength($name, 1, 127); Assertion::regex($name, self::VALID_NAME_PATTERN, sprintf('DynamicField name [%s] must match pattern [%s].', $name, self::VALID_NAME_PATTERN)); $this->name = $name; $this->kind = $kind->getValue(); $field = self::createField($this->kind); $this->value = $field->getType()->decode($value, $field); $field->guardValue($this->value); }
/** * todo: review random failures from YamlSerializer on $id when no tag is present. * * @param SchemaCurie $curie * @param string $id * @param string $tag The tag will be automatically fixed to a slug-formatted-string. * @throws \Exception */ public function __construct(SchemaCurie $curie, $id, $tag = null) { $this->curie = $curie; $this->id = trim((string) $id) ?: 'null'; Assertion::regex($this->id, '/^[\\w\\/\\.:-]+$/', null, 'MessageRef.id'); if (null !== $tag) { $this->tag = strtolower(preg_replace('/[^\\w\\.-]/', '-', $tag)) ?: null; } if ($this->curie->isMixin()) { throw new LogicException('Mixins cannot be used in a MessageRef.'); } }
/** * {@inheritdoc} */ public function guard($value, Field $field) { parent::guard($value, $field); if ($pattern = $field->getPattern()) { Assertion::regex($value, $pattern, null, $field->getName()); } switch ($field->getFormat()->getValue()) { case Format::UNKNOWN: break; case Format::DATE: Assertion::regex($value, '/^\\d{4}-\\d{2}-\\d{2}$/', null, $field->getName()); break; case Format::DATE_TIME: Assertion::true(DateUtils::isValidISO8601Date($value), sprintf('Field [%s] must be a valid ISO8601 date-time. Format must match one of [%s], [%s] or [%s].', $field->getName(), DateUtils::ISO8601_ZULU, DateUtils::ISO8601, \DateTime::ISO8601), $field->getName()); break; case Format::SLUG: Assertion::regex($value, '/^([\\w\\/-]|[\\w-][\\w\\/-]*[\\w-])$/', null, $field->getName()); break; case Format::EMAIL: Assertion::email($value, null, $field->getName()); break; case Format::HASHTAG: Assertion::true(HashtagUtils::isValid($value), sprintf('Field [%s] must be a valid hashtag. @see HashtagUtils::isValid', $field->getName()), $field->getName()); break; case Format::IPV4: case Format::IPV6: /* * todo: need separate assertion for ipv4 and ipv6 */ Assertion::url('http://' . $value, sprintf('Field [%s] must be a valid [%s].', $field->getName(), $field->getFormat()->getValue()), $field->getName()); break; case Format::HOSTNAME: case Format::URI: case Format::URL: /* * fixme: need better handling for HOSTNAME, URI and URL... assertion library just has one "url" handling * but we really need separate ones for each of these formats. right now we're just prefixing * the value with a http so it looks like a url. this won't work for thinks like mailto: * urn:, etc. */ if (false === strpos($value, 'http')) { $value = 'http://' . $value; } Assertion::url($value, sprintf('Field [%s] must be a valid [%s].', $field->getName(), $field->getFormat()->getValue()), $field->getName()); break; case Format::UUID: Assertion::uuid($value, null, $field->getName()); break; default: break; } }
/** * @param string $name * @param Type $type * @param FieldRule $rule * @param bool $required * @param null|int $minLength * @param null|int $maxLength * @param null|string $pattern * @param null|string $format * @param null|int $min * @param null|int $max * @param int $precision * @param int $scale * @param null|mixed $default * @param bool $useTypeDefault * @param null|string $className * @param null|array $anyOfClassNames * @param \Closure|null $assertion * @param bool $overridable */ public function __construct($name, Type $type, FieldRule $rule = null, $required = false, $minLength = null, $maxLength = null, $pattern = null, $format = null, $min = null, $max = null, $precision = 10, $scale = 2, $default = null, $useTypeDefault = true, $className = null, array $anyOfClassNames = null, \Closure $assertion = null, $overridable = false) { Assertion::betweenLength($name, 1, 127); Assertion::regex($name, self::VALID_NAME_PATTERN, sprintf('Field [%s] must match pattern [%s].', $name, self::VALID_NAME_PATTERN)); Assertion::boolean($required); Assertion::boolean($useTypeDefault); Assertion::boolean($overridable); /* * a message type allows for interfaces to be used * as the "className". so long as the provided argument * passes the instanceof check it's okay. */ if ($type->getTypeValue() === TypeName::MESSAGE) { if (!class_exists($className) && !interface_exists($className)) { Assertion::true(false, sprintf('Field [%s] className [%s] must be a class or interface.', $name, $className)); } } else { // anyOf is only supported on nested messages Assertion::nullOrClassExists($className); $anyOfClassNames = null; } $this->name = $name; $this->type = $type; $this->required = $required; $this->useTypeDefault = $useTypeDefault; $this->className = $className; $this->anyOfClassNames = $anyOfClassNames; $this->assertion = $assertion; $this->overridable = $overridable; $this->applyFieldRule($rule); $this->applyStringOptions($minLength, $maxLength, $pattern, $format); $this->applyNumericOptions($min, $max, $precision, $scale); $this->applyDefault($default); }
/** * @param string $accountId * @throws \InvalidArgumentException */ protected function __construct($accountId) { Assertion::regex($accountId, '/^[A-Za-z0-9]+$/'); parent::__construct($accountId); }