Created
March 4, 2015 08:46
-
-
Save zavodnoyapl1992/6c526bd4d92a3dc00ee0 to your computer and use it in GitHub Desktop.
Mongo ActiveRecord
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
<?php | |
namespace Library\Mongo; | |
abstract class AbstractEntity | |
{ | |
private $fields = array(); | |
private $_id; | |
/** | |
* @var $mongoCollection /MongoCollection | |
*/ | |
private $mongoCollection; | |
abstract public function getCollectionName(); | |
public function __construct(\MongoDB $mongo) | |
{ | |
$this->mongoCollection = $mongo->selectCollection($this->getCollectionName()); | |
} | |
public function getCollection() | |
{ | |
return $this->mongoCollection; | |
} | |
public function persist() | |
{ | |
$class = new \ReflectionClass(get_called_class()); | |
$propList = $class->getProperties(); | |
foreach ($propList as $prop) { | |
$methodName = 'get'. mb_strtoupper(substr($prop->getName(), 0, 1)) . substr($prop->getName(), 1); | |
if (!method_exists($this, $methodName)) { | |
throw new \BadMethodCallException("Not found method $methodName in " . get_called_class()); | |
} | |
$this->fields[$this->convertToUnderscore(trim($prop->getName(), '_'))] = $this->$methodName(); | |
} | |
} | |
public function insert() | |
{ | |
if (empty($this->fields)) { | |
throw new \ErrorException('Data can not be NULL'); | |
} | |
$this->mongoCollection->insert($this->fields); | |
} | |
public function update() | |
{ | |
if (empty($this->fields)) { | |
throw new \ErrorException('Data can not be NULL'); | |
} | |
if (!$this->_id) { | |
throw new \ErrorException('You can update only this item, but _id is NULL'); | |
} | |
$this->mongoCollection->update(array('_id' => $this->_id), $this->fields); | |
} | |
public function upsert() | |
{ | |
if (empty($this->fields)) { | |
throw new \ErrorException('Data can not be NULL'); | |
} | |
$this->mongoCollection->update(array('_id' => $this->_id), $this->fields); | |
} | |
public function filling($item) | |
{ | |
if (!$item) { | |
return; | |
} | |
foreach ($item as $key => $value) { | |
$key = $this->convertToCamelCase(trim($key, '_')); | |
$methodName = 'set'. mb_strtoupper(substr($key, 0, 1)) . substr($key, 1); | |
if (!method_exists($this, $methodName)) { | |
throw new \BadMethodCallException("Not found method $methodName in " . get_called_class()); | |
} | |
$this->$methodName($value); | |
} | |
} | |
public function delete() | |
{ | |
if (!$this->_id) { | |
throw new \ErrorException('You can remove only this item, but _id is NULL'); | |
} | |
$this->getCollection()->remove(array('_id' => $this->_id)); | |
$this->fields = array(); | |
} | |
/** | |
* @return \MongoId | |
*/ | |
public function getId() | |
{ | |
return $this->_id; | |
} | |
/** | |
* @param \MongoId $id | |
*/ | |
public function setId($id) | |
{ | |
$this->_id = $id; | |
} | |
private function convertToUnderscore($input) { | |
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches); | |
$ret = $matches[0]; | |
foreach ($ret as &$match) { | |
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match); | |
} | |
return implode('_', $ret); | |
} | |
private function convertToCamelCase($string) | |
{ | |
$str = str_replace(' ', '', ucwords(str_replace('_', ' ', $string))); | |
$str[0] = strtolower($str[0]); | |
return $str; | |
} | |
} |
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
<?php | |
namespace Library\Mongo; | |
class LandingRedirect extends AbstractEntity | |
{ | |
const COLLECTION = 'landing_redirect'; | |
protected $channel; | |
protected $urlIn; | |
protected $urlOut; | |
protected $project; | |
protected $operator; | |
protected $partner; | |
protected $subscribe; | |
protected $date; | |
/** | |
* @return mixed | |
*/ | |
public function getChannel() | |
{ | |
return $this->channel; | |
} | |
/** | |
* @param mixed $channel | |
*/ | |
public function setChannel($channel) | |
{ | |
$this->channel = $channel; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getUrlIn() | |
{ | |
return $this->urlIn; | |
} | |
/** | |
* @param mixed $urlIn | |
*/ | |
public function setUrlIn($urlIn) | |
{ | |
$this->urlIn = $urlIn; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getUrlOut() | |
{ | |
return $this->urlOut; | |
} | |
/** | |
* @param mixed $urlOut | |
*/ | |
public function setUrlOut($urlOut) | |
{ | |
$this->urlOut = $urlOut; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getProject() | |
{ | |
return $this->project; | |
} | |
/** | |
* @param mixed $project | |
*/ | |
public function setProject($project) | |
{ | |
$this->project = $project; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getOperator() | |
{ | |
return $this->operator; | |
} | |
/** | |
* @param mixed $operator | |
*/ | |
public function setOperator($operator) | |
{ | |
$this->operator = $operator; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getPartner() | |
{ | |
return $this->partner; | |
} | |
/** | |
* @param mixed $partner | |
*/ | |
public function setPartner($partner) | |
{ | |
$this->partner = $partner; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getSubscribe() | |
{ | |
return $this->subscribe; | |
} | |
/** | |
* @param mixed $subscribe | |
*/ | |
public function setSubscribe($subscribe) | |
{ | |
$this->subscribe = $subscribe; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getDate() | |
{ | |
return $this->date; | |
} | |
/** | |
* @param mixed $date | |
*/ | |
public function setDate($date) | |
{ | |
$this->date = $date; | |
} | |
public function getCollectionName() | |
{ | |
return self::COLLECTION; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment