diff --git a/omnisend/includes/Internal/V1/class-client.php b/omnisend/includes/Internal/V1/class-client.php index bc9aea8..9a89055 100644 --- a/omnisend/includes/Internal/V1/class-client.php +++ b/omnisend/includes/Internal/V1/class-client.php @@ -19,6 +19,7 @@ class Client implements \Omnisend\SDK\V1\Client { + private string $api_key; private string $plugin_name; private string $plugin_version; diff --git a/omnisend/includes/SDK/V1/class-client.php b/omnisend/includes/SDK/V1/class-client.php index bfb36db..a60afa6 100644 --- a/omnisend/includes/SDK/V1/class-client.php +++ b/omnisend/includes/SDK/V1/class-client.php @@ -31,7 +31,7 @@ public function create_contact( $contact ): CreateContactResponse; * * @param Event $event * - * @return CreateContactResponse + * @return SendCustomerEventResponse */ public function send_customer_event( $event ): SendCustomerEventResponse; } diff --git a/omnisend/includes/SDK/V1/class-event.php b/omnisend/includes/SDK/V1/class-event.php index 85da385..6cf57eb 100644 --- a/omnisend/includes/SDK/V1/class-event.php +++ b/omnisend/includes/SDK/V1/class-event.php @@ -7,6 +7,7 @@ namespace Omnisend\SDK\V1; +use Omnisend\SDK\V1\EventContact; use WP_Error; defined( 'ABSPATH' ) || die( 'no direct access' ); @@ -17,16 +18,17 @@ */ class Event { - private $contact = null; - private $event_name = null; - private $event_time = null; - private $origin = null; - private $properties = null; + + private $contact = null; + private $event_name = null; + private $event_time = null; + private $origin = null; + private array $properties = array(); /** * Validate event properties. * - * todo kas yra reuquired? ka reikia vailiduoti? + * TODO kas yra reuquired? ka reikia vailiduoti? * It ensures that phone or email is set and that they are valid. In addition other properties are validated if they are expected type and format. * * @return WP_Error @@ -34,12 +36,8 @@ class Event { public function validate(): WP_Error { $error = new WP_Error(); - - if ( $this->contact != null ) { - $contact_error = $this->contact->validate; - if ( is_wp_error( $contact_error ) ) { - $error->add( $contact_error ); - } + if ( $contact instanceof EventContact ) { + $error->merge_from( $contact->validate() ); } return $error; @@ -69,7 +67,7 @@ public function set_event_time( $event_time ): void { /** - * Sets event origin.". //todo event origin ar just origin? + * Sets event origin. * * @param $origin * @@ -80,14 +78,17 @@ public function set_origin( $origin ): void { } /** - * Sets event properties. //todo event properties ar just properties? - * - * @param $properties + * @param $key + * @param $value * * @return void */ - public function set_properties( $properties ): void { - $this->properties = $properties; + public function add_properties( $key, $value ): void { + if ( $key == '' ) { + return; + } + + $this->properties[ $key ] = $value; } /** @@ -100,4 +101,45 @@ public function set_properties( $properties ): void { public function set_contact( $contact ): void { $this->contact = $contact; } + + /** + * Convert event to array. + * + * If event is valid it will be transformed to array that can be sent to Omnisend. + * + * @return array + */ + public function to_array(): array { + if ( $this->validate()->has_errors() ) { + return array(); + } + + $time_now = gmdate( 'c' ); + + $arr = array(); + + if ( $this->contact ) { + $arr['contact'] = $this->contact->to_array(); + } + + if ( $this->event_name ) { + $arr['eventName'] = $this->event_name; + } + + if ( $this->event_time ) { + $arr['eventTime'] = $this->event_time; + } else { + $arr['eventTime'] = $this->$time_now; + } + + if ( $this->origin ) { + $arr['origin'] = $this->origin; + } + + if ( $this->properties ) { + $arr['properties'] = $this->properties; + } + + return $arr; + } } diff --git a/omnisend/includes/SDK/V1/class-eventcontact.php b/omnisend/includes/SDK/V1/class-eventcontact.php new file mode 100644 index 0000000..46dfd1e --- /dev/null +++ b/omnisend/includes/SDK/V1/class-eventcontact.php @@ -0,0 +1,122 @@ +email != null && ! is_email( $this->email ) ) { + $error->add( 'email', 'Not a email.' ); + } + + if ( $this->phone == null && $this->email == null && $this->id == null ) { + $error->add( 'identifier', 'Phone or email or ID must be set.' ); + } + + $string_properties = array( + 'id', + 'email', + 'phone', + ); + + foreach ( $string_properties as $property ) { + if ( $this->$property != null && ! is_string( $this->$property ) ) { + $error->add( $property, 'Not a string.' ); + } + } + + return $error; + } + + /** + * Sets event name. + * + * @param $id + * + * @return void + */ + public function set_id( $id ): void { + $this->id = $id; + } + + /** + * Sets event time. + * + * @param $email + * + * @return void + */ + public function set_email( $email ): void { + $this->email = $email; + } + + + /** + * Sets event phone. + * + * @param $phone + * + * @return void + */ + public function set_phone( $phone ): void { + $this->phone = $phone; + } + + /** + * Convert event to array. + * + * If event is valid it will be transformed to array that can be sent to Omnisend. + * + * @return array + */ + public function to_array(): array { + if ( $this->validate()->has_errors() ) { + return array(); + } + + $arr = array(); + + if ( $this->id ) { + $arr['id'] = $this->id; + } + + if ( $this->email ) { + $arr['email'] = $this->email; + } + + if ( $this->phone ) { + $arr['phone'] = $this->phone; + } + + return $arr; + } +} diff --git a/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php b/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php index f03df5b..5f6418d 100644 --- a/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php +++ b/omnisend/includes/SDK/V1/class-sendcustomereventresponse.php @@ -17,7 +17,6 @@ class SendCustomerEventResponse { private WP_Error $wp_error; /** - * @param string $contact_id * @param WP_Error $wp_error */ public function __construct( WP_Error $wp_error ) {