woocommerce/classes/gateways/mijireh/includes/Model.php

136 lines
2.8 KiB
PHP
Raw Normal View History

<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2012-06-08 10:46:10 +00:00
class Mijireh_Model {
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
protected $_data = array();
protected $_errors = array();
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
/**
* Set the value of one of the keys in the private $_data array.
2012-11-27 16:22:47 +00:00
*
2012-06-08 10:46:10 +00:00
* @param string $key The key in the $_data array
* @param string $value The value to assign to the key
* @return boolean
*/
public function __set($key, $value) {
$success = false;
if(array_key_exists($key, $this->_data)) {
$this->_data[$key] = $value;
$success = true;
}
return $success;
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
/**
* Get the value for the key from the private $_data array.
2012-11-27 16:22:47 +00:00
*
2012-06-08 10:46:10 +00:00
* Return false if the requested key does not exist
2012-11-27 16:22:47 +00:00
*
2012-06-08 10:46:10 +00:00
* @param string $key The key from the $_data array
* @return mixed
*/
public function __get($key) {
$value = false;
if(array_key_exists($key, $this->_data)) {
$value = $this->_data[$key];
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
/*
elseif(method_exists($this, $key)) {
$value = call_user_func_array(array($this, $key), func_get_args());
}
*/
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
return $value;
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
/**
* Return true if the given $key in the private $_data array is set
2012-11-27 16:22:47 +00:00
*
2012-06-08 10:46:10 +00:00
* @param string $key
2012-11-27 16:22:47 +00:00
* @return boolean
2012-06-08 10:46:10 +00:00
*/
public function __isset($key) {
return isset($this->_data[$key]);
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
/**
2012-11-27 16:22:47 +00:00
* Set the value of the $_data array to null for the given key.
*
2012-06-08 10:46:10 +00:00
* @param string $key
* @return void
*/
public function __unset($key) {
if(array_key_exists($key, $this->_data)) {
$this->_data[$key] = null;
}
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
/**
* Return the private $_data array
2012-11-27 16:22:47 +00:00
*
2012-06-08 10:46:10 +00:00
* @return mixed
*/
public function get_data() {
return $this->_data;
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
/**
* Return true if the given $key exists in the private $_data array
2012-11-27 16:22:47 +00:00
*
2012-06-08 10:46:10 +00:00
* @param string $key
* @return boolean
*/
public function field_exists($key) {
return array_key_exists($key, $this->_data);
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
public function copy_from(array $data) {
foreach($data as $key => $value) {
if(array_key_exists($key, $this->_data)) {
$this->_data[$key] = $value;
}
}
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
public function clear() {
foreach($this->_data as $key => $value) {
if($key == 'id') {
$this->_data[$key] = null;
}
else {
$this->_data[$key] = '';
}
}
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
public function add_error($error_message) {
if(!empty($error_message)) {
$this->_errors[] = $error_message;
}
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
public function clear_errors() {
$this->_errors = array();
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
public function get_errors() {
return $this->_errors;
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
public function get_error_lines($glue="\n") {
$error_lines = '';
if(count($this->_errors)) {
$error_lines = implode($glue, $this->_errors);
}
return $error_lines;
}
2012-11-27 16:22:47 +00:00
2012-06-08 10:46:10 +00:00
public function is_valid() {
return count($this->_errors) == 0;
}
2012-11-27 16:22:47 +00:00
}