public static function check(Resource $resource) { foreach ($resource->mandatory() as $key => $value) { if (is_numeric($key) && $resource->hasNotProperty($value) && !isset($resource->defaults()[$value])) { throw new RuntimeException("Property `" . get_class($resource) . "::\${$value}` is mandatory but not set. " . "Mandatory fields are: " . join(',', $resource->mandatory()) . "."); } } }
public static function check(Resource $resource) { foreach ($resource->mandatory() as $key => $value) { if (isset($value['when']['condition']) && $value['when']['condition'] === 'is_present' && $resource->hasProperty($value['when']['property']) && $resource->hasNotProperty($key)) { throw new RuntimeException("Property `" . get_class($resource) . "::\${$key}` is mandatory but not set"); } } }
public static function check(Resource $resource) { foreach ($resource->properties() as $key => $value) { if (isset($resource->allowedValues()[$key])) { $allowedValues = $resource->allowedValues()[$key]; if (!in_array($value, $allowedValues)) { throw new RuntimeException('Value `' . $value . '` is not allowed ' . 'for key `' . $key . '`. ' . 'Allowed values are: ' . var_export($allowedValues, true)); } } } }
public static function check(Resource $resource) { foreach ($resource->properties() as $key => $value) { if (isset($resource->rewrites()[$key])) { $when = $resource->rewrites()[$key]['when']; $set = $resource->rewrites()[$key]['set']; if (isset($when['greater_than'])) { if ($resource->get($key) > $resource->get($when['greater_than'])) { $resource->set($key, $resource->get($set['equals_to'])); } } } } }
public static function check(Resource $resource) { foreach ($resource->properties() as $key => $value) { if (isset($resource->ranges()[$key])) { if ($value < $resource->ranges()[$key]['more_than']) { throw new RuntimeException('Value `' . $value . '` is out of range: ' . '`' . $resource->ranges()[$key]['more_than'] . ' to ' . $resource->ranges()[$key]['more_than'] . '`.'); } } } }
public static function check(Resource $resource) { foreach ($resource->properties() as $key => $value) { if (isset($resource->rules()[$key])) { $rule = $resource->rules()[$key]; if (gettype($resource->get($key)) !== key($rule)) { $expectedType = isset($rule['object']) ? $rule['object'] : 'undefined'; throw new RuntimeException('Attribute `' . $key . '` must be of type `' . (key($rule) == 'scalar' ? current($rule) : $expectedType) . '` with value `' . $value . '`'); } if (!$resource->get($key) instanceof \Sensorario\Resources\Resource && get_class($resource->get($key)) != current($rule)) { if ('array' !== current($rule)) { throw new RuntimeException('Attribute `' . $key . '` must be an object of type ' . current($rule)); } } } } }
public static function check(Resource $resource) { $allowed = array_merge($resource->allowed(), $resource->mandatory()); foreach ($resource->properties() as $key => $value) { if (!in_array($key, $allowed)) { $isAllowed = false; foreach ($allowed as $kk => $vv) { if (!is_numeric($kk) && $resource->hasProperty($vv['when']['property'])) { $isAllowed = true; } } if (!$isAllowed) { throw new RuntimeException("Key `" . get_class($resource) . "::\${$key}` with value `" . $value . "` is not allowed"); } } } }
public static function check(Resource $resource) { foreach ($resource->mandatory() as $key => $value) { if (isset($value['when']['has_value'])) { $propertyName = $value['when']['property']; $propertyValue = $value['when']['has_value']; if (is_array($propertyValue)) { foreach ($propertyValue as $value) { if ($resource->get($propertyName) === $value && $resource->hasNotProperty($key)) { throw new RuntimeException('When property `' . $key . '` has value ' . '`' . $value . '` also `' . $key . '` is mandatory'); } } } else { if ($resource->get($propertyName) === $propertyValue && $resource->hasNotProperty($key)) { throw new RuntimeException('When property `' . $propertyName . '` has value ' . '`' . $propertyValue . '` also `' . $key . '` is mandatory'); } } } } }
/** * @expectedException RuntimeException */ public function testHasMandatoryPropertiesWhenAnotherOneHasAParticularValue() { $configurator = new Configurator('foo', new Container(['resources' => ['foo' => ['constraints' => ['allowed' => ['bar'], 'mandatory' => ['mandatory_property_name', 'foo' => ['when' => ['property' => 'bar', 'has_value' => '42']]]]]]])); $properties = ['bar' => '42']; Resource::box($properties, $configurator); }