| 1 |
<?php |
| 2 |
/** |
| 3 |
* Generic object that is like stdClass but only allows get/set of defiend properties |
| 4 |
* |
| 5 |
* @package Caldera_Forms Modified by QuantumCloud |
| 6 |
* @author Josh Pollock <Josh@CalderaWP.com> |
| 7 |
* @license GPL-2.0+ |
| 8 |
* @link |
| 9 |
* @copyright 2016 CalderaWP LLC |
| 10 |
*/ |
| 11 |
abstract class Qcformbuilder_Forms_Object { |
| 12 |
|
| 13 |
public function __construct( stdClass $obj = null ) { |
| 14 |
if( null !== $obj ){ |
| 15 |
$this->set_form_object( $obj ); |
| 16 |
} |
| 17 |
|
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Translate from a stdClass object to this object type |
| 22 |
* |
| 23 |
* @since 1.4.0 |
| 24 |
* |
| 25 |
* @param \stdClass $obj |
| 26 |
*/ |
| 27 |
public function set_form_object( stdClass $obj ){ |
| 28 |
foreach( $obj as $property => $value ){ |
| 29 |
$this->$property = $value; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Set allowed properties |
| 35 |
* |
| 36 |
* @since 1.4.0 |
| 37 |
* |
| 38 |
* @param string $property Name of property |
| 39 |
* @param mixed $value Property value |
| 40 |
* |
| 41 |
* @return bool|mixed |
| 42 |
*/ |
| 43 |
public function __set( $property, $value ){ |
| 44 |
$setter = $property . '_set'; |
| 45 |
if( method_exists( $this, $setter ) ){ |
| 46 |
return call_user_func( array( $this, $setter ), $value ); |
| 47 |
}elseif ( ( is_string( $value ) || is_numeric( $value ) ) && property_exists( $this, $property ) ){ |
| 48 |
$this->$property = $value; |
| 49 |
return true; |
| 50 |
}else{ |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get allowed property value |
| 58 |
* |
| 59 |
* @since 1.4.0 |
| 60 |
* |
| 61 |
* @param string $property Name of property |
| 62 |
* |
| 63 |
* @return mixed|null|void |
| 64 |
*/ |
| 65 |
public function __get( $property ) { |
| 66 |
$getter = $property . '_get'; |
| 67 |
if( method_exists( $this, $getter ) ){ |
| 68 |
return call_user_func( array( $this, $getter ) ); |
| 69 |
} |
| 70 |
if ( property_exists( $this, $property ) ) { |
| 71 |
return $this->apply_filter( $property, $this->$property ); |
| 72 |
} else { |
| 73 |
return null; |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Convert object to array |
| 80 |
* |
| 81 |
* @since 1.4.0 |
| 82 |
* |
| 83 |
* @param bool $serialize_arrays Optional. To return arrays serialized. If true, arrays are serialized, if false, serialized data is unserialized. Default is true. |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function to_array( $serialize_arrays = true ){ |
| 88 |
$vars = get_object_vars( $this ); |
| 89 |
|
| 90 |
foreach( $vars as $property => $value ){ |
| 91 |
if( is_array( $value ) ){ |
| 92 |
if ( $serialize_arrays ) { |
| 93 |
$value = serialize( $value ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if( ! $serialize_arrays && is_serialized( $value ) ){ |
| 98 |
$value = unserialize( $value ); |
| 99 |
} |
| 100 |
|
| 101 |
$vars[ $property ] = $value; |
| 102 |
} |
| 103 |
|
| 104 |
return $vars; |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Filter value |
| 110 |
* |
| 111 |
* Called whenever property is accessed via __get() |
| 112 |
* |
| 113 |
* @since 1.4.0 |
| 114 |
* |
| 115 |
* @param string $property Property name |
| 116 |
* @param mixed $value Property value |
| 117 |
* |
| 118 |
* @return mixed|void |
| 119 |
*/ |
| 120 |
public function apply_filter( $property, $value ){ |
| 121 |
$prefix = $this->get_prefix(); |
| 122 |
$filter_name = 'qcformbuilder_forms_' . $prefix . '_' . $property; |
| 123 |
|
| 124 |
/** |
| 125 |
* Filter value before returning |
| 126 |
* |
| 127 |
* @since 1.4.0 |
| 128 |
* |
| 129 |
* @param mixed $value Property value |
| 130 |
* @param Qcformbuilder_Forms_Object $obj Current class object |
| 131 |
* @param string $class Name of class |
| 132 |
*/ |
| 133 |
return apply_filters( $filter_name, $value, $this, get_class( $this ) ); |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get prefix to use in parents. |
| 139 |
* |
| 140 |
* Used to form filter for getters. Better to ovveride and hardcode. |
| 141 |
* |
| 142 |
* @since 1.4.0 |
| 143 |
* |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
protected function get_prefix(){ |
| 147 |
$class = explode( '_', get_class( $this ) ); |
| 148 |
end( $class ); |
| 149 |
return key( $class ); |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|