Skip to content

Instantly share code, notes, and snippets.

@fullybaked
Created May 1, 2015 08:26
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save fullybaked/ab1fbd50f3b4790c3eb4 to your computer and use it in GitHub Desktop.
<?php
/**
* This is a _very_ rough draft of an idea to create a composable field in a model
* by which, the Model::find methods would return a value object rather plain data
* for a given field.
*
* Value Objects would be created in the /Lib directory of the application
*
* This is a work in progress hence being a Gist.
*/
class ComposableBehavior extends ModelBehavior
{
public function setup(Model $model, $settings = [])
{
$this->settings[$model->alias] = $settings;
}
public function afterFind(Model $model, $results, $primary = false)
{
foreach ($this->settings[$model->alias] as $field => $class) {
$class = Inflector::classify($class);
App::uses($class, 'Lib');
foreach ($results as &$result) {
if (isset($result[$model->alias][$field])) {
$object = new $class($result[$model->alias][$field]);
$result[$model->alias][$field] = $object;
}
}
}
return $results;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment