Skip to content
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

Closed

Conversation

SailorMax
Copy link
Contributor

@SailorMax SailorMax commented Oct 20, 2017

This is:

  • a bugfix
  • a new feature

Checklist:

What does it change?
Add possibility to check value of cell on validity based on list of possible values.

Copy link
Member

@PowerKiKi PowerKiKi left a 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 ?

// TODO: write check on all cases
switch ($cellValidation->getType()) {
case Cell\DataValidation::TYPE_LIST:
$formula1 = $cellValidation->getFormula1();
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

$this->getWorksheet()->getParent()
)->calculateFormula($match_formula, $this->getCoordinate(), $this);

return $result != '#N/A';
Copy link
Member

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 !==.

@PowerKiKi
Copy link
Member

Oh btw, next time use composer fix to fix code style.

@SailorMax
Copy link
Contributor Author

SailorMax commented Oct 23, 2017

in PhpOffice\PhpSpreadsheet\Cell\Validator
or in PhpOffice\PhpSpreadsheet\Cell\DataValidation?

as regular method or as static method?

what name for method?

@PowerKiKi
Copy link
Member

PowerKiKi commented Oct 23, 2017 via email

Copy link
Member

@PowerKiKi PowerKiKi left a 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...

*
* @return bool
*/
public function hasValidValue()
Copy link
Member

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.

Copy link
Contributor Author

@SailorMax SailorMax Oct 23, 2017

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?

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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)
Copy link
Member

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:
Copy link
Member

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
Copy link
Member

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 ===

@PowerKiKi
Copy link
Member

src/PhpSpreadsheet/Cell.php was moved to src/PhpSpreadsheet/Cell/Cell.php yesterday. So the file you are trying to modify no longer exists. You should rebase your branch onto latest develop and then force push. You can try that by yourself, or else I can give it a try, but not today...

@SailorMax
Copy link
Contributor Author

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?

@SailorMax
Copy link
Contributor Author

Looks like simple merge is better :)

@PowerKiKi PowerKiKi closed this in 6561494 Oct 31, 2017
@PowerKiKi
Copy link
Member

Thanks !

Dfred pushed a commit to Dfred/PhpSpreadsheet that referenced this pull request Nov 20, 2018
…dation rules

`$cell->hasValidValue()` returns true if the cell has a value which conform to the
rules defined in `$cell->getDataValidation()`.

Closes PHPOffice#257
@SailorMax SailorMax deleted the cell_has_valid_value_part1 branch March 1, 2019 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants