Code Duplication    Length = 22-24 lines in 5 locations

src/Assert.php 5 locations

@@ 1146-1168 (lines=23) @@
1143
     * @return Assert
1144
     * @throws AssertionFailedException
1145
     */
1146
    public function length(int $length, string $message = '', string $fieldName = '', $encoding = 'utf8') : Assert
1147
    {
1148
        if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) )
1149
        {
1150
            return $this;
1151
        }
1152
        $this->string($message, $fieldName);
1153
        if ( mb_strlen($this->value, $encoding) !== $length )
1154
        {
1155
            $message    = $message ?: $this->overrideError;
1156
            $message    = sprintf(
1157
                $message ?: 'Value "%s" has to be %d exactly characters long, but length is %d.',
1158
                $this->stringify($this->value),
1159
                $length,
1160
                mb_strlen($this->value, $encoding)
1161
            );
1162
            $constraints = ['length' => $length, 'encoding' => $encoding];
1163
1164
            throw $this->createException($message, $this->overrideCode ?: self::INVALID_LENGTH, $fieldName, $constraints);
1165
        }
1166
1167
        return $this;
1168
    }
1169
1170
    /**
1171
     * Assert that value is a string and has a character count which is
@@ 1181-1204 (lines=24) @@
1178
     * @return Assert
1179
     * @throws AssertionFailedException
1180
     */
1181
    public function minLength(int $minLength, string $message = '', string $fieldName = '', $encoding = 'utf8') : Assert
1182
    {
1183
        if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) )
1184
        {
1185
            return $this;
1186
        }
1187
        $this->string($message, $fieldName);
1188
        if ( mb_strlen($this->value, $encoding) < $minLength )
1189
        {
1190
            $message = $message ?: $this->overrideError;
1191
            $message     = sprintf(
1192
                $message
1193
                    ?: 'Value "%s" is too short, it should have more than %d characters, but only has %d characters.',
1194
                $this->stringify($this->value),
1195
                $minLength,
1196
                mb_strlen($this->value, $encoding)
1197
            );
1198
            $constraints = ['min_length' => $minLength, 'encoding' => $encoding];
1199
1200
            throw $this->createException($message, $this->overrideCode ?: self::INVALID_MIN_LENGTH, $fieldName, $constraints);
1201
        }
1202
1203
        return $this;
1204
    }
1205
1206
    /**
1207
     * Assert that value is a string and has a character count which is
@@ 1217-1239 (lines=23) @@
1214
     * @return Assert
1215
     * @throws AssertionFailedException
1216
     */
1217
    public function maxLength(int $maxLength, string $message = '', string $fieldName = '', $encoding = 'utf8') : Assert
1218
    {
1219
        if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) )
1220
        {
1221
            return $this;
1222
        }
1223
        $this->string($message, $fieldName);
1224
        if ( mb_strlen($this->value, $encoding) > $maxLength )
1225
        {
1226
            $message = $message ?: $this->overrideError;
1227
            $message     = sprintf(
1228
                $message ?: 'Value "%s" is too long, it should have no more than %d characters, but has %d characters.',
1229
                $this->stringify($this->value),
1230
                $maxLength,
1231
                mb_strlen($this->value, $encoding)
1232
            );
1233
            $constraints = ['max_length' => $maxLength, 'encoding' => $encoding];
1234
1235
            throw $this->createException($message, $this->overrideCode ?: self::INVALID_MAX_LENGTH, $fieldName, $constraints);
1236
        }
1237
1238
        return $this;
1239
    }
1240
1241
    /**
1242
     * Assert that value has a length between min,max lengths (inclusive).
@@ 1300-1321 (lines=22) @@
1297
     * @return Assert
1298
     * @throws AssertionFailedException
1299
     */
1300
    public function startsWith(string $needle, string $message = '', string $fieldName = '', string $encoding = 'utf8') : Assert
1301
    {
1302
        if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) )
1303
        {
1304
            return $this;
1305
        }
1306
        $this->string($message, $fieldName);
1307
        if ( mb_strpos($this->value, $needle, 0, $encoding) !== 0 )
1308
        {
1309
            $message = $message ?: $this->overrideError;
1310
            $message     = sprintf(
1311
                $message ?: 'Value "%s" does not start with "%s".',
1312
                $this->stringify($this->value),
1313
                $this->stringify($needle)
1314
            );
1315
            $constraints = ['needle' => $needle, 'encoding' => $encoding];
1316
1317
            throw $this->createException($message, $this->overrideCode ?: self::INVALID_STRING_START, $fieldName, $constraints);
1318
        }
1319
1320
        return $this;
1321
    }
1322
1323
    /**
1324
     * Assert that value ends with a sequence of chars.
@@ 1367-1388 (lines=22) @@
1364
     * @return Assert
1365
     * @throws AssertionFailedException
1366
     */
1367
    public function contains(string $needle, string $message = '', string $fieldName = '', string$encoding = 'utf8') : Assert
1368
    {
1369
        if ( $this->doAllOrNullOr(__FUNCTION__, func_get_args()) )
1370
        {
1371
            return $this;
1372
        }
1373
        $this->string($message, $fieldName);
1374
        if ( mb_strpos($this->value, $needle, 0, $encoding) === false )
1375
        {
1376
            $message = $message ?: $this->overrideError;
1377
            $message     = sprintf(
1378
                $message ?: 'Value "%s" does not contain "%s".',
1379
                $this->stringify($this->value),
1380
                $this->stringify($needle)
1381
            );
1382
            $constraints = ['needle' => $needle, 'encoding' => $encoding];
1383
1384
            throw $this->createException($message, $this->overrideCode ?: self::INVALID_STRING_CONTAINS, $fieldName, $constraints);
1385
        }
1386
1387
        return $this;
1388
    }
1389
1390
    /**
1391
     * Assert that value is in an array of choices.