static function safeWrite($file, $content, $perm = 0664) { lmb_assert_type($file, 'string', 'File name must be a string'); lmb_assert_true($file, 'File name must be a string'); self::mkdir(dirname($file)); $tmp = self::generateTmpFile('_'); $fh = fopen($tmp, 'w'); if ($fh === false) { @unlink($tmp); throw new lmbFsException('could not open file for writing', array('file' => $file)); } //just for safety @flock($fh, LOCK_EX); fwrite($fh, $content); @flock($fh, LOCK_UN); fclose($fh); if (lmbSys::isWin32() && file_exists($file)) { @unlink($file); } if (!@rename($tmp, $file)) { @unlink($tmp); throw new lmbFsException('could not move file', array('src' => $tmp, 'file' => $file)); } @chmod($file, $perm); if (file_exists($tmp)) { @unlink($tmp); } }
protected function _log($message, $params, $level) { lmb_assert_type($message, 'string'); lmb_assert_type($params, 'array'); lmb_assert_type($level, 'integer'); $this->log->log($message, $level, $params); }
function add($object) { lmb_assert_type($object, 'lmbActiveRecord'); $property = $object->mapFieldToProperty($this->relation_info['field']); $object->set($property, $this->owner); parent::add($object); }
function check($value) { try { lmb_assert_type($value, $this->type); } catch (lmbInvalidArgumentException $e) { $this->error($this->custom_error); } }
function log($message, $level = LOG_INFO, $params = array(), lmbBacktrace $backtrace = null, $entry_title = null) { lmb_assert_type($level, 'integer'); if (!$backtrace) { lmb_assert_array_with_key($this->backtrace_depth, $level); $backtrace = new lmbBacktrace($this->backtrace_depth[$level]); } $this->_write($level, $message, $params, $backtrace, $entry_title); }
function testAssertType_CustomMessage() { $message = uniqid('lmb_assert_type'); try { lmb_assert_type(true, 'string', $message); } catch (lmbInvalidArgumentException $e) { $this->assertPattern('/' . $message . '/', $e->getMessage()); } }
protected function _isTagsValid($tags) { lmb_assert_type($tags, 'array'); $tags_versions = (array) $this->wrapped_cache->get(array_keys($tags)); foreach ($tags_versions as $tag_key => $tag_version) { if (is_null($tag_version) || $tags[$tag_key] != $tag_version) { return false; } } return true; }
/** * @param string $name * @return lmbLog */ function getLogFromConf($name) { lmb_assert_true($name); lmb_assert_type($name, 'string'); if ($this->toolkit->hasConf('log')) { $conf = $this->toolkit->getConf('log'); $log = $this->toolkit->createLog($conf['logs'][$name]); } else { $log = $this->toolkit->getDefaultLog(); } return $log; }
/** * Imports magically data into object using relation info. This method is magic because it allows to * import scalar data into objects. E.g: * <code> * //provided Book has Author many-to-one relation as 'author' property * $book = new Book(); * //will try load Author with id = 2 * $book->import(array('title' => 'Alice in wonderand', * 'author' => 2)); * //should print '2' * echo $book->getAuthor()->getId(); * </code> * @param array|object */ function import($source) { lmb_assert_type($source, 'array'); if (is_object($source)) { if ($source instanceof lmbActiveRecord) { $this->importRaw($source->exportRaw()); $this->setIsNew($source->isNew()); } else { $this->import($source->export()); } return; } foreach ($source as $property => $value) { if (isset($this->_composed_of[$property])) { $this->_importAggregatedObject($property, $value); } elseif (isset($this->_has_many[$property])) { $this->_importCollection($property, $value, $this->_has_many[$property]['class']); } elseif (isset($this->_has_many_to_many[$property])) { $this->_importCollection($property, $value, $this->_has_many_to_many[$property]['class']); } elseif (isset($this->_belongs_to[$property])) { $this->_importEntity($property, $value, $this->_belongs_to[$property]['class']); } elseif (isset($this->_many_belongs_to[$property])) { $this->_importEntity($property, $value, $this->_many_belongs_to[$property]['class']); } elseif (isset($this->_has_one[$property])) { $this->_importEntity($property, $value, $this->_has_one[$property]['class']); } elseif ($this->_canImportProperty($property)) { $this->set($property, $value); } } $this->_onAfterImport(); }
function paginate($offset, $limit) { lmb_assert_type($offset, 'integer'); lmb_assert_type($limit, 'integer'); $this->iteratedDataset = null; $this->offset = $offset; $this->limit = $limit; return $this; }
function at($pos) { $this->_ensureDataset(); lmb_assert_type($pos, 'integer'); return $this->dataset->at($pos); }
static function &sortArray($array, $sort_params, $preserve_keys = true) { lmb_assert_type($array, 'array'); lmb_assert_type($sort_params, 'array'); if (empty($array)) { return $array; } $array_mod = array(); foreach ($array as $key => $value) { $array_mod['_' . $key] = $value; } $i = 0; $multi_sort_line = "return array_multisort( "; foreach ($sort_params as $name => $sort_type) { $i++; foreach ($array_mod as $row_key => $row) { if (is_object($row)) { $sort_values[$i][] = $row->get($name); } else { $sort_values[$i][] = $row[$name]; } } if ($sort_type == 'DESC') { $sort_args[$i] = SORT_DESC; } else { $sort_args[$i] = SORT_ASC; } $multi_sort_line .= '$sort_values[' . $i . '], $sort_args[' . $i . '], '; } $multi_sort_line .= '$array_mod );'; eval($multi_sort_line); $array = array(); foreach ($array_mod as $key => $value) { if ($preserve_keys) { $array[substr($key, 1)] = $value; } else { $array[] = $value; } } return $array; }