8月
3
2006
分類:
最近更新:
2006-08-03
PHP~~Form Serializer Class
依據 PHP::serialize and unserialize with Form 實作的 serialization class 。
Class: FormSerializer, Licensed by GNU LGPL, Copyright (C) 2006 shirock@educities.edu.tw.
<?php
// Class: FormSerializer
// Copyright (C) 2006 shirock@educities.edu.tw
// ------------------------------------------------------------------------
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
// -------------------------------------------------------------------------
//
// If you want store serialized objects, do that in view, not in control.
// If you want store objects with EnableViewState, enable in view, not in control.
// The control will detect if there be a serialized one by hiself.
// by shirock
//
/**
* Class: FormSerializer
*
* PHP 5.1 above needed!
*
* @author shirock@educitiese.du.tw
*/
class FormSerializer {
/**
* $status is an associative array to state serialized objects.
*
* @access protcted
* @var array
*/
protected $status;
/**
* $id is an identity for this FormSerializer class.
*
* @access protected
* @var string
*/
protected $id;
/**
* Constructor
*/
public function __construct($id = 'my_serialization', $serialized = NULL) {
//if $_POST[$this->id] cound not unserialize (may be invalid data)
//it will set status with false. You would need to check below.
$this->id = $id;
if( !empty($serialized) ) {
$this->status = unserialize( gzinflate( base64_decode($serialized) ) );
}
else if( isset($_POST[$this->id]) ) {
$this->status = unserialize( gzinflate( base64_decode($_POST[$this->id]) ) );
} else { $this->status = false; }
}
/**
* Compress and encode all serialized objects stated in this FormSerializer.
* If PHP does not run as CLI and set $output_tag as true, output a hidden
* filed of HTML form which stored this.
*
* @param boolean $output_tag
* @return all serialized objects stated in this as an encoded string.
*/
public function &store($output_tag = true) {
$serialized_status = base64_encode( gzdeflate( serialize($this->status) ) );
if( ($output_tag == true) and (php_sapi_name() != 'cli') ) {
// not run in Command Line, output.
echo "<input id='{$this->id}' name='{$this->id}' type='hidden' value='{$serialized_status}'/>";
}
return $serialized_status;
}
/**
* Magic Method. State $obj in this and access it as a FormSerializer's
* member named $source_name.
*
* @param string $source_name
* @param object $obj
*/
public function __set($source_name, $obj) {
$this->status[$source_name] = serialize($obj);
}
/**
* Magic Method. Get an object stated in this.
*
* Who allocate object, who serialize/unserialize that.
* Serializiation will be done by view.
* If view does not store the serialized objects, here will return false.
* Control should check return result.
*
* @param string $source_name
*/
public function __get($source_name) {
if( isset($this->status[$source_name]) ) {
return unserialize( $this->status[$source_name] );
//return unserialize(stripslashes($_POST[$serialized_name]));
}
else {
return false;
}
}
public function __isset($source_name) {
return isset($this->status[$source_name]);
}
public function __unset($source_name) {
unset($this->status[$source_name]);
}
}
?>
相關文章
樂多舊網址: http://blog.roodo.com/rocksaying/archives/1967501.html