Created
December 30, 2014 12:02
-
-
Save ikwattro/56212865f451afb20665 to your computer and use it in GitHub Desktop.
simple entity generation
This file contains hidden or 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 Ikwattro\Play\Model\Base; | |
| use GraphAware\Vertix\Vertix; | |
| use Ikwattro\Play\Model\Base\UserMap; | |
| use Neoxygen\NeoClient\Exception\HttpException; | |
| class BaseUser | |
| { | |
| protected $id; | |
| protected $first_name; | |
| protected $last_name; | |
| protected $birth_date; | |
| protected $active; | |
| protected $modifiedParts = []; | |
| protected $modifiedLabels = []; | |
| protected $new = true; | |
| protected $deleted; | |
| public function getIdKey() | |
| { | |
| return UserMap::KEY_ID; | |
| } | |
| public function hasModifiedParts() | |
| { | |
| if (!empty($this->modifiedParts)) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| public function isModified($p) | |
| { | |
| if (array_key_exists($p, $this->modifiedParts)) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| public function hasModifiedLabels() | |
| { | |
| if (!empty($this->modifiedLabels)) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| public function isLabelModified($l) | |
| { | |
| if (null !== $l && array_key_exists($l, $this->modifiedLabels)) { | |
| return true; | |
| } | |
| return false; | |
| } | |
| public function getId() | |
| { | |
| return $this->id; | |
| } | |
| public function setFirstName($v) | |
| { | |
| if ('' !== $v) { | |
| $v = (string) $v; | |
| if ($this->first_name !== $v) { | |
| $this->first_name = $v; | |
| $this->modifiedParts[UserMap::KEY_FIRST_NAME] = true; | |
| } | |
| } | |
| return $this; | |
| } | |
| public function getFirstName() | |
| { | |
| return $this->first_name; | |
| } | |
| public function setLastName($v) | |
| { | |
| if ('' !== $v) { | |
| $v = (string) $v; | |
| if ($this->last_name !== $v) { | |
| $this->last_name = $v; | |
| $this->modifiedParts[UserMap::KEY_LAST_NAME] = true; | |
| } | |
| } | |
| return $this; | |
| } | |
| public function getLastName() | |
| { | |
| return $this->last_name; | |
| } | |
| public function setBirthDate(\DateTime $v) | |
| { | |
| if ($this->birth_date !== $v) { | |
| $this->birth_date = $v; | |
| $this->modifiedParts['birth_date'] = true; | |
| } | |
| return $this; | |
| } | |
| public function getBirthDate() | |
| { | |
| return $this->birth_date; | |
| } | |
| public function isActive() | |
| { | |
| return (boolean) $this->active; | |
| } | |
| public function setActive($v = true) | |
| { | |
| $v = (boolean) $v; | |
| if ($this->active !== $v) { | |
| $this->active = $v; | |
| $this->modifiedLabels[UserMap::LABEL_ACTIVE] = true; | |
| } | |
| return $this; | |
| } | |
| public function isNew() | |
| { | |
| return $this->new; | |
| } | |
| public function isDeleted() | |
| { | |
| return null !== $this->deleted; | |
| } | |
| public function save($conn = null) | |
| { | |
| if ($this->isDeleted()) { | |
| throw new \RuntimeException('You can not save an object that has been deleted'); | |
| } | |
| if (null === $conn) { | |
| $conn = Vertix::getWriteConnection(); | |
| } | |
| if ($this->isNew()) { | |
| $this->id = Vertix::getNewUuid(); | |
| $this->modifiedParts[UserMap::KEY_ID] = true; | |
| return $this->doInsert($conn); | |
| } | |
| return $this->doUpdate($conn); | |
| } | |
| protected function doInsert($conn = null) | |
| { | |
| $identifier = strtolower(UserMap::INTERNAL_LABEL); | |
| $p = []; | |
| $q = 'CREATE ('.$identifier.':'.UserMap::INTERNAL_LABEL.')'.PHP_EOL; | |
| $q .= 'SET '.$identifier.'.'.$this->getIdKey().' = {parts}.'.$this->getIdKey().PHP_EOL; | |
| $p[UserMap::KEY_ID] = (string) $this->getId(); | |
| foreach (UserMap::getDefaultLabels() as $label) { | |
| $q .= 'SET '.$identifier.' :'.ucfirst($label).PHP_EOL; | |
| } | |
| if ($this->isLabelModified(UserMap::LABEL_ACTIVE)) { | |
| if ($this->isActive()) { | |
| $q .= 'SET '.$identifier.' :'.ucfirst(UserMap::getCamelNameForKey(UserMap::LABEL_ACTIVE)).PHP_EOL; | |
| } else { | |
| $q .= 'REMOVE '.$identifier.':'.ucfirst(UserMap::getCamelNameForKey(UserMap::LABEL_ACTIVE)).PHP_EOL; | |
| } | |
| } | |
| if ($this->isModified(UserMap::KEY_FIRST_NAME)) { | |
| $q .= 'SET '.$identifier.'.'.UserMap::KEY_FIRST_NAME.' = {parts}.'.UserMap::KEY_FIRST_NAME.PHP_EOL; | |
| $p[UserMap::KEY_FIRST_NAME] = (string) $this->getFirstName(); | |
| } | |
| if ($this->isModified(UserMap::KEY_LAST_NAME)) { | |
| $q .= 'SET '.$identifier.'.'.UserMap::KEY_LAST_NAME.' = {parts}.'.UserMap::KEY_LAST_NAME.PHP_EOL; | |
| $p[UserMap::KEY_LAST_NAME] = (string) $this->getLastName(); | |
| } | |
| try { | |
| $parameters['parts'] = $p; | |
| Vertix::getClient()->sendCypherQuery($q, $parameters, $conn); | |
| $this->new = false; | |
| $this->modifiedLabels = []; | |
| $this->modifiedParts = []; | |
| return $this; | |
| } catch (HttpException $e) { | |
| $this->new = true; | |
| $this->id = null; | |
| unset($this->modifiedParts[UserMap::KEY_ID]); | |
| throw new \Exception($e->getMessage()); | |
| } | |
| } | |
| protected function doUpdate($conn = null) | |
| { | |
| if (empty($this->modifiedLabels) && empty($this->modifiedParts)) { | |
| return $this; | |
| } | |
| $identifier = strtolower(UserMap::INTERNAL_LABEL); | |
| $p = []; | |
| $q = 'MATCH ('.$identifier.':'.UserMap::INTERNAL_LABEL.')'.PHP_EOL; | |
| $q .= 'WHERE '.$identifier.'.'.$this->getIdKey().' = {parts}.'.$this->getIdKey().PHP_EOL; | |
| $p[UserMap::KEY_ID] = (string) $this->getId(); | |
| if ($this->isLabelModified(UserMap::LABEL_ACTIVE)) { | |
| if ($this->isActive()) { | |
| $q .= 'SET '.$identifier.' :'.ucfirst(UserMap::getCamelNameForKey(UserMap::LABEL_ACTIVE)).PHP_EOL; | |
| } else { | |
| $q .= 'REMOVE '.$identifier.':'.ucfirst(UserMap::getCamelNameForKey(UserMap::LABEL_ACTIVE)).PHP_EOL; | |
| } | |
| } | |
| if ($this->isModified(UserMap::KEY_FIRST_NAME)) { | |
| $q .= 'SET '.$identifier.'.'.UserMap::KEY_FIRST_NAME.' = {parts}.'.UserMap::KEY_FIRST_NAME.PHP_EOL; | |
| $p[UserMap::KEY_FIRST_NAME] = (string) $this->getFirstName(); | |
| } | |
| if ($this->isModified(UserMap::KEY_LAST_NAME)) { | |
| $q .= 'SET '.$identifier.'.'.UserMap::KEY_LAST_NAME.' = {parts}.'.UserMap::KEY_LAST_NAME.PHP_EOL; | |
| $p[UserMap::KEY_LAST_NAME] = (string) $this->getLastName(); | |
| } | |
| try { | |
| $parameters['parts'] = $p; | |
| Vertix::getClient()->sendCypherQuery($q, $parameters, $conn); | |
| $this->new = false; | |
| $this->modifiedLabels = []; | |
| $this->modifiedParts = []; | |
| return $this; | |
| } catch (HttpException $e) { | |
| $this->new = true; | |
| $this->id = null; | |
| unset($this->modifiedParts[UserMap::KEY_ID]); | |
| throw new \Exception($e->getMessage()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment