Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 150 | Zur aktuellen Revision | Ganze Datei anzeigen | Leerzeichen ignorieren | Details | Blame | Letzte Änderung | Log anzeigen | RSS feed

Revision 150 Revision 399
Zeile 1... Zeile 1...
1
<?php
1
<?php
Zeile 2... Zeile 2...
2
 
2
 
Zeile -... Zeile 3...
-
 
3
namespace Illuminate\Validation\Concerns;
-
 
4
 
-
 
5
use Brick\Math\BigDecimal;
3
namespace Illuminate\Validation\Concerns;
6
use Brick\Math\BigNumber;
4
 
7
use Brick\Math\Exception\MathException as BrickMathException;
5
use DateTime;
8
use DateTime;
6
use DateTimeInterface;
9
use DateTimeInterface;
7
use Egulias\EmailValidator\EmailValidator;
10
use Egulias\EmailValidator\EmailValidator;
Zeile 11... Zeile 14...
11
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
14
use Egulias\EmailValidator\Validation\NoRFCWarningsValidation;
12
use Egulias\EmailValidator\Validation\RFCValidation;
15
use Egulias\EmailValidator\Validation\RFCValidation;
13
use Exception;
16
use Exception;
14
use Illuminate\Database\Eloquent\Model;
17
use Illuminate\Database\Eloquent\Model;
15
use Illuminate\Support\Arr;
18
use Illuminate\Support\Arr;
-
 
19
use Illuminate\Support\Exceptions\MathException;
16
use Illuminate\Support\Facades\Date;
20
use Illuminate\Support\Facades\Date;
17
use Illuminate\Support\Str;
21
use Illuminate\Support\Str;
18
use Illuminate\Validation\Rules\Exists;
22
use Illuminate\Validation\Rules\Exists;
19
use Illuminate\Validation\Rules\Unique;
23
use Illuminate\Validation\Rules\Unique;
20
use Illuminate\Validation\ValidationData;
24
use Illuminate\Validation\ValidationData;
Zeile 333... Zeile 337...
333
        }
337
        }
334
    }
338
    }
Zeile 335... Zeile 339...
335
 
339
 
336
    /**
340
    /**
-
 
341
     * Validate that an attribute contains only alphabetic characters.
337
     * Validate that an attribute contains only alphabetic characters.
342
     * If the 'ascii' option is passed, validate that an attribute contains only ascii alphabetic characters.
338
     *
343
     *
339
     * @param  string  $attribute
344
     * @param  string  $attribute
340
     * @param  mixed  $value
345
     * @param  mixed  $value
341
     * @return bool
346
     * @return bool
342
     */
347
     */
343
    public function validateAlpha($attribute, $value)
348
    public function validateAlpha($attribute, $value, $parameters)
-
 
349
    {
-
 
350
        if (isset($parameters[0]) && $parameters[0] === 'ascii') {
-
 
351
            return is_string($value) && preg_match('/\A[a-zA-Z]+\z/u', $value);
-
 
352
        }
344
    {
353
 
345
        return is_string($value) && preg_match('/^[\pL\pM]+$/u', $value);
354
        return is_string($value) && preg_match('/\A[\pL\pM]+\z/u', $value);
Zeile 346... Zeile 355...
346
    }
355
    }
347
 
356
 
-
 
357
    /**
-
 
358
     * Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
348
    /**
359
     * If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters,
349
     * Validate that an attribute contains only alpha-numeric characters, dashes, and underscores.
360
     * dashes, and underscores.
350
     *
361
     *
351
     * @param  string  $attribute
362
     * @param  string  $attribute
352
     * @param  mixed  $value
363
     * @param  mixed  $value
353
     * @return bool
364
     * @return bool
354
     */
365
     */
355
    public function validateAlphaDash($attribute, $value)
366
    public function validateAlphaDash($attribute, $value, $parameters)
356
    {
367
    {
357
        if (! is_string($value) && ! is_numeric($value)) {
368
        if (! is_string($value) && ! is_numeric($value)) {
Zeile -... Zeile 369...
-
 
369
            return false;
-
 
370
        }
-
 
371
 
-
 
372
        if (isset($parameters[0]) && $parameters[0] === 'ascii') {
358
            return false;
373
            return preg_match('/\A[a-zA-Z0-9_-]+\z/u', $value) > 0;
359
        }
374
        }
Zeile 360... Zeile 375...
360
 
375
 
361
        return preg_match('/^[\pL\pM\pN_-]+$/u', $value) > 0;
376
        return preg_match('/\A[\pL\pM\pN_-]+\z/u', $value) > 0;
-
 
377
    }
362
    }
