| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Freemius |
| 4 |
* @copyright Copyright (c) 2015, Freemius, Inc. |
| 5 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 |
| 6 |
* @since 1.0.3 |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Get object's public variables. |
| 15 |
* |
| 16 |
* @author Vova Feldman (@svovaf) |
| 17 |
* @since 1.0.0 |
| 18 |
* |
| 19 |
* @param object $object |
| 20 |
* |
| 21 |
* @return array |
| 22 |
*/ |
| 23 |
function fs_get_object_public_vars( $object ) { |
| 24 |
return get_object_vars( $object ); |
| 25 |
} |
| 26 |
|
| 27 |
class FS_Entity { |
| 28 |
/** |
| 29 |
* @var number |
| 30 |
*/ |
| 31 |
public $id; |
| 32 |
/** |
| 33 |
* @var string Datetime value in 'YYYY-MM-DD HH:MM:SS' format. |
| 34 |
*/ |
| 35 |
public $updated; |
| 36 |
/** |
| 37 |
* @var string Datetime value in 'YYYY-MM-DD HH:MM:SS' format. |
| 38 |
*/ |
| 39 |
public $created; |
| 40 |
|
| 41 |
/** |
| 42 |
* @param bool|object $entity |
| 43 |
*/ |
| 44 |
function __construct( $entity = false ) { |
| 45 |
if ( ! ( $entity instanceof stdClass ) && ! ( $entity instanceof FS_Entity ) ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
$props = fs_get_object_public_vars( $this ); |
| 50 |
|
| 51 |
foreach ( $props as $key => $def_value ) { |
| 52 |
$this->{$key} = isset( $entity->{$key} ) ? |
| 53 |
$entity->{$key} : |
| 54 |
$def_value; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
static function get_type() { |
| 59 |
return 'type'; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @author Vova Feldman (@svovaf) |
| 64 |
* @since 1.0.6 |
| 65 |
* |
| 66 |
* @param FS_Entity $entity1 |
| 67 |
* @param FS_Entity $entity2 |
| 68 |
* |
| 69 |
* @return bool |
| 70 |
*/ |
| 71 |
static function equals( $entity1, $entity2 ) { |
| 72 |
if ( is_null( $entity1 ) && is_null( $entity2 ) ) { |
| 73 |
return true; |
| 74 |
} else if ( is_object( $entity1 ) && is_object( $entity2 ) ) { |
| 75 |
return ( $entity1->id == $entity2->id ); |
| 76 |
} else if ( is_object( $entity1 ) ) { |
| 77 |
return is_null( $entity1->id ); |
| 78 |
} else { |
| 79 |
return is_null( $entity2->id ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
private $_is_updated = false; |
| 84 |
|
| 85 |
/** |
| 86 |
* Update object property. |
| 87 |
* |
| 88 |
* @author Vova Feldman (@svovaf) |
| 89 |
* @since 1.0.9 |
| 90 |
* |
| 91 |
* @param string|array[string]mixed $key |
| 92 |
* @param string|bool $val |
| 93 |
* |
| 94 |
* @return bool |
| 95 |
*/ |
| 96 |
function update( $key, $val = false ) { |
| 97 |
if ( ! is_array( $key ) ) { |
| 98 |
$key = array( $key => $val ); |
| 99 |
} |
| 100 |
|
| 101 |
$is_updated = false; |
| 102 |
|
| 103 |
foreach ( $key as $k => $v ) { |
| 104 |
if ( $this->{$k} === $v ) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
if ( ( is_string( $this->{$k} ) && is_numeric( $v ) || |
| 109 |
( is_numeric( $this->{$k} ) && is_string( $v ) ) ) && |
| 110 |
$this->{$k} == $v |
| 111 |
) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
// Update value. |
| 116 |
$this->{$k} = $v; |
| 117 |
|
| 118 |
$is_updated = true; |
| 119 |
} |
| 120 |
|
| 121 |
$this->_is_updated = $is_updated; |
| 122 |
|
| 123 |
return $is_updated; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Checks if entity was updated. |
| 128 |
* |
| 129 |
* @author Vova Feldman (@svovaf) |
| 130 |
* @since 1.0.9 |
| 131 |
* |
| 132 |
* @return bool |
| 133 |
*/ |
| 134 |
function is_updated() { |
| 135 |
return $this->_is_updated; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @param $id |
| 140 |
* |
| 141 |
* @author Vova Feldman (@svovaf) |
| 142 |
* @since 1.1.2 |
| 143 |
* |
| 144 |
* @return bool |
| 145 |
*/ |
| 146 |
static function is_valid_id($id){ |
| 147 |
return is_numeric($id); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @author Leo Fajardo (@leorw) |
| 152 |
* @since 2.3.1 |
| 153 |
* |
| 154 |
* @return string |
| 155 |
*/ |
| 156 |
public static function get_class_name() { |
| 157 |
return get_called_class(); |
| 158 |
} |
| 159 |
} |