/** * Tests various aspects of a newly constructed field. * * @dataProvider provider_construction */ public function test_construction(Jelly_Field $field, $expected) { // Ensure the following properties have been set foreach ($expected as $key => $value) { // NULL set is the expected value when allow_null is TRUE, skip it if ($key === 'null_set') { continue; } $this->assertSame($field->{$key}, $value, 'Field properties must match'); } // Ensure that null values are handled properly if ($field->allow_null) { $this->assertSame($field->set(NULL), NULL, 'Field must return NULL when given NULL since `allow_null` is TRUE'); } else { $this->assertSame($field->set(NULL), $expected['null_set'], 'Since `allow_null` is FALSE, field must return expected value when given NULL'); } // Ensure convert_empty works if ($field->convert_empty) { // allow_null must be true if convert_empty is TRUE and empty_value is NULL if ($field->empty_value === NULL) { $this->assertTrue($field->allow_null, 'allow_null must be TRUE since convert_empty is TRUE'); } // Test setting a few empty values foreach (array(NULL, FALSE, '', '0', 0) as $value) { $this->assertSame($field->set($value), $field->empty_value); } } }
/** * Displays a selection of models to relate to * * @param string $prefix The prefix to put before the filename to be rendered * @return View **/ public function input($prefix = 'jelly/field', $data = array()) { if (!isset($data['options'])) { $data['options'] = Jelly::select($this->foreign['model'])->execute()->as_array(':primary_key', ':name_key'); } return parent::input($prefix, $data); }
/** * Sets up foreign and through properly. * * @param string $model * @param string $column * @return void */ public function initialize($model, $column) { // Default to the name of the column if (empty($this->foreign)) { $foreign_model = inflector::singular($column); $this->foreign = $foreign_model . '.' . $foreign_model . ':primary_key'; } elseif (is_string($this->foreign) and FALSE === strpos($this->foreign, '.')) { $this->foreign = $this->foreign . '.' . $this->foreign . ':primary_key'; } // Create an array from them for easier access if (!is_array($this->foreign)) { $this->foreign = array_combine(array('model', 'field'), explode('.', $this->foreign)); } // Create the default through connection if (empty($this->through) or is_string($this->through)) { if (empty($this->through)) { // Find the join table based on the two model names pluralized, // sorted alphabetically and with an underscore separating them $through = array(inflector::plural($this->foreign['model']), inflector::plural($model)); sort($through); $this->through = implode('_', $through); } $this->through = array('model' => $this->through, 'fields' => array(inflector::singular($model) . ':foreign_key', inflector::singular($this->foreign['model']) . ':foreign_key')); } parent::initialize($model, $column); }
/** * Sets the default to 0 if we have no format, or an empty string otherwise. * * @param array $options */ public function __construct($options = array()) { parent::__construct($options); if (!isset($options['default']) and !$this->allow_null) { // Having a format implies we're saving a string, so we want a proper default $this->default = $this->format ? '' : 0; } }
/** * Ensures allow_null is not set to FALSE on the field, as it prevents * proper auto-incrementing of a primary key. * * @param array $options */ public function __construct($options = array()) { parent::__construct($options); // Ensure allow_null is TRUE if (!$this->allow_null) { throw new Kohana_Exception(':class cannot have allow_null set to FALSE', array(':class' => get_class($this))); } }
/** * Ensures convert_empty is not set on the field, as it prevents FALSE * from ever being set on the field. * * @param array $options */ public function __construct($options = array()) { parent::__construct($options); // Ensure convert_empty is FALSE if ($this->convert_empty) { throw new Kohana_Exception(':class cannot have convert_empty set to TRUE', array(':class' => get_class($this))); } }
/** * Ensures there is a path for saving set * * @param array $options */ public function __construct($options = array()) { parent::__construct($options); // Ensure we have path to save to if (empty($this->path) or !is_writable($this->path)) { throw new Kohana_Exception(get_class($this) . ' must have a `path` property set that points to a writable directory'); } // Make sure the path has a trailing slash $this->path = rtrim(str_replace('\\', '/', $this->path), '/') . '/'; }
/** * Ensures there is a path for saving set * * @param array $options */ public function __construct($options = array()) { parent::__construct($options); // Normalize the path $this->path = realpath(str_replace('\\', '/', $this->path)); // Ensure we have a trailing slash if (!empty($this->path) and is_writable($this->path)) { $this->path = rtrim($this->path, '/') . '/'; } else { throw new Kohana_Exception(get_class($this) . ' must have a `path` property set that points to a writable directory'); } }
/** * Ensures there is a choices array set * * @param array $options */ public function __construct($options = array()) { parent::__construct($options); // Ensure we have choices to gather values from if (empty($this->choices)) { throw new Kohana_Exception('Field_Enum must have a `choices` property set'); } // Convert non-associative values to associative ones if (!arr::is_assoc($this->choices)) { $this->choices = array_combine($this->choices, $this->choices); } }
/** * Determines the actual foreign model and field that the * relationship is tied to. * * @param string $model * @param string $column * @return void */ public function initialize($model, $column) { parent::initialize($model, $column); // Empty? The model defaults to the the singularized name // of this field, and the field defaults to this field's model's foreign key if (empty($this->foreign)) { $this->foreign = inflector::singular($this->name) . '.' . $model . ':foreign_key'; } elseif (FALSE === strpos($this->foreign, '.')) { $this->foreign = $this->foreign . '.' . $model . ':foreign_key'; } // Create an array fo easier access to the separate parts $this->foreign = array_combine(array('model', 'field'), explode('.', $this->foreign)); }
/** * Automatically sets foreign to sensible defaults. * * @param string $model * @param string $column * @return void */ public function initialize($model, $column) { // Default to the name of the column if (empty($this->foreign)) { $this->foreign = $column . '.:primary_key'; } elseif (FALSE === strpos($this->foreign, '.')) { $this->foreign = $this->foreign . '.:primary_key'; } // Create an array from them $this->foreign = array_combine(array('model', 'field'), explode('.', $this->foreign)); // Default to the foreign model's primary key if (empty($this->column)) { $this->column = $column . '_id'; } // Column is set and won't be overridden parent::initialize($model, $column); }
/** * Adds a rule that uploads the file. * * @param Jelly_Model $model * @param string $column * @return void */ public function initialize($model, $column) { parent::initialize($model, $column); if (count($this->rules) > 0) { // If rules can be found check if the rule for not_empty is set foreach ($this->rules as $rule) { if (is_string($rule[0]) and $rule[0] === 'not_empty') { $this->rules[] = array(array(':field', 'file_not_empty'), array(':validation', ':model', ':field')); } } } if ((bool) $this->types) { $this->rules[] = array(array(':field', 'file_invalid_type'), array(':validation', ':model', ':field', $this->types)); } // Add a rule to save the file when validating $this->rules[] = array(array(':field', '_upload'), array(':validation', ':model', ':field')); }
/** * Ensures there is a path for saving set * * @param array $options */ public function __construct($options = array()) { parent::__construct($options); $this->path = $this->_check_path($this->path); }