<commit_before><?php

namespace ShopifyApi\Models;

use DateTime;
use BadMethodCallException;
use ShopifyApi\Util;
use ShopifyApi\Client;
use ShopifyApi\Api\AbstractApi;

/**
 * Class AbstractModel
 */
abstract class AbstractModel
{

    /** @var string $api_name */
    protected static $api_name;

    /** @var array $load_params */
    protected static $load_params = [];

    /** @var Client $client */
    protected $client;

    /** @var AbstractApi $api */
    protected $api;

    /** @var array $fields */
    protected $fields;

    /** @var string $id */
    protected $id;

    /** @var array $data */
    protected $data;

    /**
     * Constructor.
     *
     * @param $client Client
     * @param string  $id       The id of the object
     */
    public function __construct(Client $client, $id = null)
    {
        $this->client = $client;
        $this->api = $client->api(static::$api_name);
        $this->fields = $this->api->getFields();

        if ($id) {
            $this->id = $id;
            $this->refresh();
        }
    }

    /**
     * Because __get() was being a dick.
     *
     * @param $method
     * @param $arguments
     * @return mixed
     */
    public function __call($method, $arguments)
    {
        $chunks = explode('_', Util::snake($method));

        if (count($chunks) > 1) {
            $action = array_shift($chunks);
            $key = implode('_', $chunks);

            switch ($action) {
                case 'get':
                    return $this->data[$key];
                    break;
                case 'set':
                    $this->data[$key] = $arguments[0];
                    return $this;
                    break;
                case 'has':
                    return array_key_exists($key, $this->data);
                    break;
            }
        }

        throw new BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()');
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return array
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @return $this
     */
    public function setData($data)
    {
        $this->data = $data;

        return $this;
    }

    /**
     * @param $name
     * @return mixed
     */
    public function getOriginal($name)
    {
        return $this->data[$name];
    }

    /**
     * @param DateTime|string
     * @return $this
     */
    public function setCreatedAt($stringOrDateTime)
    {
        $this->data['created_at'] = $stringOrDateTime instanceof DateTime
            ? $stringOrDateTime->format('c') : $stringOrDateTime;

        return $this;
    }
<commit_msg>Fixed bug fetching Id on instantiations without an id.<commit_after><?php

namespace ShopifyApi\Models;

use DateTime;
use BadMethodCallException;
use ShopifyApi\Util;
use ShopifyApi\Client;
use ShopifyApi\Api\AbstractApi;

/**
 * Class AbstractModel
 */
abstract class AbstractModel
{

    /** @var string $api_name */
    protected static $api_name;

    /** @var array $load_params */
    protected static $load_params = [];

    /** @var Client $client */
    protected $client;

    /** @var AbstractApi $api */
    protected $api;

    /** @var array $fields */
    protected $fields;

    /** @var string $id */
    protected $id;

    /** @var array $data */
    protected $data;

    /**
     * Constructor.
     *
     * @param $client Client
     * @param string  $id       The id of the object
     */
    public function __construct(Client $client, $id = null)
    {
        $this->client = $client;
        $this->api = $client->api(static::$api_name);
        $this->fields = $this->api->getFields();

        if ($id) {
            $this->id = $id;
            $this->refresh();
        }
    }

    /**
     * Because __get() was being a dick.
     *
     * @param $method
     * @param $arguments
     * @return mixed
     */
    public function __call($method, $arguments)
    {
        $chunks = explode('_', Util::snake($method));

        if (count($chunks) > 1) {
            $action = array_shift($chunks);
            $key = implode('_', $chunks);

            switch ($action) {
                case 'get':
                    return $this->data[$key];
                    break;
                case 'set':
                    $this->data[$key] = $arguments[0];
                    return $this;
                    break;
                case 'has':
                    return array_key_exists($key, $this->data);
                    break;
            }
        }

        throw new BadMethodCallException('Call to undefined method ' . get_class($this) . '::' . $method . '()');
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return array
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param array $data
     * @return $this
     */
    public function setData(array $data)
    {
        $this->id = isset($data['id']) ? $data['id'] : null;
        $this->data = $data;

        return $this;
    }

    /**
     * @param $name
     * @return mixed
     */
    public function getOriginal($name)
    {
        return $this->data[$name];
    }

    /**
     * @param DateTime|string
     * @return $this
     */
    public function setCreatedAt($stringOrDateTime)
    {
        $this->data['created_at'] = $stringOrDateTime instanceof DateTime
            ? $stringOrDateTime->format('c') : $stringOrDateTime;

        return $this;
    }

{'fraction_non_alphanumeric': 0.14455060155697097, 'fraction_numerical': 0.0007077140835102619, 'mean_word_length': 1.6465355805243447, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7530417', 'n_tokens_mistral': 1694, 'n_tokens_neox': 1635, 'n_words': 388}