PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/SimplOn/Datas/Count.php

http://github.com/SimplOnPHP/SimplOnPHP
PHP | 71 lines | 24 code | 3 blank | 44 comment | 5 complexity | 5e93546dbdb798950cb4e4357571b6fb MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. Copyright © 2011 Rubén Schaffer Levine and Luca Lauretta <http://simplonphp.org/>
  4. This file is part of “SimplOn PHP”.
  5. “SimplOn PHP” is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation version 3 of the License.
  8. “SimplOn PHP” is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with “SimplOn PHP”. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. namespace SimplOn\Datas;
  16. /**
  17. * Count data type
  18. *
  19. * This data receives a string which is used in COUNT() to be used in database statements.
  20. *
  21. * @author López Santiago Daniel <http://www.behance.net/zesk8>
  22. * @copyright (c) 2013, López Santiago Daniel
  23. * @category Data
  24. */
  25. class Count extends ComplexData {
  26. protected
  27. $list = true,
  28. $fetch = true;
  29. /**
  30. * val
  31. *
  32. * Store the value recived in $this->val.
  33. *
  34. * @param string $val
  35. * @return string
  36. * @throws \SimplOn\DataValidationException
  37. */
  38. function val($val = null){
  39. if (isset($val)) {
  40. if (is_numeric($val) && is_int($val*1)) {
  41. $this->val = intval($val);
  42. } else {
  43. throw new \SimplOn\DataValidationException($this->validationNaN);
  44. }
  45. } else {
  46. return $this->val;
  47. }
  48. }
  49. /**
  50. *
  51. * doRead
  52. *
  53. * Create the syntax of a "count" like this: "COUNT(item) as dataName", which
  54. * will be used in the database statements to count results.
  55. *
  56. * @return array
  57. */
  58. public function doRead(){
  59. $countItem = $this->sources;
  60. return ($this->fetch())
  61. ? array(array('COUNT('.$countItem.') as '.$this->name(), $this->getClass()))
  62. : null;
  63. }
  64. }