Source for file big_parameter.php

Documentation is available at big_parameter.php

  1. <?php
  2.  
  3. /**
  4. * midcom_helper_big_parameter
  5. *
  6. * Helper class to cope with too large parameters.
  7. * A nice crutch until MgdSchema takes off.
  8. */
  9.  
  10. class midcom_helper_big_parameter {
  11.  
  12. /**
  13. * Midgard object to attach parameters to.
  14. * @access private
  15. * */
  16. var $_obj; // Midgard object to attach parameters to.
  17.  
  18.  
  19. /**
  20. * Constructur function.
  21. * @params MidgardObject obj - the object to attach the param to.
  22. * */
  23.  
  24. function midcom_helper_big_parameter(&$obj) {
  25. $this->_obj = &$obj;
  26. }
  27.  
  28. function parameter ($domain, $name, $value = null) {
  29. if ($value == null ) {
  30. return $this->_readParam($domain,$name);
  31. } else {
  32. return $this->_writeParam($domain,$name,$value);
  33. }
  34. }
  35. function _readParam($domain,$name)
  36. {
  37. $value = "";
  38. $n = $this->_obj->parameter($domain, $name ."_n");
  39. /* parameter returns an empty string if no param found. */
  40. if ($n != '' )
  41. {
  42. for ($i = 0; $i < $n ; $i++)
  43. {
  44. $value .= $this->_obj->parameter($domain,$name . "_$i");
  45. }
  46. } else {
  47. /* note this returns "" if the parameter doesn't exist just like
  48. * the normal call would.*/
  49. return $this->_obj->parameter($domain, $name );
  50. }
  51. return $value;
  52. }
  53. function _writeParam($domain, $name, $value)
  54. {
  55. $n = $this->_obj->parameter($domain,$name . "_n");
  56. // handle small parameters the old way.
  57. if (strlen($value) < 255 ) {
  58. // delete the old values
  59. if ($n > 0 ) {
  60. for ($i = 0; $i < $n ;$i++) {
  61. $this->_obj->parameter($domain,$name . "_$i" , "");
  62. }
  63. $this->_obj->parameter($domain, $name . "_n", '');
  64. }
  65. return $this->_obj->parameter($domain,$name,$value);
  66. }
  67. $matches = array();
  68. preg_match_all('/.{1,' . 255 . '}/s', $value, $matches);
  69. for ($i = 0; $i < count ($matches[0]);$i++) {
  70. // check if we manage to set the param, otherwise return false as the
  71. // normal call would do.
  72. if (! $this->_obj->parameter($domain,$name . "_$i" , $matches[0][$i]) ) return false;
  73. }
  74. /* Delete the old values
  75. * using the old $i here!
  76. * */
  77. if ($n > $i ) for (;$i < $n ; $i++) $this->_obj->parameter($domain,$name . "_$i" ,'');
  78. /* if we can write one parameter we can write them all! */
  79. $this->_obj->parameter($domain , $name . "_n" , count($matches[0]));
  80. /* delete the old parameter */
  81. $this->_obj->parameter($domain,$name, '');
  82. return true;
  83.  
  84. }
  85.  
  86.  
  87.  
  88. }
  89.  
  90.  
  91.  
  92.  
  93. ?>

Documentation generated on Mon, 21 Nov 2005 18:12:27 +0100 by phpDocumentor 1.3.0RC3