StackFormation\Helper\Validator::validateTags PHP Method

validateTags() public static method

public static validateTags ( array $tags )
$tags array
    public static function validateTags(array $tags)
    {
        // @see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#tag-restrictions
        if (count($tags) > 10) {
            throw new \Exception('No more than 10 tags are allowed');
        }
        foreach ($tags as $tag) {
            // key
            if (!isset($tag['Key'])) {
                throw new \Exception('Tag key is missing');
            }
            $key = $tag['Key'];
            if (strlen($key) > 127) {
                throw new \Exception('Keys cannot be longer than 127 characters');
            }
            if (!preg_match('/^[a-zA-Z0-9_\\-+=:\\/@\\.]{1,127}$/', $key)) {
                throw new \Exception("Invalid characters in key '{$key}'");
            }
            if (strpos($key, 'aws:') === 0) {
                throw new \Exception('The aws: prefix cannot be used for keys');
            }
            // value
            if (!isset($tag['Value'])) {
                throw new \Exception('Tag value is missing');
            }
            $value = $tag['Value'];
            if (strlen($value) > 255) {
                throw new \Exception('Values cannot be longer than 255 characters');
            }
            if (!preg_match('/^[a-zA-Z0-9_\\-+=:\\/@\\.]{1,255}$/', $value)) {
                throw new \Exception("Invalid characters in value '{$value}' (key: {$key})");
            }
        }
    }

Usage Example

コード例 #1
0
 /**
  * @test
  */
 public function moreThanTenTags()
 {
     $this->setExpectedException('Exception', 'No more than 10 tags are allowed');
     $tags = [];
     for ($i = 0; $i < 11; $i++) {
         $tags[] = ['Key' => "Key{$i}", 'Value' => "Value{$i}"];
     }
     Validator::validateTags($tags);
 }
All Usage Examples Of StackFormation\Helper\Validator::validateTags