Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
r1k0
committed
Apr 20, 2016
1 parent
aca30b7
commit fbb9218
Showing
10 changed files
with
627 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Fazb\RedBean\Helper; | ||
|
||
use RedBeanPHP\OODBBean; | ||
|
||
interface HelperInterface | ||
{ | ||
/** | ||
* Apply behavior | ||
* | ||
* @param OODBBean $bean | ||
* @param string $eventName | ||
*/ | ||
public function apply(OODBBean $bean, $eventName, array $options); | ||
|
||
/** | ||
* Sets options for a model to apply to | ||
* | ||
* @param array $options | ||
*/ | ||
public function configure($options = array()); | ||
|
||
/** | ||
* Returns a list of events name | ||
* | ||
* @return array | ||
*/ | ||
public function getEventsName(); | ||
|
||
/** | ||
* Returns a name | ||
* | ||
* @return string | ||
*/ | ||
public function getName(); | ||
|
||
/** | ||
* Validate configuration and/or set defaults | ||
* | ||
* @param array $configuration | ||
* @return array $configuration | ||
*/ | ||
public function validate(array $configuration); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace Fazb\RedBean\Helper; | ||
|
||
use Fazb\RedBean\Helper\HelperInterface; | ||
use RedBeanPHP\OODBBean; | ||
|
||
/** | ||
* Sluggable | ||
*/ | ||
class Sluggable implements HelperInterface | ||
{ | ||
protected $defaults = array( | ||
'events' => array( | ||
'update' => array() | ||
), | ||
'options' => array( | ||
'fieldName' => 'slug' | ||
) | ||
); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure($defaults = array()) | ||
{ | ||
$this->defaults = array_merge( | ||
$this->defaults, | ||
$defaults | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function apply(OODBBean $bean, $eventName, array $options) | ||
{ | ||
$fields = $options['events'][$eventName]; | ||
$parts = array(); | ||
foreach ($fields as $name) { | ||
$parts[] = $bean[$name]; | ||
} | ||
|
||
$fieldName = $options['options']['fieldName']; | ||
|
||
if ($parts) { | ||
$bean->setMeta(sprintf('cast.%s', $fieldName), 'string'); | ||
$bean->$fieldName = $this->sluggify(join(' ', $parts)); | ||
} | ||
} | ||
|
||
/* | ||
* Sluggify | ||
* | ||
* @param string | ||
* | ||
* @return string | ||
*/ | ||
public function sluggify($text) | ||
{ | ||
// replace non letter or digits by - | ||
$text = preg_replace('~[^\\pL\d]+~u', '-', $text); | ||
// trim | ||
$text = trim($text, '-'); | ||
// transliterate | ||
if (function_exists('iconv')) { | ||
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); | ||
} | ||
// lowercase | ||
$text = strtolower($text); | ||
// remove unwanted characters | ||
$text = preg_replace('~[^-\w]+~', '', $text); | ||
if (empty($text)) { | ||
return 'n-a'; | ||
} | ||
|
||
return $text; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getEventsName() | ||
{ | ||
return array_keys($this->defaults['events']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'sluggable'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate(array $configuration) | ||
{ | ||
if (!isset($configuration['events'])) { | ||
throw new \RuntimeException(sprintf('You must specify an "events" key, with the targetted fields for the "%s" helper', $this->getName())); | ||
} | ||
|
||
if (!isset($configuration['options']['fieldName'])) { | ||
$configuration['options']['fieldName'] = $this->defaults['options']['fieldName']; | ||
} | ||
|
||
return $configuration; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Fazb\RedBean\Helper; | ||
|
||
use Fazb\RedBean\Helper\HelperInterface; | ||
use RedBeanPHP\OODBBean; | ||
|
||
/** | ||
* Timestampable | ||
*/ | ||
class Timestampable implements HelperInterface | ||
{ | ||
protected $defaults = array( | ||
'events' => array( | ||
'dispense' => array('created_at'), | ||
'update' => array('updated_at') | ||
), | ||
'options' => array( | ||
'dateFormat' => 'Y-m-d H:i:s' | ||
) | ||
); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure($defaults = array()) | ||
{ | ||
$this->defaults = array_merge( | ||
$this->defaults, | ||
$defaults | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function apply(OODBBean $bean, $eventName, array $options) | ||
{ | ||
$fields = $options['events'][$eventName]; | ||
$dateFormat = $options['options']['dateFormat']; | ||
|
||
foreach ($fields as $name) { | ||
$bean->$name = (new \DateTime())->format($dateFormat); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getEventsName() | ||
{ | ||
return array_keys($this->defaults['events']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'timestampable'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function validate(array $configuration) | ||
{ | ||
if (!isset($configuration['events'])) { | ||
$configuration['events'] = $this->defaults['events']; | ||
} | ||
|
||
if (!isset($configuration['options']['dateFormat'])) { | ||
$configuration['options']['dateFormat'] = $this->defaults['options']['dateFormat']; | ||
} | ||
|
||
return $configuration; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
|
||
namespace Fazb\RedBean; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Monolog\Logger as Monolog; | ||
use RedBeanPHP\Logger as BaseLogger; | ||
use RedBeanPHP\Logger\RDefault\Debug; | ||
use RedBeanPHP\RedException; | ||
|
||
/** | ||
* Logger | ||
*/ | ||
class Logger extends Debug implements BaseLogger | ||
{ | ||
protected $logger; | ||
|
||
protected $debugQuery; | ||
|
||
protected $strLen = 40; | ||
|
||
protected $throwException = false; | ||
|
||
public function __construct(LoggerInterface $logger, $throwException = true, $debugQuery = false) | ||
{ | ||
$this->logger = $logger; | ||
$this->throwException = $throwException; | ||
$this->debugQuery = $debugQuery; | ||
$this->mode = self::C_LOGGER_ARRAY; | ||
} | ||
|
||
/** | ||
* Writes a query for logging with all bindings / params filled | ||
* in. | ||
* | ||
* @param string $newSql the query | ||
* @param array $bindings the bindings to process (key-value pairs) | ||
* | ||
* @return string | ||
*/ | ||
private function writeQuery($newSql, $newBindings) | ||
{ | ||
//avoid str_replace collisions: slot1 and slot10 (issue 407). | ||
uksort($newBindings, function ($a, $b) { | ||
return (strlen($b) - strlen($a)); | ||
}); | ||
|
||
$newStr = $newSql; | ||
foreach ($newBindings as $slot => $value) { | ||
if (strpos($slot, ':') === 0) { | ||
$newStr = str_replace($slot, $this->fillInValue($value), $newStr); | ||
} | ||
} | ||
return $newStr; | ||
} | ||
|
||
public function log() | ||
{ | ||
if (func_num_args() < 1) { | ||
return; | ||
}; | ||
|
||
$message = ''; | ||
if ($this->debugQuery) { | ||
$sql = func_get_arg(0); | ||
|
||
if (func_num_args() < 2) { | ||
$bindings = array(); | ||
} else { | ||
$bindings = func_get_arg(1); | ||
} | ||
|
||
if (is_array($bindings)) { | ||
$newSql = $this->normalizeSlots($sql); | ||
$newBindings = $this->normalizeBindings($bindings); | ||
$message = $this->writeQuery($newSql, $newBindings); | ||
} else { | ||
$message = $sql; | ||
} | ||
|
||
if ($message) { | ||
$this->doLog($message); | ||
} | ||
} else { | ||
foreach (func_get_args() as $argument) { | ||
if (is_array($argument)) { | ||
$argument = array_filter($argument); | ||
$message = print_r($argument, true); | ||
$this->doLog($message); | ||
} else { | ||
$this->doLog($argument); | ||
} | ||
} | ||
} | ||
} | ||
|
||
protected function doLog($message) | ||
{ | ||
$level = preg_match('@error@i', $message) ? Monolog::CRITICAL : Monolog::DEBUG; | ||
$this->logger->addRecord($level, $message); | ||
|
||
if ($level === Monolog::CRITICAL && $this->throwException) { | ||
throw new RedException($message); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Fazb\RedBean\Model; | ||
|
||
use RedBeanPHP\SimpleModel; | ||
|
||
/** | ||
* BaseModel | ||
*/ | ||
abstract class BaseModel extends SimpleModel | ||
{ | ||
public function getType() | ||
{ | ||
return $this->bean->getMeta('type'); | ||
} | ||
} |
Oops, something went wrong.