-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cell->hasValidValue() - test value on list validation #257
Cell->hasValidValue() - test value on list validation #257
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution. It seems like an interesting feature. However we should probably keep it separated from the Cell
class to avoid making this class even bigger than it already is. I suggest moving your code to a new, independent class. Maybe something like PhpOffice\PhpSpreadsheet\Cell\Validator
?
src/PhpSpreadsheet/Cell.php
Outdated
// TODO: write check on all cases | ||
switch ($cellValidation->getType()) { | ||
case Cell\DataValidation::TYPE_LIST: | ||
$formula1 = $cellValidation->getFormula1(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should extract the validation specific for a type into their own method. That would prevent hasValidValue()
to become too huge in the future when we add validation for other types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
src/PhpSpreadsheet/Cell.php
Outdated
$this->getWorksheet()->getParent() | ||
)->calculateFormula($match_formula, $this->getCoordinate(), $this); | ||
|
||
return $result != '#N/A'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use Functions::NA()
instead of hardcoded string and a strict comparison !==
.
Oh btw, next time use |
in as regular method or as static method? what name for method? |
It should definitely be a different class than DataValidation.
Maybe DataValidator, if you want to make the relation between the two a bit
more obvious. And I'd go for instance methods to prevent pitfall of
singleton pattern in future code evolution.
So usage could be like:
$validator = new DataValidator ();
$isValid = $validator->isValid($cell);
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I think it's better that way. But we can improve it even further. Also will have to reach 100% coverage of the class with phpunit. I'll check that later...
src/PhpSpreadsheet/Cell.php
Outdated
* | ||
* @return bool | ||
*/ | ||
public function hasValidValue() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can drop entirely this method. Keeping the cell class unchanged. This limit the public API to the strict minimum for easier maintenance in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this is part of cell as object. Isn't it?
Where is border? Which method has to be in the Cell, and which has to work as helper?
Cell has setDataValidation() and hasDataValidation() methods, but hasn't any methods to validate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PowerKiKi
So? Do you think? Or do you insist? :)
What is in priority? Maintenance or Usability?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had another look and the Cell
class is not as huge as I though it was, so let's keep the method here as you suggest. But the if (!$this->hasDataValidation())
should still be moved in the validator, otherwise the validation logic would be split into two different places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved. But now we have some kind of conflict. GitHub do not allow me even view where is the problem.
Can you fix it?
* | ||
* @return bool | ||
*/ | ||
public function isValidCellValue(&$cell) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because there is only one thing that can be validated, the method can simply be "isValid". If you think it might not be clear enough, then we should find a better name for the class, but not the method.
Also the parameter must be type hinted with Cell, and it should not be passed by reference.
|
||
// TODO: write check on all cases | ||
switch ($dataValidation->getType()) { | ||
case DataValidation::TYPE_LIST: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's refactor the code for list validation in a private method. Something like "isValueInList()" or similar...
case DataValidation::TYPE_LIST: | ||
$formula1 = $dataValidation->getFormula1(); | ||
if (!empty($formula1)) { | ||
if ($formula1[0] == '"') { // inline values list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should use strict equality ===
|
Not beauty rebase, but done. Now I trying to make it better in local branch, but don't really understand how to do it. Can you help me? Do I need shift split point to end of develop-branch? How? |
766e02d
to
451f577
Compare
Looks like simple merge is better :) |
Thanks ! |
…dation rules `$cell->hasValidValue()` returns true if the cell has a value which conform to the rules defined in `$cell->getDataValidation()`. Closes PHPOffice#257
This is:
Checklist:
What does it change?
Add possibility to check value of cell on validity based on list of possible values.