/** * @dataProvider provideInvalidValues */ public function testRejectionOfInvalidValues($invalid_value, $assert_message = '') { $rule = new KeyValueListRule('scalar', []); $this->assertFalse($rule->apply($invalid_value), $assert_message . ' should be rejected'); $this->assertNull($rule->getSanitizedValue(), $assert_message . ' should be null for an invalid value'); }
protected function execute($value, EntityInterface $entity = null) { try { if (is_array($value)) { if (!empty($value) && !$this->isAssoc($value)) { $this->throwError('non_assoc_array', ['value' => $value], IncidentInterface::CRITICAL); return false; } $image = Image::createFromArray($value); } elseif ($value instanceof Image) { $image = Image::createFromArray($value->toNative()); } else { $this->throwError('invalid_type', ['value' => $value], IncidentInterface::CRITICAL); return false; } $incoming_data = $image->toNative(); $data = []; foreach ($this->validations as $property_name => $implementor) { $rule = new $implementor('valid-' . $property_name, $this->getSupportedOptionsFor($implementor, $property_name)); if (!$rule->apply($incoming_data[$property_name])) { $this->throwIncidentsAsErrors($rule, $property_name); return false; } $data[$property_name] = $rule->getSanitizedValue(); } // meta data accepts scalar values $rule = new KeyValueListRule('valid-metadata', $this->getMetadataOptions()); if (!$rule->apply($incoming_data[Image::PROPERTY_METADATA])) { $this->throwIncidentsAsErrors($rule, Image::PROPERTY_METADATA); return false; } $data[Image::PROPERTY_METADATA] = $rule->getSanitizedValue(); // set the sanitized new image data $this->setSanitizedValue(Image::createFromArray($data)); } catch (Exception $e) { // pretty catch all, but there may be Assert and BadValueExceptions depending on usage / later changes $this->throwError('invalid_data', ['error' => $e->getMessage()], IncidentInterface::CRITICAL); return false; } return true; }