From 235b5aa4a13296af6cadd99ff35516c6f1187c36 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 26 Jun 2023 11:08:04 +0200 Subject: [PATCH] build a simple to_array converter --- includes/activity/class-actor.php | 4 +-- ...ivity-object.php => class-base-object.php} | 31 +++++++++++++++++-- includes/activity/class-person.php | 2 +- includes/functions.php | 11 +++++++ tests/test-class-activitypub-activity.php | 13 ++++++++ 5 files changed, 56 insertions(+), 5 deletions(-) rename includes/activity/{class-activity-object.php => class-base-object.php} (94%) diff --git a/includes/activity/class-actor.php b/includes/activity/class-actor.php index e9f810b..604261f 100644 --- a/includes/activity/class-actor.php +++ b/includes/activity/class-actor.php @@ -7,11 +7,11 @@ namespace Activitypub\Activity; -class Actor extends Activity_Object { +class Actor extends Base_Object { /** * @var string */ - protected $type = 'Object'; + protected $type = 'Person'; /** * A reference to an ActivityStreams OrderedCollection comprised of diff --git a/includes/activity/class-activity-object.php b/includes/activity/class-base-object.php similarity index 94% rename from includes/activity/class-activity-object.php rename to includes/activity/class-base-object.php index a71a83e..009cab3 100644 --- a/includes/activity/class-activity-object.php +++ b/includes/activity/class-base-object.php @@ -10,7 +10,7 @@ namespace Activitypub\Activity; use WP_Error; use function Activitypub\camel_to_snake_case; - +use function Activitypub\snake_to_camel_case; /** * ObjectType is an implementation of one of the * Activity Streams Core Types. @@ -23,7 +23,7 @@ use function Activitypub\camel_to_snake_case; * * @see https://www.w3.org/TR/activitystreams-core/#object */ -class Activity_Object { +class Base_Object { /** * The object's unique global identifier * @@ -562,4 +562,31 @@ class Activity_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; + } } diff --git a/includes/activity/class-person.php b/includes/activity/class-person.php index cf161bc..f2ed9b9 100644 --- a/includes/activity/class-person.php +++ b/includes/activity/class-person.php @@ -12,7 +12,7 @@ namespace Activitypub\Activity; * * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-person */ -class Person extends Activity_Object { +class Person extends Base_Object { /** * @var string */ diff --git a/includes/functions.php b/includes/functions.php index 212060e..2a17bfa 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -248,6 +248,17 @@ function camel_to_snake_case( $string ) { return strtolower( preg_replace( '/(? '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() ); + } }