378
 
363
 
379
    /**
364
    /**
380
     * Validate that an attribute contains only alpha-numeric characters.
365
     * Validate that an attribute contains only alpha-numeric characters.
381
     * If the 'ascii' option is passed, validate that an attribute contains only ascii alpha-numeric characters.
366
     *
382
     *
367
     * @param  string  $attribute
383
     * @param  string  $attribute
368
     * @param  mixed  $value
384
     * @param  mixed  $value
369
     * @return bool
385
     * @return bool
370
     */
386
     */
371
    public function validateAlphaNum($attribute, $value)
387
    public function validateAlphaNum($attribute, $value, $parameters)
Zeile -... Zeile 388...
-
 
388
    {
-
 
389
        if (! is_string($value) && ! is_numeric($value)) {
-
 
390
            return false;
-
 
391
        }
372
    {
392
 
373
        if (! is_string($value) && ! is_numeric($value)) {
393
        if (isset($parameters[0]) && $parameters[0] === 'ascii') {
Zeile 374... Zeile 394...
374
            return false;
394
            return preg_match('/\A[a-zA-Z0-9]+\z/u', $value) > 0;
375
        }
395
        }
376
 
396
 
Zeile 431... Zeile 451...
431
     */
451
     */
432
    public function validateBetween($attribute, $value, $parameters)
452
    public function validateBetween($attribute, $value, $parameters)
433
    {
453
    {
434
        $this->requireParameterCount(2, $parameters, 'between');
454
        $this->requireParameterCount(2, $parameters, 'between');
Zeile -... Zeile 455...
-
 
455
 
435
 
456
        return with(
436
        $size = $this->getSize($attribute, $value);
-
 
437
 
457
            BigNumber::of($this->getSize($attribute, $value)),
-
 
458
            fn ($size) => $size->isGreaterThanOrEqualTo($parameters[0]) && $size->isLessThanOrEqualTo($parameters[1])
438
        return $size >= $parameters[0] && $size <= $parameters[1];
459
        );
Zeile 439... Zeile 460...
439
    }
460
    }
440
 
461
 
441
    /**
462
    /**
Zeile 1075... Zeile 1096...
1075
        $comparedToValue = $this->getValue($parameters[0]);
1096
        $comparedToValue = $this->getValue($parameters[0]);
Zeile 1076... Zeile 1097...
1076
 
1097
 
Zeile 1077... Zeile 1098...
1077
        $this->shouldBeNumeric($attribute, 'Gt');
1098
        $this->shouldBeNumeric($attribute, 'Gt');
1078
 
1099
 
1079
        if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1100
        if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
Zeile 1080... Zeile 1101...
1080
            return $this->getSize($attribute, $value) > $parameters[0];
1101
            return BigNumber::of($this->getSize($attribute, $value))->isGreaterThan($parameters[0]);
1081
        }
1102
        }
1082
 
1103
 
Zeile 1083... Zeile 1104...
1083
        if (is_numeric($parameters[0])) {
1104
        if (is_numeric($parameters[0])) {
1084
            return false;
1105
            return false;
1085
        }
1106
        }
Zeile 1086... Zeile 1107...
1086
 
1107
 
1087
        if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1108
        if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1088
            return $value > $comparedToValue;
1109
            return BigNumber::of($value)->isGreaterThan($comparedToValue);
Zeile 1110... Zeile 1131...
1110
        $comparedToValue = $this->getValue($parameters[0]);
1131
        $comparedToValue = $this->getValue($parameters[0]);
Zeile 1111... Zeile 1132...
1111
 
1132
 
Zeile 1112... Zeile 1133...
1112
        $this->shouldBeNumeric($attribute, 'Lt');
1133
        $this->shouldBeNumeric($attribute, 'Lt');
1113
 
1134
 
1114
        if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1135
        if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
Zeile 1115... Zeile 1136...
1115
            return $this->getSize($attribute, $value) < $parameters[0];
1136
            return BigNumber::of($this->getSize($attribute, $value))->isLessThan($parameters[0]);
1116
        }
1137
        }
1117
 
1138
 
Zeile 1118... Zeile 1139...
1118
        if (is_numeric($parameters[0])) {
1139
        if (is_numeric($parameters[0])) {
1119
            return false;
1140
            return false;
1120
        }
1141
        }
Zeile 1121... Zeile 1142...
1121
 
1142
 
1122
        if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1143
        if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1123
            return $value < $comparedToValue;
1144
            return BigNumber::of($value)->isLessThan($comparedToValue);
Zeile 1145... Zeile 1166...
1145
        $comparedToValue = $this->getValue($parameters[0]);
1166
        $comparedToValue = $this->getValue($parameters[0]);
Zeile 1146... Zeile 1167...
1146
 
1167
 
Zeile 1147... Zeile 1168...
1147
        $this->shouldBeNumeric($attribute, 'Gte');
1168
        $this->shouldBeNumeric($attribute, 'Gte');
1148
 
1169
 
1149
        if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1170
        if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
Zeile 1150... Zeile 1171...
1150
            return $this->getSize($attribute, $value) >= $parameters[0];
1171
            return BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($parameters[0]);
1151
        }
1172
        }
1152
 
1173
 
Zeile 1153... Zeile 1174...
1153
        if (is_numeric($parameters[0])) {
1174
        if (is_numeric($parameters[0])) {
1154
            return false;
1175
            return false;
1155
        }
1176
        }
Zeile 1156... Zeile 1177...
1156
 
1177
 
1157
        if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1178
        if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1158
            return $value >= $comparedToValue;
1179
            return BigNumber::of($value)->isGreaterThanOrEqualTo($comparedToValue);
Zeile 1180... Zeile 1201...
1180
        $comparedToValue = $this->getValue($parameters[0]);
1201
        $comparedToValue = $this->getValue($parameters[0]);
Zeile 1181... Zeile 1202...
1181
 
1202
 
Zeile 1182... Zeile 1203...
1182
        $this->shouldBeNumeric($attribute, 'Lte');
1203
        $this->shouldBeNumeric($attribute, 'Lte');
1183
 
1204
 
1184
        if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1205
        if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
Zeile 1185... Zeile 1206...
1185
            return $this->getSize($attribute, $value) <= $parameters[0];
1206
            return BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($parameters[0]);
1186
        }
1207
        }
1187
 
1208
 
Zeile 1188... Zeile 1209...
1188
        if (is_numeric($parameters[0])) {
1209
        if (is_numeric($parameters[0])) {
1189
            return false;
1210
            return false;
1190
        }
1211
        }
Zeile 1191... Zeile 1212...
1191
 
1212
 
1192
        if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1213
        if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1193
            return $value <= $comparedToValue;
1214
            return BigNumber::of($value)->isLessThanOrEqualTo($comparedToValue);
Zeile 1380... Zeile 1401...
1380
 
1401
 
1381
        if ($value instanceof UploadedFile && ! $value->isValid()) {
1402
        if ($value instanceof UploadedFile && ! $value->isValid()) {
1382
            return false;
1403
            return false;
Zeile 1383... Zeile 1404...
1383
        }
1404
        }
1384
 
1405
 
Zeile 1385... Zeile 1406...
1385
        return $this->getSize($attribute, $value) <= $parameters[0];
1406
        return BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($parameters[0]);
1386
    }
1407
    }
1387
 
1408
 
Zeile 1482... Zeile 1503...
1482
     */
1503
     */
1483
    public function validateMin($attribute, $value, $parameters)
1504
    public function validateMin($attribute, $value, $parameters)
1484
    {
1505
    {
1485
        $this->requireParameterCount(1, $parameters, 'min');
1506
        $this->requireParameterCount(1, $parameters, 'min');
Zeile 1486... Zeile 1507...
1486
 
1507
 
1487
        return $this->getSize($attribute, $value) >= $parameters[0];
1508
        return BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($parameters[0]);
Zeile 1488... Zeile 1509...
1488
    }
1509
    }
1489
 
1510
 
1490
    /**
1511
    /**
Zeile 1503... Zeile 1524...
1503
 
1524
 
1504
        return ! preg_match('/[^0-9]/', $value) && $length >= $parameters[0];
1525
        return ! preg_match('/[^0-9]/', $value) && $length >= $parameters[0];
Zeile 1505... Zeile 1526...
1505
    }
1526
    }
-
 
1527
 
-
 
1528
    /**
-
 
1529
     * Validate that an attribute is missing.
-
 
1530
     *
-
 
1531
     * @param  string  $attribute
-
 
1532
     * @param  mixed  $value
-
 
1533
     * @param  array<int, int|string>  $parameters
-
 
1534
     * @return bool
-
 
1535
     */
-
 
1536
    public function validateMissing($attribute, $value, $parameters)
-
 
1537
    {
-
 
1538
        return ! Arr::has($this->data, $attribute);
-
 
1539
    }
-
 
1540
 
-
 
1541
    /**
-
 
1542
     * Validate that an attribute is missing when another attribute has a given value.
-
 
1543
     *
-
 
1544
     * @param  string  $attribute
-
 
1545
     * @param  mixed  $value
-
 
1546
     * @param  array<int, int|string>  $parameters
-
 
1547
     * @return bool
-
 
1548
     */
-
 
1549
    public function validateMissingIf($attribute, $value, $parameters)
-
 
1550
    {
-
 
1551
        $this->requireParameterCount(2, $parameters, 'missing_if');
-
 
1552
 
-
 
1553
        [$values, $other] = $this->parseDependentRuleParameters($parameters);
-
 
1554
 
-
 
1555
        if (in_array($other, $values, is_bool($other) || is_null($other))) {
-
 
1556
            return $this->validateMissing($attribute, $value, $parameters);
-
 
1557
        }
-
 
1558
 
-
 
1559
        return true;
-
 
1560
    }
-
 
1561
 
-
 
1562
    /**
-
 
1563
     * Validate that an attribute is missing unless another attribute has a given value.
-
 
1564
     *
-
 
1565
     * @param  string  $attribute
-
 
1566
     * @param  mixed  $value
-
 
1567
     * @param  array<int, int|string>  $parameters
-
 
1568
     * @return bool
-
 
1569
     */
-
 
1570
    public function validateMissingUnless($attribute, $value, $parameters)
-
 
1571
    {
-
 
1572
        $this->requireParameterCount(2, $parameters, 'missing_unless');
-
 
1573
 
-
 
1574
        [$values, $other] = $this->parseDependentRuleParameters($parameters);
-
 
1575
 
-
 
1576
        if (! in_array($other, $values, is_bool($other) || is_null($other))) {
-
 
1577
            return $this->validateMissing($attribute, $value, $parameters);
-
 
1578
        }
-
 
1579
 
-
 
1580
        return true;
-
 
1581
    }
-
 
1582
 
-
 
1583
    /**
-
 
1584
     * Validate that an attribute is missing when any given attribute is present.
-
 
1585
     *
-
 
1586
     * @param  string  $attribute
-
 
1587
     * @param  mixed  $value
-
 
1588
     * @param  array<int, int|string>  $parameters
-
 
1589
     * @return bool
-
 
1590
     */
-
 
1591
    public function validateMissingWith($attribute, $value, $parameters)
-
 
1592
    {
-
 
1593
        $this->requireParameterCount(1, $parameters, 'missing_with');
-
 
1594
 
-
 
1595
        return ! Arr::hasAny($this->data, $parameters);
-
 
1596
    }
-
 
1597
 
-
 
1598
    /**
-
 
1599
     * Validate that an attribute is missing when all given attributes are present.
-
 
1600
     *
-
 
1601
     * @param  string  $attribute
-
 
1602
     * @param  mixed  $value
-
 
1603
     * @param  array<int, int|string>  $parameters
-
 
1604
     * @return bool
-
 
1605
     */
-
 
1606
    public function validateMissingWithAll($attribute, $value, $parameters)
-
 
1607
    {
-
 
1608
        $this->requireParameterCount(1, $parameters, 'missing_with');
-
 
1609
 
-
 
1610
        return ! Arr::has($this->data, $parameters);
-
 
1611
    }
1506
 
1612
 
1507
    /**
1613
    /**
1508
     * Validate the value of an attribute is a multiple of a given value.
1614
     * Validate the value of an attribute is a multiple of a given value.
1509
     *
1615
     *
1510
     * @param  string  $attribute
1616
     * @param  string  $attribute
Zeile 1518... Zeile 1624...
1518
 
1624
 
1519
        if (! $this->validateNumeric($attribute, $value) || ! $this->validateNumeric($attribute, $parameters[0])) {
1625
        if (! $this->validateNumeric($attribute, $value) || ! $this->validateNumeric($attribute, $parameters[0])) {
1520
            return false;
1626
            return false;
Zeile -... Zeile 1627...
-
 
1627
        }
1521
        }
1628
 
-
 
1629
        try {
-
 
1630
            $numerator = BigDecimal::of($value);
-
 
1631
            $denominator = BigDecimal::of($parameters[0]);
1522
 
1632
 
-
 
1633
            if ($numerator->isZero() && $denominator->isZero()) {
-
 
1634
                return false;
-
 
1635
            }
-
 
1636
 
1523
        if ((float) $parameters[0] === 0.0) {
1637
            if ($numerator->isZero()) {
Zeile -... Zeile 1638...
-
 
1638
                return true;
-
 
1639
            }
-
 
1640
 
-
 
1641
            if ($denominator->isZero()) {
1524
            return false;
1642
                return false;
-
 
1643
            }
-
 
1644
 
-
 
1645
            return $numerator->remainder($denominator)->isZero();
1525
        }
1646
        } catch (BrickMathException $e) {
Zeile 1526... Zeile 1647...
1526
 
1647
            throw new MathException('An error occurred while handling the mulitple_of input values.', previous: $e);
1527
        return bcmod($value, $parameters[0], 16) === '0.0000000000000000';
1648
        }
1528
    }
1649
    }
Zeile 1687... Zeile 1808...
1687
     * @param  mixed  $value
1808
     * @param  mixed  $value
1688
     * @return bool
1809
     * @return bool
1689
     */
1810
     */
1690
    public function validateProhibited($attribute, $value)
1811
    public function validateProhibited($attribute, $value)
1691
    {
1812
    {
1692
        return false;
1813
        return ! $this->validateRequired($attribute, $value);
1693
    }
1814
    }
Zeile 1694... Zeile 1815...
1694
 
1815
 
1695
    /**
1816
    /**
1696
     * Validate that an attribute does not exist when another attribute has a given value.
1817
     * Validate that an attribute does not exist when another attribute has a given value.
Zeile 1742... Zeile 1863...
1742
     * @param  mixed  $parameters
1863
     * @param  mixed  $parameters
1743
     * @return bool
1864
     * @return bool
1744
     */
1865
     */
1745
    public function validateProhibits($attribute, $value, $parameters)
1866
    public function validateProhibits($attribute, $value, $parameters)
1746
    {
1867
    {
-
 
1868
        if ($this->validateRequired($attribute, $value)) {
1747
        return ! Arr::hasAny($this->data, $parameters);
1869
            foreach ($parameters as $parameter) {
-
 
1870
                if ($this->validateRequired($parameter, Arr::get($this->data, $parameter))) {
-
 
1871
                    return false;
-
 
1872
                }
-
 
1873
            }
-
 
1874
        }
-
 
1875
 
-
 
1876
        return true;
1748
    }
1877
    }
Zeile 1749... Zeile 1878...
1749
 
1878
 
1750
    /**
1879
    /**
1751
     * Indicate that an attribute is excluded.
1880
     * Indicate that an attribute is excluded.
Zeile 2049... Zeile 2178...
2049
     */
2178
     */
2050
    public function validateSize($attribute, $value, $parameters)
2179
    public function validateSize($attribute, $value, $parameters)
2051
    {
2180
    {
2052
        $this->requireParameterCount(1, $parameters, 'size');
2181
        $this->requireParameterCount(1, $parameters, 'size');
Zeile 2053... Zeile 2182...
2053
 
2182
 
2054
        return $this->getSize($attribute, $value) == $parameters[0];
2183
        return BigNumber::of($this->getSize($attribute, $value))->isEqualTo($parameters[0]);
Zeile 2055... Zeile 2184...
2055
    }
2184
    }
2056
 
2185
 
2057
    /**
2186
    /**
Zeile 2208... Zeile 2337...
2208
    /**
2337
    /**
2209
     * Get the size of an attribute.
2338
     * Get the size of an attribute.
2210
     *
2339
     *
2211
     * @param  string  $attribute
2340
     * @param  string  $attribute
2212
     * @param  mixed  $value
2341
     * @param  mixed  $value
2213
     * @return mixed
2342
     * @return int|float|string
2214
     */
2343
     */
2215
    protected function getSize($attribute, $value)
2344
    protected function getSize($attribute, $value)
2216
    {
2345
    {
2217
        $hasNumeric = $this->hasRule($attribute, $this->numericRules);
2346
        $hasNumeric = $this->hasRule($attribute, $this->numericRules);