class-freemius-entity.php
5 months ago
class-freemius-license.php
5 months ago
class-freemius-plugin.php
5 months ago
class-freemius-scope.php
5 months ago
class-freemius-site.php
5 months ago
class-freemius-user.php
5 months ago
index.php
6 months ago
class-freemius-entity.php
170 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WBCR\Factory_Freemius_Rio_600\Entities; |
| 4 | |
| 5 | use stdClass; |
| 6 | |
| 7 | if ( ! defined( 'ABSPATH' ) ) { |
| 8 | exit; |
| 9 | } |
| 10 | |
| 11 | /** |
| 12 | * @version 1.0 |
| 13 | */ |
| 14 | class Entity { |
| 15 | |
| 16 | /** |
| 17 | * @var number |
| 18 | */ |
| 19 | public $id; |
| 20 | /** |
| 21 | * @var string Datetime value in 'YYYY-MM-DD HH:MM:SS' format. |
| 22 | */ |
| 23 | public $updated; |
| 24 | /** |
| 25 | * @var string Datetime value in 'YYYY-MM-DD HH:MM:SS' format. |
| 26 | */ |
| 27 | public $created; |
| 28 | |
| 29 | /** |
| 30 | * @var bool |
| 31 | */ |
| 32 | private $is_updated = false; |
| 33 | |
| 34 | /** |
| 35 | * @param bool|object|array $entity |
| 36 | */ |
| 37 | public function __construct( $entity = false ) { |
| 38 | if ( ! ( $entity instanceof stdClass ) && ! is_array( $entity ) ) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | $props = get_object_vars( $this ); |
| 43 | |
| 44 | foreach ( $props as $key => $def_value ) { |
| 45 | if ( is_object( $entity ) ) { |
| 46 | $this->{$key} = isset( $entity->{$key} ) ? $entity->{$key} : $def_value; |
| 47 | } else { |
| 48 | $this->{$key} = isset( $entity[ $key ] ) ? $entity[ $key ] : $def_value; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @since 1.1 |
| 55 | * |
| 56 | * @param object|array $data |
| 57 | * |
| 58 | * @return bool |
| 59 | */ |
| 60 | public function populate( $data ) { |
| 61 | if ( ! is_object( $data ) && ! is_array( $data ) ) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | $props = get_object_vars( $this ); |
| 66 | |
| 67 | foreach ( $props as $key => $def_value ) { |
| 68 | if ( is_array( $data ) ) { |
| 69 | $this->{$key} = isset( $data[ $key ] ) ? $data[ $key ] : $def_value; |
| 70 | } else { |
| 71 | $this->{$key} = isset( $data->{$key} ) ? $data->{$key} : $def_value; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @since 1.0.0 |
| 80 | * @return array |
| 81 | */ |
| 82 | public function to_array() { |
| 83 | return get_object_vars( $this ); |
| 84 | } |
| 85 | |
| 86 | static function get_type() { |
| 87 | return 'type'; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @since 1.0.6 |
| 92 | * |
| 93 | * @param Entity $entity1 |
| 94 | * @param Entity $entity2 |
| 95 | * |
| 96 | * @return bool |
| 97 | */ |
| 98 | static function equals( $entity1, $entity2 ) { |
| 99 | if ( is_null( $entity1 ) && is_null( $entity2 ) ) { |
| 100 | return true; |
| 101 | } elseif ( is_object( $entity1 ) && is_object( $entity2 ) ) { |
| 102 | return ( $entity1->id == $entity2->id ); |
| 103 | } elseif ( is_object( $entity1 ) ) { |
| 104 | return is_null( $entity1->id ); |
| 105 | } else { |
| 106 | return is_null( $entity2->id ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /** |
| 112 | * Update object property. |
| 113 | * |
| 114 | * @since 1.0.9 |
| 115 | * |
| 116 | * @param string|array[string]mixed $key |
| 117 | * @param string|bool $val |
| 118 | * |
| 119 | * @return bool |
| 120 | */ |
| 121 | function update( $key, $val = false ) { |
| 122 | if ( ! is_array( $key ) ) { |
| 123 | $key = [ $key => $val ]; |
| 124 | } |
| 125 | |
| 126 | $is_updated = false; |
| 127 | |
| 128 | foreach ( $key as $k => $v ) { |
| 129 | if ( $this->{$k} === $v ) { |
| 130 | continue; |
| 131 | } |
| 132 | |
| 133 | if ( ( is_string( $this->{$k} ) && is_numeric( $v ) || ( is_numeric( $this->{$k} ) && is_string( $v ) ) ) && $this->{$k} == $v ) { |
| 134 | continue; |
| 135 | } |
| 136 | |
| 137 | // Update value. |
| 138 | $this->{$k} = $v; |
| 139 | |
| 140 | $is_updated = true; |
| 141 | } |
| 142 | |
| 143 | $this->is_updated = $is_updated; |
| 144 | |
| 145 | return $is_updated; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Checks if entity was updated. |
| 150 | * |
| 151 | * @since 1.0.9 |
| 152 | * |
| 153 | * @return bool |
| 154 | */ |
| 155 | function is_updated() { |
| 156 | return $this->is_updated; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * @since 1.1.2 |
| 161 | * |
| 162 | * @param $id |
| 163 | * |
| 164 | * @return bool |
| 165 | */ |
| 166 | static function is_valid_id( $id ) { |
| 167 | return is_numeric( $id ); |
| 168 | } |
| 169 | } |
| 170 |