build a simple to_array converter

This commit is contained in:
Matthias Pfefferle 2023-06-26 11:08:04 +02:00
parent ffa02e7b18
commit 235b5aa4a1
5 changed files with 56 additions and 5 deletions

View file

@ -7,11 +7,11 @@
namespace Activitypub\Activity; namespace Activitypub\Activity;
class Actor extends Activity_Object { class Actor extends Base_Object {
/** /**
* @var string * @var string
*/ */
protected $type = 'Object'; protected $type = 'Person';
/** /**
* A reference to an ActivityStreams OrderedCollection comprised of * A reference to an ActivityStreams OrderedCollection comprised of

View file

@ -10,7 +10,7 @@ namespace Activitypub\Activity;
use WP_Error; use WP_Error;
use function Activitypub\camel_to_snake_case; use function Activitypub\camel_to_snake_case;
use function Activitypub\snake_to_camel_case;
/** /**
* ObjectType is an implementation of one of the * ObjectType is an implementation of one of the
* Activity Streams Core Types. * Activity Streams Core Types.
@ -23,7 +23,7 @@ use function Activitypub\camel_to_snake_case;
* *
* @see https://www.w3.org/TR/activitystreams-core/#object * @see https://www.w3.org/TR/activitystreams-core/#object
*/ */
class Activity_Object { class Base_Object {
/** /**
* The object's unique global identifier * The object's unique global identifier
* *
@ -562,4 +562,31 @@ class Activity_Object {
return $object; return $object;
} }
/**
* Convert Object to an array.
*
* It tries to get the object attributes if they exist
* and falls back to the getters. Empty values are ignored.
*
* @return array An array built from the Object.
*/
public function to_array() {
$array = array();
$vars = get_object_vars( $this );
foreach ( $vars as $key => $value ) {
// if value is empty, try to get it from a getter.
if ( ! $value ) {
$value = call_user_func( array( $this, 'get_' . $key ) );
}
// if value is still empty, ignore it for the array and continue.
if ( $value ) {
$array[ snake_to_camel_case( $key ) ] = $value;
}
}
return $array;
}
} }

View file

@ -12,7 +12,7 @@ namespace Activitypub\Activity;
* *
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-person * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-person
*/ */
class Person extends Activity_Object { class Person extends Base_Object {
/** /**
* @var string * @var string
*/ */

View file

@ -248,6 +248,17 @@ function camel_to_snake_case( $string ) {
return strtolower( preg_replace( '/(?<!^)[A-Z]/', '_$0', $string ) ); return strtolower( preg_replace( '/(?<!^)[A-Z]/', '_$0', $string ) );
} }
/**
* Convert a string from snake_case to camelCase.
*
* @param string $string The string to convert.
*
* @return string The converted string.
*/
function snake_to_camel_case( $string ) {
return lcfirst( str_replace( '_', '', ucwords( $string, '_' ) ) );
}
/** /**
* Check if a request is for an ActivityPub request. * Check if a request is for an ActivityPub request.
* *

View file

@ -28,4 +28,17 @@ class Test_Activitypub_Activity extends WP_UnitTestCase {
remove_all_filters( 'activitypub_extract_mentions' ); remove_all_filters( 'activitypub_extract_mentions' );
\wp_trash_post( $post ); \wp_trash_post( $post );
} }
public function test_object_transformation() {
$test_array = array(
'id' => 'https://example.com/post/123',
'type' => 'Note',
'content' => 'Hello world!',
);
$object = \Activitypub\Activity\Base_Object::from_array( $test_array );
$this->assertEquals( 'Hello world!', $object->get_content() );
$this->assertEquals( $test_array, $object->to_array() );
}
} }