Created
May 1, 2015 08:26
-
-
Save fullybaked/ab1fbd50f3b4790c3eb4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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