<?php

namespace App\Services;

use Illuminate\Database\Eloquent\Model;

abstract class ModelPropertyParser {
    //todo parse multiple property values
    public static function parse(Model $model, $prop) {
        $result = null;
        collect(explode('->', $prop))->each(function($prop) use($model, &$result) {
            $func = $params = null;
            if (strpos($prop, '(')) {
                list($func, $params) = explode('(', $prop);
                $params = trim($params, '()"') ?: null;
            }
            if ($result && is_object($result)) $result = $func ? ($params ? $result->$func($params) : $result->$func()) : $result->$prop;
            else $result = $func ? ($params ? $model->$func($params) : $model->$func()) : $model->$prop;
            if (!$result) return false;
        });
        return $result;
    }

}