Ejemplo n.º 1
0
 /**
  * @param $name
  * @return object
  */
 protected static function _get($name)
 {
     Assert(static::$resources)->keyExists($name, "The object {$name} does not exist in the registry.");
     if (is_object(static::$resources[$name]) && static::$resources[$name] instanceof \Closure) {
         static::$resources[$name] = static::$resources[$name]->__invoke();
     }
     return static::$resources[$name];
 }
Ejemplo n.º 2
0
 /**
  * @param bool $ignore
  *
  * @return $this
  */
 public function ignoreErrors($ignore = true)
 {
     Assert($ignore)->boolean();
     $this->ignoreErrors = $ignore;
     return $this;
 }
Ejemplo n.º 3
0
 /**
  * @param string $cache
  * @returns CacheInterface
  * @throws \Exception
  */
 public function getCache($cache = 'default')
 {
     Assert($this->caches)->keyExists($cache, "Could not load cache pool by name ({$cache})");
     return $this->caches[$cache];
 }
Ejemplo n.º 4
0
 /**
  * @param null|int $ttl
  * @return null
  */
 protected function _getTtl($ttl)
 {
     $ttl = !is_null($ttl) ? $ttl : $this->defaultTtl;
     Assert($ttl)->int()->range(1, 315360000);
     // Max 10 years..
     return $ttl;
 }
Ejemplo n.º 5
0
 /**
  * @param string $query
  * @param integer $limit
  * @param null|integer $offset
  * @return string
  */
 public function setLimit($query, $limit = null, $offset = null)
 {
     Assert($query)->string()->notEmpty();
     Assert($limit)->nullOr()->integer();
     Assert($offset)->nullOr()->integer();
     if ($offset) {
         $limit = is_null($limit) ? 1 : $limit;
         $limit = $limit + $offset;
         return "SELECT * FROM ({$query}) WHERE ROWNUM between {$offset} AND {$limit}";
     }
     if ($limit) {
         return "SELECT * FROM ({$query}) WHERE ROWNUM <= {$limit}";
     }
     return $query;
 }
Ejemplo n.º 6
0
<?php

require_once __DIR__ . '/../../../../vendor/autoload.php';
use function Terah\Assert\Assert;
use Assert as Beberlei;
$assert = new Assert('');
Assert::that('');
Beberlei\that('');
echo "Benchmarking static Terah\\Assert";
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
    Assert::that(true)->true();
}
$time = microtime(true) - $start;
echo "Taken: {$time}" . PHP_EOL;
echo "Benchmarking new Terah\\Assert";
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
    Assert(true)->true();
}
$time = microtime(true) - $start;
echo "Taken: {$time}" . PHP_EOL;
echo "Benchmarking fluent Beberlei";
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
    Beberlei\that(true)->true();
}
$time = microtime(true) - $start;
echo "Taken: {$time}" . PHP_EOL;
Ejemplo n.º 7
0
 /**
  * @param string $query
  * @param integer $limit
  * @param null|integer $offset
  * @return string
  */
 public function setLimit($query, $limit = null, $offset = null)
 {
     Assert($query)->string()->notEmpty();
     Assert($limit)->nullOr()->integer();
     Assert($offset)->nullOr()->integer();
     if ($limit) {
         $query .= " LIMIT {$limit}";
     }
     if ($offset) {
         $query .= " OFFSET {$offset}";
     }
     return $query;
 }
Ejemplo n.º 8
0
 /**
  * @param string $field
  * @param mixed $value
  * @param bool|false $permissive
  * @return float|int|null|string
  */
 protected function _fixType($field, $value, $permissive = false)
 {
     Assert($value)->nullOr()->scalar("var is type of " . gettype($value));
     if (empty($this->_schema) || !array_key_exists($field, $this->_schema) && $permissive) {
         return $value;
     }
     $columns = $this->getColumns(false);
     Assert($columns)->keyExists($field, "The property {$field} does not exist.");
     $field_type = !empty($columns[$field]) ? $columns[$field] : null;
     if (is_null($value)) {
         return null;
     }
     // return on null, '' but not 0
     if (!is_numeric($value) && empty($value)) {
         return null;
     }
     // Don't cast invalid values... only those that can be cast cleanly
     switch ($field_type) {
         case 'varchar':
         case 'text':
         case 'date':
         case 'datetime':
         case 'timestamp':
             return (string) $value;
         case 'int':
         case 'tinyint':
             return !is_numeric($value) ? $value : (int) $value;
         case 'decimal':
             return !is_numeric($value) ? $value : (double) $value;
         default:
             return $value;
     }
 }
Ejemplo n.º 9
0
 /**
  * @param array $keys
  * @return array
  */
 public function getLoggingValues($keys = [])
 {
     Assert($keys)->isArray();
     $logables = $this->getFieldsByMetaGroup(self::LOGABLE);
     $logables = array_merge($logables, $keys);
     $logables = array_unique($logables);
     $data = [];
     foreach ($logables as $key) {
         $data[$key] = $this->get($key);
     }
     return $data;
 }
Ejemplo n.º 10
0
 /**
  * @param string $name
  * @return AbstractPdo
  */
 public function getConnection($name)
 {
     Assert($this->_connections)->keyExists($name, "The database connection {$name} has not been added.");
     if ($this->_connections[$name] instanceof \Closure) {
         $this->_connections[$name] = $this->_connections[$name]->__invoke();
     }
     return $this->_connections[$name];
 }
Ejemplo n.º 11
0
 /**
  * @param $data
  */
 public function append($data)
 {
     Assert($data)->isInstanceOf($this->entityType);
     //$this->data[] = $data;
     parent::append($data);
 }
Ejemplo n.º 12
0
 /**
  * @param string $name
  * @return mixed
  * @throws \Terah\Assert\AssertionFailedException
  */
 public function get($name)
 {
     Assert($this->_meta_data)->keyExists($name, "Invalid property ({$name}) sent to response meta");
     return $this->_meta_data[$name];
 }
Ejemplo n.º 13
0
 /**
  * @return string
  * @throws \Exception
  */
 public function toString()
 {
     $this->generate();
     $tempFile = $this->getTempFullPath();
     $fileData = file_get_contents($tempFile);
     Assert($fileData)->notEmpty("The generated pdf file is empty");
     return $fileData;
 }