How to call a class inside a function

2

I have the following folder structure

/THEME
      /vendor
        autoload.php
        /composer
          files of composer
        /jflight
           /gacookie
              /src
              /tests
              other files
  composer.json
  composer.lock
  function.php

Inside src, has the class:

Utmz.php

    <?php namespace Jflight\GACookie;

use Jflight\GACookie\ParseInterface;

class Utmz extends Cookie
{
    /**
     * Timestamp
     * @var string
     */
    public $timestamp;

    /**
     * Session count
     * @var string
     */
    public $session_count;

    /**
     * Campaign number
     * @var string
     */
    public $campaign_number;

    /**
     * Source
     * @var string
     */
    public $source;

    /**
     * Medium
     * @var string
     */
    public $medium;

    /**
     * Campaign
     * @var string
     */
    public $campaign;

    /**
     * Term
     * @var string
     */
    public $term;

    /**
     * Content
     * @var string
     */
    public $content;

    /**
     * Maps the string values in the cookie to the properties of the Utmz object
     * @var array
     */
    protected $keyMappings = array(
                        'utmcsr' => 'source',
                        'utmcmd' => 'medium',
                        'utmccn' => 'campaign',
                        'utmctr' => 'term',
                        'utmcct' => 'content'
                        );

    /**
     * Constructor
     * @param DateTimeImmutable $date
     */
    public function __construct(\DateTime $date)
    {
        $this->date = $date;
    }

    /**
     * Parse cookie string and assign properties to this
     * @param  string $cookie The string from inside the cookie
     * @return static
     */
    public function parse($cookie)
    {
        $cookieBits = explode('.', $cookie, 5);
        $this->timestamp = $this->date->createFromFormat('U', $cookieBits[1]);
        $this->session_count = (integer) $cookieBits[2];
        $this->campaign_number = (integer) $cookieBits[3];
        $array = $this->paramsToArray($cookieBits);
        $this->setExtras($array);
        return $this;
    }

    /**
     * Logic for setting the extras values from the cookie as object properties
     * @param array $array
     */
    protected function setExtras($array)
    {
        if (is_array($array))
        {
            foreach ($array as $key => $item)
            {
                if (array_key_exists($key, $this->keyMappings))
                {
                    $property = $this->keyMappings[$key];
                    $this->$property = $item;
                }
            }
        }
    }

    /**
     * Logic for splitting the extra properties from string to array
     * @param  string $cookie
     * @return array
     */
    protected function paramsToArray($cookie)
    {
        if (isset($cookie[4]))
        {

            $pairs = explode('|', $cookie[4]);

            foreach ($pairs as $pair)
            {
                $item = explode('=', $pair);

                $return[$item[0]] = $item[1];
            }

            return $return;
        } 
    }
}

in the file function.php that is in the root I added:

require_once dirname(__FILE__).'/vendor/autoload.php';

function get_data_cookies(){

            // COMO EU CHAMO A CLASSE AQUI, QUE ESTA NO DIRETÓRIO /src/Utmz.php???


            // utmz

            $utmz->timestamp; // DateTime
            $utmz->session_count; // Integer
            $utmz->campaign_number; // Integer
            $utmz->source; // string
            $utmz->medium; // string
            $utmz->campaign; // string
            $utmz->term; // string
            $utmz->content; // string

}

How do I call the class within the function get_data_cookies ?

----------------- UPDATE ----------

require get_template_directory().'/vendor/autoload.php';

use Jflight\GACookie\GACookie;


function get_cookies(){

    $utma = GACookie::parse('utma');
    $utmz = GACookie::parse('utmz');
    var_dump( $utma );
    var_dump( $utmz ); 

                    //utma

             $args = $utma->time_of_first_visit; // DateTime
             $args = $utma->time_of_last_visit; // DateTime
             $args = $utma->time_of_current_visit; // DateTime
             $args = $utma->session_count; // Integer

            // utmz

             $args = $utmz->timestamp; // DateTime
             $args = $utmz->session_count; // Integer
             $args = $utmz->campaign_number; // Integer
             $args = $utmz->source; // string
             $args = $utmz->medium; // string
             $args = $utmz->campaign; // string
             $args = $utmz->term; // string
             $args = $utmz->content;  // string


}

In the message body of the contact form where I am trying to use this data I insert:

<?php get_cookies() ?>

but this returns

bool(false)bool(false)
    
asked by anonymous 04.11.2017 / 21:56

1 answer

0

The Utmz library class Jflight / GACookie needs to be instantiated and needs a string that represents the Google Analytics cookie to initialize. I do not know what context this library is using, but try this:

require_once dirname(__FILE__).'/vendor/autoload.php';

use \Jflight\GACookie\Utmz;

function get_data_cookies(){
  $utmz = new Utmz(new Datetime());
  $utmz->parse("GA1.2.1640324400.1505291004.utmcct=testando|utmcsr=teste");  

  $utmz->timestamp; // DateTime
  $utmz->session_count; // Integer
  $utmz->campaign_number; // Integer
  $utmz->source; // string
  $utmz->medium; // string
  $utmz->campaign; // string
  $utmz->term; // string
  $utmz->content; // string
}

However, read the library README.md file that has clear usage examples: link

    
05.11.2017 / 07:44