Jump to content

Translating Data with a Property-Value Object


iwato

Recommended Posts

BACKGROUND:  Many moons ago it was suggested that I create a CRON job that would read into a MySQL table the results of a method call to a Matomo API call and thereby speed up the rendering of data for visitors to the Grammar Captive website.  I have since come to learn that this technique is called data translation.  The following code creates a class object that when inserted into a foreach loop is suppose to fill the properties of the object for further manipulation.  Unfortunately it does perform the intended task.

THE CLASS

	class TranslateMatomoAction {
		public $mysqli_obj;
		public $visitID;
		public $type;
		public $serverTimePretty;
		public $timestamp;
		public $url;
		public $generationTime;
		public $pageTitle;
		public $pageID;
		public $pageView_ID;
		public $pageID_Action;
		public $timeSpent;
		public $eventCategory;
		public $eventAction;
		public $eventName;

		public function __construct($property, $value) {
			if ($property === 'type') {
				$this->type = $value;
			} else if ($property === 'serverTimePretty') {
				$this->serverTimePretty = $value;
			} else if ($property === 'timestamp') {
				$this->timestamp = $value;
			} else if ($property === 'url') {
				$this->url = $value;
			} else if ($property === 'generationTimeMilliseconds') {
				$this->generationTime = $value;
			} else if ($property === 'pageTitle') {
				$this->pageTitle = $value;
			} else if ($property === 'pageId') {
				$this->pageID = $value;
			} else if ($property === 'idpageview') {
				$this->pageView_ID = $value;
			} else if ($property === 'pageIdAction') {
				$this->pageID_Action = $value;
			} else if ($property === 'timeSpent') {
				$this->timeSpent = $value;
			} else if ($property === 'eventCategory') {
				$this->eventCategory = $value;
			} else if ($property === 'eventAction') {
				$this->eventAction = $value;
			} else if ($property === 'eventName') {
				$this->eventName = $value;
			}
		}
		public function set_visitID($visitID) {
			$this->visitID = $visitID;
		}
		public function set_mysqli_obj($mysqli_obj) {
			$this->visitID = $visitID;
		}
	}

THE INSERTED CLASS AND ASSOCIATED CODE

				if (is_array($value1) && ($key1 === 'actionDetails')) {
					foreach($value1 as $key2 => $value2) {

					//LEVEL 2 - This foreach statement traverses the set of indexed arrays whose elements are a key-value pair of indices and arrays. Each array is a value of the actionDetails element of a visit.

						if (is_array($value2)) {
							$matomo_action = new TranslateMatomoAction($key3,$value3);
							foreach($value2 as $key3 => $value3) {

							//LEVEL 3 - This foreach statement traverses the elements of the associative array corresponding to the value of $key2.
		
								$matomo_action($key3, $value);
							}
						}
					}
				}

DILEMMA:  Unfortunately, the above procedure fails to fill the values of the object's property as expected.

QUESTION:  Where have I gone wrong?

Roddy

 

 

 

Link to comment
Share on other sites

10 minutes ago, iwato said:

BACKGROUND:  Many moons ago it was suggested that I create a CRON job that would read into a MySQL table the results of a method call to a Matomo API call and thereby speed up the rendering of data for visitors to the Grammar Captive website.  I have since come to learn that this technique is called data translation.

Data translation, is that what its called? I didn't know that. 

10 minutes ago, iwato said:

QUESTION:  Where have I gone wrong?

Hey Roddy!

I guess the issue here is that you're using the object incorrectly, as its currently defined in your class.

__construct is only called when the object is created. ($matomo_action = new TranslateMatomoAction(arg1, arg2);)
I can see you're trying to be clever here, and fortunately you can be super clever with a slight tweak.

By instead defining that logic in the __invoke magic function, you can get exactly the behaviour you desire. Your level 3 action is already attempting to use it as such.

For the inserted class, all you'd need is something like this. (untested, working off brain here)

<?php				
if (is_array($value1) && ($key1 === 'actionDetails')) {
  foreach($value1 as $key2 => $value2) {

    //LEVEL 2 - This foreach statement traverses the set of indexed arrays whose elements are a key-value pair of indices and arrays. Each array is a value of the actionDetails element of a visit.

    if (is_array($value2)) {
      $matomo_action = new TranslateMatomoAction();
      foreach($value2 as $key3 => $value3) {

        //LEVEL 3 - This foreach statement traverses the elements of the associative array corresponding to the value of $key2.

        $matomo_action($key3, $value);
      }
    }
  }
}
?>
  • Thanks 1
Link to comment
Share on other sites

Hi, Funce!

Thank you for your quick response. It was you who gave me the idea of creating a data table.  And, now I have two: one for each visit and one for all of the actions and events associated with each visit.  Hooray! Hooray!

In regard to the TranslateMatomoAction class I understand now clearly about the __contructor() function.  I was trying to make it do something that it was not intended to do.  This said, I was unable to get the __invoke() function to work as you suggested.  So, I moved the contents of the __constructor() function outside of the class and changed the variable names. My object is now fully complemented with the desired information is ready for MySQL insertion.

Many thanks!

Roddy

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...