Code Duplication    Length = 22-24 lines in 5 locations

src/Assert.php 5 locations

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