Exemple #1
0
 /**
  * Parses the generic configuration.
  * @param String $content
  * @return Array
  */
 protected function _parseContent($content)
 {
     $output = array();
     $matches = array();
     $paramsString = implode('|', $this->_deployParams);
     $pattern = '/:?(?P<paramName>' . $paramsString . ')[,:]? [\'"](?P<paramValue>[^\'"]*)[\'"]/';
     if (!preg_match_all($pattern, $content, $matches)) {
         throw new Exception("Could not extract deploy parameters from " . self::GENERIC_CONFIG_PATH);
     }
     foreach ($this->_deployParams as $p) {
         $indices = array_keys(array_filter($matches['paramName'], function ($pn) use($p) {
             return $pn === $p;
         }));
         if (!count($indices)) {
             continue;
         }
         $output[$p] = array_values(array_get_subset($matches['paramValue'], $indices));
         // For now: only treat the server param as array (since it's common for it to be an
         // array, in the case of a multi-server setup)
         if ($p !== 'server') {
             $output[$p] = $output[$p][0];
         }
     }
     // explode server into user and server parts
     if (!empty($output['server'])) {
         $output['server'] = array_map(function ($serverConfig) {
             $bits = explode('@', $serverConfig, 2);
             return array('user' => $bits[0], 'server' => $bits[1]);
         }, $output['server']);
     }
     return $output;
 }
Exemple #2
0
 /** @test */
 public function should_respect_default_values()
 {
     $fieldConfig = $this->_getFieldConfig();
     $defaults = array('created' => '1975-04-11 12:40:12', 'is_featured' => 0, 'name' => 'Henk');
     $faker = new Garp_Model_Db_Faker();
     $fakeRow = $faker->createFakeRow($fieldConfig, $defaults);
     $subset = array_get_subset($fakeRow, array_keys($defaults));
     $this->assertEquals($defaults, $subset);
 }
Exemple #3
0
 protected function _getForeignKeyWhereClause(Garp_Db_Table_Row $record, $model, $foreignKeyColumns)
 {
     $whereData = array_get_subset($record->toArray(), $foreignKeyColumns);
     // Assume one "id" primary key
     if (count($foreignKeyColumns) > 1) {
         throw new Exception("Can't deal with multiple foreign keys right now!");
     }
     $whereData = array_combine(array('id'), array_values($whereData));
     return $model->arrayToWhereClause($whereData);
 }
Exemple #4
0
 /**
  * Validate wether the given columns are long enough
  *
  * @param array $data The data to validate
  * @param Garp_Model_Db $model
  * @param bool $onlyIfAvailable Wether to skip validation on fields that are not in the array
  * @return void
  * @throws Garp_Model_Validator_Exception
  */
 public function validate(array $data, Garp_Model_Db $model, $onlyIfAvailable = true)
 {
     $theFields = $this->_fields;
     $applicableFields = array_keys(array_get_subset($data, array_keys($theFields)));
     $tooShortFields = array_filter($applicableFields, function ($field) use($theFields, $data) {
         return !is_null($data[$field]) && strlen($data[$field]) < $theFields[$field];
     });
     if (count($tooShortFields)) {
         $first = current($tooShortFields);
         throw new Garp_Model_Validator_Exception(Garp_Util_String::interpolate(__(self::ERROR_MESSAGE), array('value' => $first, 'min' => $theFields[$first])));
     }
 }
Exemple #5
0
 /**
  * Remove cruft from HTTP params and provide sensible defaults.
  *
  * @param array $params The combined URL parameters
  * @return array
  */
 protected function _extractOptionsForFetch(array $params)
 {
     if (!isset($params['options'])) {
         $params['options'] = array();
     }
     try {
         $options = is_string($params['options']) ? Zend_Json::decode(urldecode($params['options'])) : $params['options'];
     } catch (Zend_Json_Exception $e) {
         throw new Garp_Content_Api_Rest_Exception(sprintf(self::EXCEPTION_INVALID_JSON, $e->getMessage()));
     }
     $options = array_get_subset($options, array('sort', 'start', 'limit', 'fields', 'query', 'group', 'with'));
     if (!isset($options['limit'])) {
         $options['limit'] = self::DEFAULT_PAGE_LIMIT;
     }
     if (isset($options['with'])) {
         // Normalize into an array
         $options['with'] = (array) $options['with'];
     }
     return $options;
 }