Skip to content

Instantly share code, notes, and snippets.

@jamesmoey
Last active March 6, 2019 23:20
Show Gist options
  • Save jamesmoey/0552c5c811ffd387149f18e8699ee455 to your computer and use it in GitHub Desktop.
Save jamesmoey/0552c5c811ffd387149f18e8699ee455 to your computer and use it in GitHub Desktop.
Setter & Getter, Adder & Remover live template for Doctrine Collection. Use in IntellJ
<templateSet group="Custom">
<template name="sgcollection" value="/**&#10; * @return $CLASS$&#10; */&#10;public function get$CLASS$(): $CLASS$&#10;{&#10; return $this-&gt;$VAR$;&#10;}&#10;&#10;/**&#10; * @param $CLASS$ $$$VAR$&#10; * @return $this&#10; */&#10;public function set$CLASS$($CLASS$ $$$VAR$)&#10;{&#10; if ($$$VAR$ !== $this-&gt;$VAR$) {&#10; if ($this-&gt;$VAR$ !== null) {&#10; $this-&gt;$VAR$-&gt;remove$RELATECLASS$($this);&#10; }&#10; $this-&gt;$VAR$ = $$$VAR$;&#10; if ($this-&gt;$VAR$ !== null) {&#10; $this-&gt;$VAR$-&gt;add$RELATECLASS$($this);&#10; }&#10; }&#10; return $this;&#10;}" description="Generate Setter &amp; Getter for Relationship" toReformat="true" toShortenFQNames="true">
<variable name="CLASS" expression="classNameComplete()" defaultValue="" alwaysStopAt="true" />
<variable name="VAR" expression="phpVar" defaultValue="" alwaysStopAt="true" />
<variable name="RELATECLASS" expression="classNameComplete()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP" value="true" />
</context>
</template>
<template name="arcollection" value="/**&#10; * @param $CLASS$ $item&#10; * @return $this&#10; */&#10;public function add$CLASS$($CLASS$ $item) {&#10; if (!$this-&gt;$VAR$-&gt;contains($item)) {&#10; $this-&gt;$VAR$-&gt;add($item);&#10; $item-&gt;set$METHOD$($this);&#10; }&#10; return $this;&#10;}&#10;&#10;/**&#10; * @param $CLASS$ $item&#10; * @return $this&#10; */&#10;public function remove$CLASS$($CLASS$ $item) {&#10; if ($this-&gt;$VAR$-&gt;contains($item)) {&#10; $this-&gt;$VAR$-&gt;removeElement($item);&#10; $item-&gt;set$METHOD$(null);&#10; }&#10; return $this;&#10;}" description="Generate Adder &amp; Remover Collection" toReformat="true" toShortenFQNames="true">
<variable name="CLASS" expression="classNameComplete()" defaultValue="" alwaysStopAt="true" />
<variable name="VAR" expression="phpVar" defaultValue="" alwaysStopAt="true" />
<variable name="METHOD" expression="classNameComplete()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP" value="true" />
</context>
</template>
<template name="one2one" value="/**&#10; * @return $CLASS$&#10; */&#10;public function get$CLASS$(): $CLASS$&#10;{&#10; return $this-&gt;$VAR$;&#10;}&#10;&#10;/**&#10; * @param $CLASS$ $$$VAR$&#10; * @return $this&#10; */&#10;public function set$CLASS$($CLASS$ $$$VAR$)&#10;{&#10; if ($$$VAR$ !== $this-&gt;$VAR$) {&#10; $prev = $this-&gt;$VAR$;&#10; $this-&gt;$VAR$ = $$$VAR$;&#10; if ($prev !== null) {&#10; $prev-&gt;set$RELATECLASS$(null);&#10; }&#10; if ($this-&gt;$VAR$ !== null) {&#10; $this-&gt;$VAR$-&gt;set$RELATECLASS$($this);&#10; }&#10; }&#10; return $this;&#10;}" description="Generate Setter &amp; Getter for One To One Relationship" toReformat="true" toShortenFQNames="true">
<variable name="CLASS" expression="classNameComplete()" defaultValue="" alwaysStopAt="true" />
<variable name="VAR" expression="phpVar" defaultValue="" alwaysStopAt="true" />
<variable name="RELATECLASS" expression="classNameComplete()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP" value="true" />
</context>
</template>
</templateSet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment