/** * Setup * * Sets up the configuration for the model * * @author Paul Smith <*****@*****.**> * @since 1.0 * @param Model $model * @param array $config * @return void */ public function setup(Model $Model, $settings = array()) { foreach (array('json', 'serialized') as $type) { if (isset($settings[$type])) { // Automatically parse json & serialized field structure from model attributes $settings[$type] = Hash::normalize($settings[$type]); foreach ($settings[$type] as $field => $attribute) { if (empty($attribute)) { $settings[$type][$field] = $attribute = $field; } } $modelAttributes = Hash::expand($Model->attributes()); foreach ($settings[$type] as $field => $attribute) { $match = Hash::extract($modelAttributes, $attribute); if (!empty($match)) { $this->settings[$Model->alias][$type][$field] = $match; } } } else { $this->settings[$Model->alias][$type] = array(); } } $this->settings[$Model->alias]['virtual'] = isset($settings['virtual']) ? $settings['virtual'] : false; $this->settings[$Model->alias]['callback'] = isset($settings['callback']) ? $settings['callback'] : false; // Set up callback fields for this model if ($this->settings[$Model->alias]['callback']) { $this->__setupSpecialFields($Model, 'callback'); } }
/** * {@inheritdoc} */ public function setup(Model $Model, $config = array()) { if (empty($config)) { $config = $this->_defaults; } $this->settings[$Model->alias] = Hash::normalize((array) $config); }
/** * Test Instance Setup * * @author Everton Yoshitani <*****@*****.**> * @since 1.0 * @return void */ public function testInstanceSetup() { $components = Hash::normalize($this->Permissions->components); $this->assertArrayHasKey('Auth', $components); $this->assertInstanceOf('AuthComponent', $this->Permissions->Auth); $this->assertArrayHasKey('Acl', $components); $this->assertInstanceOf('AclComponent', $this->Permissions->Acl); }
/** * {@inheritdoc} */ public function setup(Model $Model, $config = array()) { if (!empty($config) && (!array_key_exists('fields', (array) $config) && Hash::numeric((array) array_keys($config)))) { $config = array('fields' => Hash::normalize($config)); } $this->settings[$Model->alias] = array_merge($this->_defaults, $config); $this->bindSecurityToken($Model); }
/** * Test Instance Setup * * @author Everton Yoshitani <*****@*****.**> * @since 1.0 * @return void */ public function testInstanceSetup() { // Test Components $components = Hash::normalize($this->Api->components); $this->assertArrayHasKey('Query', $components); $this->assertArrayHasKey('InputData', $components); $this->assertArrayHasKey('Permissions', $components); $this->assertArrayHasKey('Resource', $components); $this->assertArrayHasKey('ApiPaginator', $components); $this->assertArrayHasKey('ApiRequestHandler', $components); }
public function constructLimits() { $global = array('callback' => array('flash', array('security.access_limit.fail')), 'duration' => '+1 hour', 'limit' => 5); if (!empty($this->settings[AccessLimitComponent::ALL])) { $global = array_merge($global, $this->settings[AccessLimitComponent::ALL]); unset($this->settings[AccessLimitComponent::ALL]); } foreach ($this->settings as $requestType => $settings) { $this->{$requestType} = array(); foreach (Hash::normalize($settings) as $action => $limit) { if (!empty($limit) && !is_array($limit)) { $limit = compact('limit'); } $this->{$requestType}[$action] = array_merge($global, (array) $limit); } } }
/** * Overrides `Model::__construct()`. * * - Auto-load plugin behaviors using the 'Model.construct' event. * * @param integer|string|array $id Set this ID for this model on startup, can also be an array of options, see above. * @param string $table Name of database table to use. * @param string $ds DataSource connection name. */ public function __construct($id = false, $table = null, $ds = null) { parent::__construct($id, $table, $ds); foreach (get_class_methods($this) as $method) { $key = lcfirst(str_replace('_find', '', $method)); if (isset($this->findMethods[$key]) || strpos($method, '_find') !== 0) { continue; } $ReflectionMethod = new ReflectionMethod($this, $method); $params = $ReflectionMethod->getParameters(); foreach (array('state', 'query', 'results') as $k => $name) { if ((array) $params[$k] != compact('name')) { $key = null; break; } } if (!empty($key)) { $this->findMethods[$key] = true; } } if (empty($this->friendly)) { $this->friendly = Inflector::humanize(Inflector::underscore($this->name)); } $timer = 'modelConstruct' . $this->alias; Common::startTimer($timer, __d('common', "Event: Model.construct (%s)", $this->alias)); $result = array_merge(array('actsAs' => array()), (array) $this->triggerEvent('Model.construct', $this)); $this->actsAs = Hash::merge(Hash::normalize((array) $result['actsAs']), Hash::normalize((array) $this->actsAs)); unset($result['actsAs']); foreach ($this->actsAs as $name => $config) { list(, $behavior) = pluginSplit($name); if (!$this->Behaviors->loaded($behavior)) { $this->Behaviors->load($name, $config); } } $this->_set($result); Common::stopTimer($timer); }
/** * Merge each of the keys in a property together. * * @param array $current The current merged value. * @param array $parent The parent class' value. * @param bool $isAssoc Whether or not the merging should be done in associative mode. * @return mixed The updated value. */ protected function _mergePropertyData($current, $parent, $isAssoc) { if (!$isAssoc) { return array_merge($parent, $current); } $parent = Hash::normalize($parent); foreach ($parent as $key => $value) { if (!isset($current[$key])) { $current[$key] = $value; } } return $current; }
/** * {@inheritdoc} * * @return ConsoleOptionParser */ public function getOptionParser() { $parser = parent::getOptionParser(); $parser->description('There is no help'); $taskNames = array_keys(Hash::normalize($this->tasks)); foreach ($taskNames as $taskName) { $Task = $this->{$taskName}; $parser->addSubcommand(Inflector::underscore($Task->name), array('help' => $Task->getOptionParser()->description(), 'parser' => $Task->getOptionParser())); } return $parser; }
/** * Overrides `Controller::constructClasses()` to apply the 'Controller.constructClasses' * event's results. * * @return void */ public function constructClasses() { $timer = 'controllerConstructClasses'; Common::startTimer($timer, __d('common', "Event: Controller.constructClasses")); $properties = array('components', 'helpers'); $result = $this->triggerEvent('Controller.constructClasses', $this); $this->_mergeControllerVars(); foreach ($properties as $property) { if (!isset($result[$property])) { continue; } $this->{$property} = Hash::merge(Hash::normalize($result[$property]), Hash::normalize((array) $this->{$property})); unset($result[$property]); } $this->_set($result); Common::stopTimer($timer); parent::constructClasses(); }
/** * Normalize action configuration * * If an action doesn't have a CrudClass specified (the value part of the array) * try to compute it by exploding on action name on '_' and take the last chunk * as CrudClass identifier. * * @param mixed $types Class type(s). * @return void * @throws CakeException If className is missing for listener. */ protected function _normalizeConfig($types = null) { if (!$types) { $types = array('listeners', 'actions'); } foreach ((array) $types as $type) { $this->settings[$type] = Hash::normalize($this->settings[$type]); foreach ($this->settings[$type] as $name => $settings) { if (is_array($settings) && !empty($settings['className'])) { $this->settings[$type][$name] = $settings; continue; } $className = null; if (empty($settings)) { $settings = array(); } elseif (is_string($settings)) { $className = $settings; $settings = array(); } if ($type === 'listeners' && strpos($name, '.') !== false) { unset($this->settings[$type][$name]); $settings['className'] = $name; list($plugin, $name) = pluginSplit($name); $name = Inflector::camelize($name); } $className = $this->_handlerClassName($name, $className); if (empty($settings['className'])) { $settings['className'] = $className; } $this->settings[$type][$name] = $settings; } } }
/** * Turn off associations on the fly. * * If $reset is false, association will not be reset * to the originals defined in the model * * Example: Turn off the associated Model Support request, * to temporarily lighten the User model: * * `$this->User->unbindModel(array('hasMany' => array('SupportRequest')));` * Or alternatively: * `$this->User->unbindModel(array('hasMany' => 'SupportRequest'));` * * Unbound models that are not made permanent will reset with the next call to Model::find() * * @param array $params Set of bindings to unbind (indexed by binding type) * @param bool $reset Set to false to make the unbinding permanent * * @return bool Success * @link http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly */ public function unbindModel($params, $reset = TRUE) { foreach ($params as $assoc => $models) { if ($reset === TRUE && !isset($this->__backAssociation[$assoc])) { $this->__backAssociation[$assoc] = $this->{$assoc}; } $models = Hash::normalize((array) $models, FALSE); foreach ($models as $model) { if ($reset === FALSE && isset($this->__backAssociation[$assoc][$model])) { unset($this->__backAssociation[$assoc][$model]); } unset($this->{$assoc}[$model]); } } return TRUE; }
/** * Test Attributes * * @author Everton Yoshitani <*****@*****.**> * @since 1.0 * @return void */ public function testAttributes() { $this->TestModel->expects($this->exactly(2))->method('schema')->with()->will($this->returnValue(array('some_field' => array('type' => 'integer'), 'some_other_field' => array('type' => 'string'), 'created' => array('type' => 'datetime')))); $attributes = $this->TestModel->_attributes; $attributes['someAttribute'] = array('field' => 'some_field'); $attributes['someOtherAttribute'] = array('field' => 'some_other_field'); $attributes['created'] = array(); $expected = Hash::normalize($attributes); $expected['someAttribute']['type'] = 'int'; $expected['someOtherAttribute']['type'] = 'string'; $expected['created'] = array('field' => 'created', 'type' => 'datetime'); $result = $this->ApiBehavior->attributes($this->TestModel, $attributes); $this->assertEquals($expected, $result); $result = $this->ApiBehavior->attributes($this->TestModel); $this->assertEquals($expected, $result); }
/** * ApiComponent::$recordMapを走査するためのヘルパーメソッドです。 * * @param callable $callback * @return void */ protected function _walkMap(callable $callback) { foreach ($this->recordMap as $alias => $map) { $map = Hash::normalize($map); $wrap = null; if (array_key_exists('_wrap', $map)) { $wrap = $map['_wrap']; unset($map['_wrap']); } foreach ($map as $recordField => $paramsField) { if (!is_array($paramsField) && !$paramsField) { $paramsField = $recordField; } $callback($paramsField, $alias, $recordField, $wrap); } } }
/** * Normalizes and sets config options as properties */ public function config() { if (empty($this->options['models'])) { return; } $this->models = Hash::normalize($this->options['models']); if (isset($this->options['prependPk'])) { $this->prependPk = $this->options['prependPk']; } if (isset($this->options['slugFunction'])) { $this->slugFunction = $this->options['slugFunction']; } }
/** * Test Instance Setup * * @author Anthony Putignano <*****@*****.**> * @since 1.0 * @return void */ public function testInstanceSetup() { $components = Hash::normalize($this->InputData->components); $this->assertArrayHasKey('Query', $components); }
/** * [_render description] * @param [type] $type [description] * @return [type] */ protected function _render($type) { // @todo Replace the `AssetCompress` check by a `Reveal` rule. if (in_array($type, array('js', 'css')) && CakePlugin::loaded('AssetCompress') && isset($this->_View->AssetCompress)) { foreach ($this->{$type} as $path => $args) { $targets = $this->_View->AssetCompress->config()->targets($type); $exists = false; foreach ($targets as $target) { $files = $this->_View->AssetCompress->config()->files($target); if (in_array($this->_addExt($path, $type), $files) || in_array($path, $files)) { $exists = true; break; } } $args = array_merge(Hash::normalize(array('path', 'rel', 'options')), compact('path') + $args); if (!$exists) { if ('js' == $type) { $this->_View->Html->script($path, (array) $args['options']); } else { $this->_View->Html->css($path, $args['rel'], (array) $args['options']); } continue; } $haystack = $this->_View->fetch($args['options']['block']); $target = str_replace(array('.css', '.js'), array('', ''), $target); $needle = DS . 'assets' . DS . $target; if (strpos($haystack, $needle) === false) { $method = $type; if ('js' == $method) { $method = 'script'; } $this->_View->AssetCompress->{$method}($target, (array) $args['options']); } } return; } foreach ($this->{$type} as $path => $args) { extract($args); switch ($type) { case 'css': if (!isset($haystack)) { $haystack = $this->_View->fetch('css'); } $needle = $this->_View->Html->css($path, $rel, $options); if (strpos($haystack, $needle) === false) { $this->_View->prepend('css', $needle); } break; case 'js': $this->_View->Html->script($path, $options); break; case 'scriptBlock': $this->_View->Html->scriptBlock($script, $options); break; default: } } }
/** * Ensure data key is present in Controller:$viewVars * * @param CrudSubject $subject * @return void */ protected function _ensureData(CrudSubject $subject) { $controller = $this->_controller(); // Don't touch existing data properties if (isset($controller->viewVars['data'])) { return; } $key = $subject->success ? 'success' : 'error'; // Load configuration $config = $this->_action()->config('api.' . $key); // New, empty, data array $data = array(); // If fields should be extracted from the subject if (isset($config['data']['subject'])) { $config['data']['subject'] = Hash::normalize((array) $config['data']['subject']); $subjectArray = (array) $subject; foreach ($config['data']['subject'] as $keyPath => $valuePath) { if ($valuePath === null) { $valuePath = $keyPath; } $keyPath = $this->_expandPath($subject, $keyPath); $valuePath = $this->_expandPath($subject, $valuePath); $data = Hash::insert($data, $keyPath, Hash::get($subjectArray, $valuePath)); } } // Raw (hardcoded) key/values if (isset($config['data']['raw'])) { foreach ($config['data']['raw'] as $path => $value) { $path = $this->_expandPath($subject, $path); $data = Hash::insert($data, $path, $value); } } // Publish the new data $controller->set('data', $data); }
/** * Turn off associations on the fly. * * If $reset is false, association will not be reset * to the originals defined in the model * * Example: Turn off the associated Model Support request, * to temporarily lighten the User model: * * `$this->User->unbindModel(array('hasMany' => array('SupportRequest')));` * Or alternatively: * `$this->User->unbindModel(array('hasMany' => 'SupportRequest'));` * * Unbound models that are not made permanent will reset with the next call to Model::find() * * @param array $params Set of bindings to unbind (indexed by binding type) * @param bool $reset Set to false to make the unbinding permanent * @return bool Success * @link http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#creating-and-destroying-associations-on-the-fly */ public function unbindModel($params, $reset = true) { foreach ($params as $assoc => $models) { if ($reset === true && !isset($this->__backAssociation[$assoc])) { $this->__backAssociation[$assoc] = $this->{$assoc}; } $models = Hash::normalize((array) $models, false); foreach ($models as $model) { if ($reset === false && isset($this->__backAssociation[$assoc][$model])) { unset($this->__backAssociation[$assoc][$model]); } unset($this->{$assoc}[$model]); } } return true; }
/** * Normalizes a string or array list. * * @param mixed $list List to normalize * @param boolean $assoc If true, $list will be converted to an associative array * @param string $sep If $list is a string, it will be split into an array with $sep * @param boolean $trim If true, separated strings will be trimmed * @return array * @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::normalize */ public static function normalize($list, $assoc = true, $sep = ',', $trim = true) { if (is_string($list)) { $list = explode($sep, $list); if ($trim) { foreach ($list as $key => $value) { $list[$key] = trim($value); } } if ($assoc) { return Hash::normalize($list); } } elseif (is_array($list)) { $list = Hash::normalize($list, $assoc); } return $list; }
/** * test normalizing arrays * * @return void */ public function testNormalize() { $result = Hash::normalize(array('one', 'two', 'three')); $expected = array('one' => null, 'two' => null, 'three' => null); $this->assertEquals($expected, $result); $result = Hash::normalize(array('one', 'two', 'three'), false); $expected = array('one', 'two', 'three'); $this->assertEquals($expected, $result); $result = Hash::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four'), false); $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null); $this->assertEquals($expected, $result); $result = Hash::normalize(array('one' => 1, 'two' => 2, 'three' => 3, 'four')); $expected = array('one' => 1, 'two' => 2, 'three' => 3, 'four' => null); $this->assertEquals($expected, $result); $result = Hash::normalize(array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three')); $expected = array('one' => array('a', 'b', 'c' => 'cee'), 'two' => 2, 'three' => null); $this->assertEquals($expected, $result); }
/** * testMergeVars method * * @return void */ public function testMergeVars() { $request = new CakeRequest('controller_posts/index'); $TestController = new TestController($request); $TestController->constructClasses(); $testVars = get_class_vars('TestController'); $appVars = get_class_vars('ControllerTestAppController'); $components = is_array($appVars['components']) ? array_merge($appVars['components'], $testVars['components']) : $testVars['components']; if (!in_array('Session', $components)) { $components[] = 'Session'; } $helpers = is_array($appVars['helpers']) ? array_merge($appVars['helpers'], $testVars['helpers']) : $testVars['helpers']; $uses = is_array($appVars['uses']) ? array_merge($appVars['uses'], $testVars['uses']) : $testVars['uses']; $this->assertEquals(0, count(array_diff_key($TestController->helpers, array_flip($helpers)))); $this->assertEquals(0, count(array_diff($TestController->uses, $uses))); $this->assertEquals(count(array_diff_assoc(Hash::normalize($TestController->components), Hash::normalize($components))), 0); $expected = array('ControllerComment', 'ControllerAlias', 'ControllerPost'); $this->assertEquals($expected, $TestController->uses, '$uses was merged incorrectly, ControllerTestAppController models should be last.'); $TestController = new AnotherTestController($request); $TestController->constructClasses(); $appVars = get_class_vars('ControllerTestAppController'); $testVars = get_class_vars('AnotherTestController'); $this->assertTrue(in_array('ControllerPost', $appVars['uses'])); $this->assertFalse($testVars['uses']); $this->assertFalse(property_exists($TestController, 'ControllerPost')); $TestController = new ControllerCommentsController($request); $TestController->constructClasses(); $appVars = get_class_vars('ControllerTestAppController'); $testVars = get_class_vars('ControllerCommentsController'); $this->assertTrue(in_array('ControllerPost', $appVars['uses'])); $this->assertEquals(array('ControllerPost'), $testVars['uses']); $this->assertTrue(isset($TestController->ControllerPost)); $this->assertTrue(isset($TestController->ControllerComment)); }
/** * Setup this behavior with the specified configuration settings. * * @param Model $model Model using this behavior * @param array $config Configuration settings for $model * @return void */ public function setup(Model $model, $config = array()) { $this->settings[$model->alias] = Hash::normalize($config); }
/** * Setup workers and their callbacks for Gearman * * @return void */ protected function _setupWorkers() { $worker = $this->_worker(); foreach (Hash::normalize($this->tasks) as $t => $conf) { list($plugin, $class) = pluginSplit($t); if (!method_exists($this->{$class}, 'workerMethods')) { continue; } if (method_exists($this->{$class}, 'startup')) { $this->{$class}->startup(); } $methods = $this->{$class}->workerMethods(); foreach ($methods as $name => $method) { $name = Nodes\Environment::getProjectName() . '_' . $name; $callback = $this->_addWorkerFunction($class, $method); $worker->addFunction($name, $callback); $this->log("Registered function for {$name}", 'info'); } } }
/** * Merges this objects $property with the property in $class' definition. * This classes value for the property will be merged on top of $class' * * This provides some of the DRY magic CakePHP provides. If you want to shut it off, redefine * this method as an empty function. * * @param array $properties The name of the properties to merge. * @param string $class The class to merge the property with. * @param bool $normalize Set to true to run the properties through Hash::normalize() before merging. * @return void */ protected function _mergeVars($properties, $class, $normalize = true) { $classProperties = get_class_vars($class); foreach ($properties as $var) { if (isset($classProperties[$var]) && !empty($classProperties[$var]) && is_array($this->{$var}) && $this->{$var} != $classProperties[$var]) { if ($normalize) { $classProperties[$var] = Hash::normalize($classProperties[$var]); $this->{$var} = Hash::normalize($this->{$var}); } $this->{$var} = Hash::merge($classProperties[$var], $this->{$var}); } } }
/** * Replace by formatted time string. * * Examples: * - [time]...[/time] * - [time format="m-d-Y"]...[/time] * - [time method="niceShort"]..[/time] * * @param string $str String to check and modify. * @return string Modified string. */ protected function _replaceTime($str) { if (!preg_match_all('/\\[time(.*?)\\](.*)\\[\\/time\\]/i', $str, $matches)) { // Fallback regex for when no options are passed. if (!preg_match_all('/\\[time(.*?)\\](.[^\\[]*)\\[\\/time\\]/i', $str, $matches)) { return $str; } } foreach ($matches[0] as $i => $find) { $opts = $this->_extractAttributes(trim($matches[1][$i])); foreach (Hash::normalize(array('method' => 'nice', 'format', 'timezone')) as $k => $v) { ${$k} = $v; if (isset($opts[$k])) { ${$k} = $opts[$k]; unset($opts[$k]); } } App::uses('CakeTime', 'Utility'); $replace = empty($matches[2][$i]) ? '' : CakeTime::$method($matches[2][$i], $timezone, $format); $str = str_replace($find, $replace, $str); } return $str; }
/** * test merging vars * * @return void */ public function testMergeVars() { $this->Shell->tasks = array('DbConfig' => array('one', 'two')); $this->Shell->uses = array('Posts'); $this->Shell->mergeVars(array('tasks'), 'TestMergeShell'); $this->Shell->mergeVars(array('uses'), 'TestMergeShell', false); $expected = array('DbConfig' => null, 'Fixture' => null, 'DbConfig' => array('one', 'two')); $this->assertEquals($expected, $this->Shell->tasks); $expected = array('Fixture' => null, 'DbConfig' => array('one', 'two')); $this->assertEquals($expected, Hash::normalize($this->Shell->tasks), 'Normalized results are wrong.'); $this->assertEquals(array('Comment', 'Posts'), $this->Shell->uses, 'Merged models are wrong.'); }
/** * Checks if the current page matches the passed `$url`. * * @param string $url Regular expression or internal URL (full or partial) to check against. * @param boolean $exact Optional. Set to false if using regular expression or partial URL. * @return boolean True on success. */ private static function __isPageCurrent($url, $exact = true) { if (!$exact) { return preg_match('/' . preg_quote($url) . '/', Router::url()); } $params = Router::getParams(); $defaultRoute = Hash::normalize(array('plugin', 'controller', 'action', 'prefix', 'named', 'pass', 'lang')); if (!empty($params['prefix'])) { $defaultRoute[$params['prefix']] = null; } return Router::normalize(array_intersect_recursive($params, $defaultRoute)) == $url; }
/** * Loads the configured authentication objects. * * @return mixed either null on empty authenticate value, or an array of loaded objects. * @throws CakeException */ public function constructAuthenticate() { if (empty($this->authenticate)) { return; } $this->_authenticateObjects = array(); $config = Hash::normalize((array) $this->authenticate); $global = array(); if (isset($config[AuthComponent::ALL])) { $global = $config[AuthComponent::ALL]; unset($config[AuthComponent::ALL]); } foreach ($config as $class => $settings) { if (!empty($settings['className'])) { $class = $settings['className']; unset($settings['className']); } list($plugin, $class) = pluginSplit($class, true); $className = $class . 'Authenticate'; App::uses($className, $plugin . 'Controller/Component/Auth'); if (!class_exists($className)) { throw new CakeException(__d('cake_dev', 'Authentication adapter "%s" was not found.', $class)); } if (!method_exists($className, 'authenticate')) { throw new CakeException(__d('cake_dev', 'Authentication objects must implement an %s method.', 'authenticate()')); } $settings = array_merge($global, (array) $settings); $auth = new $className($this->_Collection, $settings); $this->_Collection->getController()->getEventManager()->attach($auth); $this->_authenticateObjects[] = $auth; } return $this->_authenticateObjects; }
/** * Get Unique Conditions * * @since 1.0 * @param Model $Model * @param string $model Model name. * @param integer $foreign_id Foreign Id. * @param array $data Data array. * @return array */ public function getUniqueConditions(Model $Model, $model = null, $foreign_id = null, $data = null) { if (!$model || !$foreign_id || !$data) { return false; } if (!$this->hasUniqueID($Model, $model)) { return false; } $uniqueConditions = array(); $uniqueID = Hash::normalize($Model->uniqueID[$model]); // Build Unique Conditions Based on Data foreach ($uniqueID as $uniqueField => $uniqueValue) { if ($uniqueValue === 'foreign_id') { $uniqueValue = $foreign_id; } elseif (in_array($uniqueField, array_keys($data))) { $uniqueValue = $data[$uniqueField]; } else { // Unique Field Not in Data return false; } $uniqueConditions[$uniqueField] = $uniqueValue; } return $uniqueConditions; }