/** * {@inheritdoc} */ public function validate(SchemaDescriptor $a, SchemaDescriptor $b) { $diff = array_diff(array_keys($a->getMixins()), array_keys($b->getMixins())); if (count($diff)) { throw new ValidatorException(sprintf('The schema "%s" must include the following mixin(s): "%s".', $b, implode('", "', $diff))); } }
/** * {@inheritdoc} */ public function validate(SchemaDescriptor $a, SchemaDescriptor $b) { /** @var FieldDescriptor[] $currentFields */ /** @var FieldDescriptor[] $inheritedFields */ $currentFields = $a->getFields(); $inheritedFields = $a->getInheritedFields(); $diff = array_intersect(array_keys($currentFields), array_keys($inheritedFields)); if (count($diff)) { /** @var \ReflectionClass $ref */ $ref = new \ReflectionClass(new FieldDescriptor('reflection', ['type' => 'string'])); foreach ($diff as $name) { foreach ($ref->getProperties() as $property) { // skip if (in_array($property->getName(), ['default', 'overridable', 'description', 'languages', 'deprecated'])) { continue; } $method = 'get' . ucfirst($property->getName()); if (!$ref->hasMethod($method)) { $method = 'is' . ucfirst($property->getName()); if (!$ref->hasMethod($method)) { continue; } } /** @var FieldDescriptor $fa */ /** @var FieldDescriptor $fb */ $fa = $currentFields[$name]; $fb = $inheritedFields[$name]; if ($fa && $fb && $fa->{$method}() != $fb->{$method}()) { throw new \RuntimeException(sprintf('The schema "%s" field "%s" is invalid. See inherited mixin fields.', $a->getId()->toString(), $property->getName())); } } } } }
/** * {@inheritdoc} */ public function validate(SchemaDescriptor $a, SchemaDescriptor $b) { $fa = array_merge($a->getInheritedFields(), $a->getFields()); $fb = array_merge($b->getInheritedFields(), $b->getFields()); /** @var \Gdbots\Pbjc\FieldDescriptor $field */ /** @var \Gdbots\Pbjc\FieldDescriptor[] $fb */ foreach ($fa as $name => $field) { if (!isset($fb[$name])) { continue; } $method = 'get' . StringUtils::toCamelFromSnake($this->attribute); if (!method_exists($field, $method)) { $method = 'is' . StringUtils::toCamelFromSnake($this->attribute); if (!method_exists($field, $method)) { throw new \RuntimeException(sprintf('Invalid FieldDescriptor attribute "%s"', $this->attribute)); } } if ($field->{$method}() != $fb[$name]->{$method}()) { $value = $field->{$method}(); if ($value instanceof Enum) { $value = $value->__toString(); } if ($value instanceof Type) { $value = $value->getTypeName()->__toString(); } if ($value === true) { $value = 'true'; } if ($value === false) { $value = 'false'; } throw new ValidatorException(sprintf('The schema "%s" field "%s" should be of %s "%s".', $b, $name, $this->attribute, $value)); } } }
/** * {@inheritdoc} */ public function validate(SchemaDescriptor $a, SchemaDescriptor $b) { $fa = array_merge($a->getInheritedFields(), $a->getFields()); $fb = array_merge($b->getInheritedFields(), $b->getFields()); /** @var \Gdbots\Pbjc\FieldDescriptor $field */ /** @var \Gdbots\Pbjc\FieldDescriptor[] $fb */ foreach ($fa as $name => $field) { if (!isset($fb[$name]) || count($field->getAnyOf()) === 0) { continue; } $aoa = []; /** @var SchemaDescriptor $schema*/ foreach ($field->getAnyOf() as $schema) { if (!in_array($schema->getId()->getCurie(), $aoa)) { $aoa[] = $schema->getId()->getCurie(); } } $aob = []; /** @var SchemaDescriptor $schema*/ foreach ($fb[$name]->getAnyOf() as $schema) { if (!in_array($schema->getId()->getCurie(), $aob)) { $aob[] = $schema->getId()->getCurie(); } } $diff = array_diff($aoa, $aob); if (count($diff)) { throw new ValidatorException(sprintf('The schema "%s" field "%s" must include the following anyOf class(es): "%s".', $b, $name, implode('", "', $diff))); } } }
/** * @param SchemaDescriptor $schema * * @return array */ private function getMixinSchemas(SchemaDescriptor $schema) { $schemaIds = []; foreach ($schema->getMixins() as $mixin) { $schemaIds[] = $mixin->getId()->getCurieWithMajorRev(); $schemaIds = array_merge($schemaIds, $this->getExtendsSchemas($mixin), $this->getMixinSchemas($mixin)); } return $schemaIds; }
/** * {@inheritdoc} */ public function validate(SchemaDescriptor $a, SchemaDescriptor $b) { $fields = array_merge($a->getInheritedFields(), $a->getFields()); /** @var \Gdbots\Pbjc\FieldDescriptor $field */ foreach ($fields as $name => $field) { if ($field->getEnum() && $field->getDefault() && !$field->getEnum()->hasValue($field->getDefault())) { throw new ValidatorException(sprintf('The schema "%s" field "%s" enum value "%s" doesn\'t exist. Check enum "%s" for all existing values.', $a->toString(), $name, $field->getDefault(), $field->getEnum()->toString())); } } }
/** * {@inheritdoc} */ public function validate(SchemaDescriptor $a, SchemaDescriptor $b) { foreach ($a->getMixins() as $mixin) { if ($m = $b->getMixin($mixin->getId()->getCurieWithMajorRev())) { if (!$m->isMixinSchema()) { throw new ValidatorException(sprintf('The schema "%s" mixins can only include other mixins. The schema "%s" is not a mixin.', $b, $mixin->getId()->toString())); } } } }
/** * @param SchemaDescriptor $schema * @param string $filename * @param string $directory * @param bool $isLatest * * @return string */ protected function getSchemaTarget(SchemaDescriptor $schema, $filename, $directory = null, $isLatest = false) { $filename = str_replace(['{vendor}', '{package}', '{category}', '{version}', '{major}'], [$schema->getId()->getVendor(), $schema->getId()->getPackage(), $schema->getId()->getCategory(), $schema->getId()->getVersion()->toString(), $schema->getId()->getVersion()->getMajor()], $filename); if ($directory === null) { $directory = sprintf('%s/%s/%s', StringUtils::toCamelFromSlug($schema->getId()->getVendor()), StringUtils::toCamelFromSlug($schema->getId()->getPackage()), StringUtils::toCamelFromSlug($schema->getId()->getCategory())); } if ($directory) { $directory .= '/'; } return sprintf('%s/%s%s%s', $this->compileOptions->getOutput(), $directory, $filename, $this->extension); }
/** * {@inheritdoc} */ public function validate(SchemaDescriptor $a, SchemaDescriptor $b) { $fa = array_merge($a->getInheritedFields(), $a->getFields()); $fb = array_merge($b->getInheritedFields(), $b->getFields()); /** @var \Gdbots\Pbjc\FieldDescriptor $field */ /** @var \Gdbots\Pbjc\FieldDescriptor[] $fb */ foreach ($fa as $name => $field) { if (!isset($fb[$name]) || !$fb[$name]->getMinLength()) { continue; } if ($field->getMinLength() && $field->getMinLength() < $fb[$name]->getMinLength() || !$field->getMinLength() && $field->getType()->getMin() < $fb[$name]->getMinLength()) { throw new ValidatorException(sprintf('The schema "%s" field "%s" min length "%d" must be less than or equal to "%d".', $b, $name, $fb[$name]->getMinLength(), $field->getMinLength() ?: $field->getType()->getMin())); } } }
/** * {@inheritdoc} */ public function validate(SchemaDescriptor $a, SchemaDescriptor $b) { $fa = array_merge($a->getInheritedFields(), $a->getFields()); $fb = array_merge($b->getInheritedFields(), $b->getFields()); /** @var \Gdbots\Pbjc\FieldDescriptor $field */ /** @var \Gdbots\Pbjc\FieldDescriptor[] $fb */ foreach ($fa as $name => $field) { if (!isset($fb[$name])) { continue; } if (!$field->isRequired() && $fb[$name]->isRequired()) { throw new ValidatorException(sprintf('The schema "%s" field "%s" must not be required.', $b, $name)); } } }
/** * Validates a single schema against previous version. * * @param SchemaDescriptor $schema * * @throws \RuntimeException */ public function validate(SchemaDescriptor $schema) { if ($prevSchema = SchemaStore::getPreviousSchema($schema->getId())) { if (!$prevSchema instanceof SchemaDescriptor) { throw new \RuntimeException(sprintf('Un-parsed schema "%s".', $prevSchema['id'])); } /** @var \Gdbots\Pbjc\Validator\Constraint $constraint */ foreach ($this->constraints as $constraint) { $constraint->validate($prevSchema, $schema); } } $constraint = new Constraint\SchemaDependencyVersion(); $constraint->validate($schema, $schema); $constraint = new Constraint\SchemaInheritanceFields(); $constraint->validate($schema, $schema); $constraint = new Constraint\FieldValidEnumValue(); $constraint->validate($schema, $schema); }
/** * {@inheritdoc} */ public function validate(SchemaDescriptor $a, SchemaDescriptor $b) { $fa = array_merge($a->getInheritedFields(), $a->getFields()); $fb = array_merge($b->getInheritedFields(), $b->getFields()); /** @var \Gdbots\Pbjc\FieldDescriptor $field */ /** @var \Gdbots\Pbjc\FieldDescriptor[] $fb */ foreach ($fa as $name => $field) { if (!isset($fb[$name])) { continue; } try { if ($field->getPattern() != $fb[$name]->getPattern() && preg_match(sprintf('/%s/', $fb[$name]->getPattern()), null) !== false) { // do nothing } } catch (\Exception $e) { throw new ValidatorException(sprintf('The schema "%s" field "%s" pattern "%s" is invalid.', $b, $name, $fb[$name]->getPattern())); } } }
/** * {@inheritdoc} */ protected function getSchemaTarget(SchemaDescriptor $schema, $filename, $directory = null, $isLatest = false) { if ($isLatest) { $filename = str_replace('{version}', 'latest', $filename); } $directory = sprintf('%s/%s/%s/%s', $schema->getId()->getVendor(), $schema->getId()->getPackage(), $schema->getId()->getCategory(), $schema->getId()->getMessage()); return parent::getSchemaTarget($schema, $filename, $directory, $isLatest); }
/** * {@inheritdoc} */ protected function getSchemaTemplates(SchemaDescriptor $schema) { return $schema->isMixinSchema() ? ['curie-interface.twig' => '{className}', 'curie-major-interface.twig' => '{className}V{major}', 'mixin.twig' => '{className}V{major}Mixin', 'trait.twig' => '{className}V{major}Trait'] : ['curie-interface.twig' => '{className}', 'message.twig' => '{className}V{major}']; }
/** * {@inheritdoc} */ public function validate(SchemaDescriptor $a, SchemaDescriptor $b) { if ($a->getExtends() != $b->getExtends()) { throw new ValidatorException(sprintf('The schema "%s" must extends "%s".', $b, $a->getExtends()->getId()->toString())); } }
/** * {@inheritdoc} */ public function validate(SchemaDescriptor $a, SchemaDescriptor $b) { if ($a->isMixinSchema() && !$b->isMixinSchema()) { throw new ValidatorException(sprintf('The schema "%s" must be a mixin.', $b)); } }
/** * @param SchemaDescriptor $schema * * @return array */ public function getAllVersions(SchemaDescriptor $schema) { return SchemaStore::getAllSchemaVersions($schema->getId()); }