pilat
1 year ago

How do I get list of all available attributes in the model?

Posted 1 year ago by pilat

Hi,

What I need is to get a list of all available attributes (keys) on the model, including accessors.

Here's what I'm trying to do:

public function fillData($template)
{
    preg_match_all('/{([^}]+)}/', $template, $matches);
    $placeholders = $matches[1];

    $attributes = $this->getFullAttributesList(); // <<< HOW??

    $replaceData = [];

    foreach ($placeholders as $placeholder) {
    if (in_array($placehoder, $attribute)) {
        $replaceData[$placeholder] = $this->{$attribute};
        }
    }

    return $this->miniTwig($template, $replaceData);
}

protected function miniTwig($template, $data)
{
    $what = array_map(function ($key) {
        return '{' . $key . '}';
    }, array_keys($data));

    $with = array_values($data);

    return str_replace($what, $with, $template);
}


protected function getFullAttributesList()
{
    // is there a way?
}


/// Useage exmaple:

$model->fillData("The product {product_name} costs {price");

Please sign in or create an account to participate in this conversation.