Code Duplication    Length = 22-24 lines in 5 locations

src/Assert.php 5 locations

@@ 1178-1200 (lines=23) @@
1175
     * @return Assert
1176
     * @throws AssertionFailedException
1177
     */
1178
    public function length(int $length, string $message='', string $fieldName='', string $encoding='utf8') : Assert
1179
    {
1180
        if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) )
1181
        {
1182
            return $this;
1183
        }
1184
        $this->string($message, $fieldName);
1185
        if ( mb_strlen($this->value, $encoding) !== $length )
1186
        {
1187
            $message    = $message ?: $this->overrideError;
1188
            $message    = sprintf(
1189
                $message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
1190
                $this->stringify($this->value),
1191
                $length,
1192
                mb_strlen($this->value, $encoding)
1193
            );
1194
            $constraints = ['length' => $length, 'encoding' => $encoding];
1195
1196
            throw $this->createException($message, $this->overrideCode ?: self::INVALID_LENGTH, $fieldName, $constraints);
1197
        }
1198
1199
        return $this;
1200
    }
1201
1202
    /**
1203
     * Assert that value is a string and has a character count which is
@@ 1213-1236 (lines=24) @@
1210
     * @return Assert
1211
     * @throws AssertionFailedException
1212
     */
1213
    public function minLength(int $minLength, string $message='', string $fieldName='', string $encoding='utf8') : Assert
1214
    {
1215
        if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) )
1216
        {
1217
            return $this;
1218
        }
1219
        $this->string($message, $fieldName);
1220
        if ( mb_strlen($this->value, $encoding) < $minLength )
1221
        {
1222
            $message = $message ?: $this->overrideError;
1223
            $message     = sprintf(
1224
                $message
1225
                    ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
1226
                $this->stringify($this->value),
1227
                $minLength,
1228
                mb_strlen($this->value, $encoding)
1229
            );
1230
            $constraints = ['min_length' => $minLength, 'encoding' => $encoding];
1231
1232
            throw $this->createException($message, $this->overrideCode ?: self::INVALID_MIN_LENGTH, $fieldName, $constraints);
1233
        }
1234
1235
        return $this;
1236
    }
1237
1238
    /**
1239
     * Assert that value is a string and has a character count which is
@@ 1249-1271 (lines=23) @@
1246
     * @return Assert
1247
     * @throws AssertionFailedException
1248
     */
1249
    public function maxLength(int $maxLength, string $message='', string $fieldName='', string $encoding='utf8') : Assert
1250
    {
1251
        if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) )
1252
        {
1253
            return $this;
1254
        }
1255
        $this->string($message, $fieldName);
1256
        if ( mb_strlen($this->value, $encoding) > $maxLength )
1257
        {
1258
            $message = $message ?: $this->overrideError;
1259
            $message     = sprintf(
1260
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
1261
                $this->stringify($this->value),
1262
                $maxLength,
1263
                mb_strlen($this->value, $encoding)
1264
            );
1265
            $constraints = ['max_length' => $maxLength, 'encoding' => $encoding];
1266
1267
            throw $this->createException($message, $this->overrideCode ?: self::INVALID_MAX_LENGTH, $fieldName, $constraints);
1268
        }
1269
1270
        return $this;
1271
    }
1272
1273
    /**
1274
     * Assert that value has a length between min,max lengths (inclusive).
@@ 1332-1353 (lines=22) @@
1329
     * @return Assert
1330
     * @throws AssertionFailedException
1331
     */
1332
    public function startsWith(string $needle, string $message='', string $fieldName='', string $encoding='utf8') : Assert
1333
    {
1334
        if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) )
1335
        {
1336
            return $this;
1337
        }
1338
        $this->string($message, $fieldName);
1339
        if ( mb_strpos($this->value, $needle, 0, $encoding) !== 0 )
1340
        {
1341
            $message = $message ?: $this->overrideError;
1342
            $message     = sprintf(
1343
                $message ?: 'Value "%s" does not start with "%s".',
1344
                $this->stringify($this->value),
1345
                $this->stringify($needle)
1346
            );
1347
            $constraints = ['needle' => $needle, 'encoding' => $encoding];
1348
1349
            throw $this->createException($message, $this->overrideCode ?: self::INVALID_STRING_START, $fieldName, $constraints);
1350
        }
1351
1352
        return $this;
1353
    }
1354
1355
    /**
1356
     * Assert that value ends with a sequence of chars.
@@ 1399-1420 (lines=22) @@
1396
     * @return Assert
1397
     * @throws AssertionFailedException
1398
     */
1399
    public function contains(string $needle, string $message='', string $fieldName='', string $encoding='utf8') : Assert
1400
    {
1401
        if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) )
1402
        {
1403
            return $this;
1404
        }
1405
        $this->string($message, $fieldName);
1406
        if ( mb_strpos($this->value, $needle, 0, $encoding) === false )
1407
        {
1408
            $message = $message ?: $this->overrideError;
1409
            $message     = sprintf(
1410
                $message ?: 'Value "%s" does not contain "%s".',
1411
                $this->stringify($this->value),
1412
                $this->stringify($needle)
1413
            );
1414
            $constraints = ['needle' => $needle, 'encoding' => $encoding];
1415
1416
            throw $this->createException($message, $this->overrideCode ?: self::INVALID_STRING_CONTAINS, $fieldName, $constraints);
1417
        }
1418
1419
        return $this;
1420
    }
1421
1422
    /**
1423
     * Assert that value is in an array of choices.