Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for DatabaseSource new binding parsing and select expressions #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Charcoal/Property/PropertyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,21 @@ class PropertyField
*/
private $extra;

/**
* @var mixed
*/
private $selectExpressions;

/**
* @var mixed
*/
private $val;

/**
* @var \Closure
*/
private $parseBinding;

/**
* @var mixed
*/
Expand Down Expand Up @@ -86,9 +96,15 @@ public function setData(array $data)
if (isset($data['extra'])) {
$this->setExtra($data['extra']);
}
if (isset($data['selectExpressions'])) {
$this->setSelectExpressions($data['selectExpressions']);
}
if (isset($data['val'])) {
$this->setVal($data['val']);
}
if (isset($data['parseBinding'])) {
$this->setParseBinding($data['parseBinding']);
}
if (isset($data['defaultVal'])) {
$this->setDefaultVal($data['defaultVal']);
}
Expand Down Expand Up @@ -247,6 +263,25 @@ public function sqlEncoding()
return $this->sqlEncoding;
}

/**
* @return array
*/
public function selectExpressions()
{
return $this->selectExpressions;
}

/**
* @param mixed $selectExpressions SqlSelectExpressions for PropertyField.
* @return self
*/
public function setSelectExpressions($selectExpressions)
{
$this->selectExpressions = $selectExpressions;

return $this;
}

/**
* @param mixed $val The field value.
* @return PropertyField Chainable
Expand All @@ -265,6 +300,25 @@ public function val()
return $this->val;
}

/**
* @return \Closure
*/
public function parseBinding()
{
return $this->parseBinding;
}

/**
* @param \Closure $parseBinding ParseBinding for PropertyField.
* @return self
*/
public function setParseBinding($parseBinding)
{
$this->parseBinding = $parseBinding;

return $this;
}

/**
* @param mixed $defaultVal The default field value.
* @return PropertyField Chainable
Expand Down
53 changes: 45 additions & 8 deletions src/Charcoal/Property/StorablePropertyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ public function fieldNames()
return $this->fieldNames;
}

/**
* Overridable to allow for custom Field Name parsing.
*
* This allows to add additional functions to a select statement that looks like this :
* 'MySQL_FUNCTION(select)'
*
* @param string $key The property field key.
* @param mixed $fieldName The raw filed name.
* @return mixed
*/
protected function fieldExpression($key, $fieldName)
{
unset($key);

return $fieldName;
}

/**
* Retrieve the property's value in a format suitable for the given field key.
*
Expand Down Expand Up @@ -190,6 +207,24 @@ public function storageVal($val)
return $val;
}

/**
* Overridable by property controller to add a custom parsing for the binding identifier.
*
* This allows to add additional functions to an expression to generate a binding statement that looks like this :
* 'MySQL_FUNCTION(:bind)'
*
* @return \Closure
*/
protected function parseBinding()
{
/**
* @param string $bind The PDO bind string.
*/
return function ($bind) {
return $bind;
};
}

/**
* Parse the property's value (from a flattened structure)
* in a format suitable for models.
Expand Down Expand Up @@ -254,14 +289,16 @@ protected function generateFields($val = null)
$fieldNames = $this->fieldNames();
foreach ($fieldNames as $fieldKey => $fieldName) {
$field = $this->createPropertyField([
'ident' => $fieldName,
'sqlType' => $this->sqlType(),
'sqlPdoType' => $this->sqlPdoType(),
'sqlEncoding' => $this->sqlEncoding(),
'extra' => $this->sqlExtra(),
'val' => $this->fieldValue($fieldKey, $val),
'defaultVal' => $this->sqlDefaultVal(),
'allowNull' => $this['allowNull'],
'ident' => $fieldName,
'sqlType' => $this->sqlType(),
'sqlPdoType' => $this->sqlPdoType(),
'sqlEncoding' => $this->sqlEncoding(),
'extra' => $this->sqlExtra(),
'selectExpressions' => $this->fieldExpression($fieldKey, $fieldName),
'val' => $this->fieldValue($fieldKey, $val),
'parseBinding' => $this->parseBinding(),
'defaultVal' => $this->sqlDefaultVal(),
'allowNull' => $this['allowNull'],
]);

$fields[$fieldKey] = $field;
Expand Down