Skip to content

Propose Moving Model::classToArray() method to Entity class as public method #579

Description

@daylightstudio

I've found it useful to be able for a model's entity class to return an array of the values being saved (e.g. for a toJson method on the Entity class). Currently the method that extracts that data is in the Model class but protected. I would like to suggest moving that method to the Entity class as public and have the Model class call it instead.

protected $exclude = [];

public function values()
{
	$mirror = new \ReflectionClass($this);
	$props  = $mirror->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED);

	$properties = [];

	$exclude = array_merge(['datamap', 'exclude'], $this->exclude);

	// Loop over each property,
	// saving the name/value in a new array we can return.
	foreach ($props as $prop)
	{
		if (in_array($prop->getName(), $exclude))
		{
			continue;
		}

		// Must make protected values accessible.
		$prop->setAccessible(true);
		$properties[$prop->getName()] = $prop->getValue($this);
	}

	return $properties;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions