'int', 'remote_id' => 'int', 'buttons' => 'array', 'conditions' => 'array', 'dismissed' => 'bool', ); /** * Constructor * * @param array $data Row from the database. */ public function __construct( $data = array() ) { foreach ( $data as $property => $value ) { if ( property_exists( $this, $property ) ) { $this->{$property} = $this->castAttribute( $property, $value ); } } } /** * Casts a property to its designated type. * * @todo Move to trait or base class. * * @since 2.11.4 * * @param string $propertyName * @param mixed $value * * @return bool|float|int|mixed|string|null */ private function castAttribute( $propertyName, $value ) { if ( ! array_key_exists( $propertyName, $this->casts ) ) { return $value; } // Let null be null. if ( is_null( $value ) ) { return null; } switch ( $this->casts[ $propertyName ] ) { case 'array' : return json_decode( $value, true ); case 'bool' : return (bool) $value; case 'float' : return (float) $value; case 'int' : return (int) $value; case 'string' : return (string) $value; default : return $value; } } /** * Returns the icon name to use for this notification type. * * @since 2.11.4 * * @return string */ public function getIcon() { switch ( $this->type ) { case 'warning' : return 'warning'; case 'error' : return 'dismiss'; case 'info' : return 'admin-generic'; case 'success' : default : return 'yes-alt'; } } /** * Converts this model to an array. * * @todo Move to trait. * * @since 2.11.4 * * @return array */ public function toArray() { $data = array(); /* * get_object_vars() returns non-public properties when used within the class * so we're using a ReflectionClass to get the public properties only. */ $object = new \ReflectionClass( $this ); foreach ( $object->getProperties( \ReflectionProperty::IS_PUBLIC ) as $property ) { if ( $property instanceof \ReflectionProperty && isset( $this->{$property->name} ) ) { $data[ $property->name ] = $this->{$property->name}; } } $data['icon_name'] = $this->getIcon(); /* Translators: %s - a length of time (e.g. "1 second") */ $data['relative_date'] = sprintf( __( '%s ago', 'easy-digital-downloads' ), human_time_diff( strtotime( $this->date_created ) ) ); return $data; } }