Skip to content

Commit ecec442

Browse files
authored
Merge pull request #320 from shalvah/fix-before-after-date-validation
Fix before/after date validation
2 parents 29734f5 + cd9e2ab commit ecec442

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
All notable changes to this project will be documented in this file.
33

44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). If you make a pull request to this project, please update this changelog.
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## Unreleased
88
### Added

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ If the project maintainer has any additional requirements, you will find them li
4444

4545
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
4646

47-
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. Add your changes to the `CHANGELOG.md` under the "Unreleased" section.
47+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
4848

4949
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
5050

src/Mpociot/ApiDoc/Generators/AbstractGenerator.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected function getParameters($routeData, $routeAction, $bindings)
152152
'description' => [],
153153
];
154154
foreach ($ruleset as $rule) {
155-
$this->parseRule($rule, $attribute, $attributeData, $routeData['id']);
155+
$this->parseRule($rule, $attribute, $attributeData, $routeData['id'], $routeData);
156156
}
157157
$routeData['parameters'][$attribute] = $attributeData;
158158
}
@@ -356,7 +356,7 @@ protected function splitValuePairs($parameters, $first = 'is ', $last = 'or ')
356356
*
357357
* @return void
358358
*/
359-
protected function parseRule($rule, $ruleName, &$attributeData, $seed)
359+
protected function parseRule($rule, $ruleName, &$attributeData, $seed, $routeData)
360360
{
361361
$faker = Factory::create();
362362
$faker->seed(crc32($seed));
@@ -376,8 +376,17 @@ protected function parseRule($rule, $ruleName, &$attributeData, $seed)
376376
break;
377377
case 'after':
378378
$attributeData['type'] = 'date';
379-
$attributeData['description'][] = Description::parse($rule)->with(date(DATE_RFC850, strtotime($parameters[0])))->getDescription();
380-
$attributeData['value'] = date(DATE_RFC850, strtotime('+1 day', strtotime($parameters[0])));
379+
$format = isset($attributeData['format']) ? $attributeData['format'] : DATE_RFC850;
380+
381+
if (strtotime($parameters[0]) === false) {
382+
// the `after` date refers to another parameter in the request
383+
$paramName = $parameters[0];
384+
$attributeData['description'][] = Description::parse($rule)->with($paramName)->getDescription();
385+
$attributeData['value'] = date($format, strtotime('+1 day', strtotime($routeData['parameters'][$paramName]['value'])));
386+
} else {
387+
$attributeData['description'][] = Description::parse($rule)->with(date($format, strtotime($parameters[0])))->getDescription();
388+
$attributeData['value'] = date($format, strtotime('+1 day', strtotime($parameters[0])));
389+
}
381390
break;
382391
case 'alpha':
383392
$attributeData['description'][] = Description::parse($rule)->getDescription();
@@ -418,13 +427,23 @@ protected function parseRule($rule, $ruleName, &$attributeData, $seed)
418427
break;
419428
case 'before':
420429
$attributeData['type'] = 'date';
421-
$attributeData['description'][] = Description::parse($rule)->with(date(DATE_RFC850, strtotime($parameters[0])))->getDescription();
422-
$attributeData['value'] = date(DATE_RFC850, strtotime('-1 day', strtotime($parameters[0])));
430+
$format = isset($attributeData['format']) ? $attributeData['format'] : DATE_RFC850;
431+
432+
if (strtotime($parameters[0]) === false) {
433+
// the `before` date refers to another parameter in the request
434+
$paramName = $parameters[0];
435+
$attributeData['description'][] = Description::parse($rule)->with($paramName)->getDescription();
436+
$attributeData['value'] = date($format, strtotime('-1 day', strtotime($routeData['parameters'][$paramName]['value'])));
437+
} else {
438+
$attributeData['description'][] = Description::parse($rule)->with(date($format, strtotime($parameters[0])))->getDescription();
439+
$attributeData['value'] = date($format, strtotime('-1 day', strtotime($parameters[0])));
440+
}
423441
break;
424442
case 'date_format':
425443
$attributeData['type'] = 'date';
426444
$attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription();
427-
$attributeData['value'] = date($parameters[0]);
445+
$attributeData['format'] = $parameters[0];
446+
$attributeData['value'] = date($attributeData['format']);
428447
break;
429448
case 'different':
430449
$attributeData['description'][] = Description::parse($rule)->with($parameters)->getDescription();

0 commit comments

Comments
 (0)