示例#1
0
 /**
  * Builds the relationship getters
  * @return string
  */
 private function buildRelationshipGetters()
 {
     $buffer = '';
     foreach ($this->getRelationships() as $relationship) {
         $field = $this->getField($relationship->getLocalColumn());
         if ($relationship->isPlural()) {
             $method = new Object\Method();
             $method->setAccess('public');
             $method->setDescription('Gets the related ' . $relationship->getAlias());
             $method->setName('get' . ucfirst($relationship->getAlias()));
             $method->setReturnType('ResultSet|Child' . $relationship->getRemoteShortModel() . '[]');
             $parameter = new Object\Parameter();
             $parameter->setDescription('');
             $parameter->setName('parameters');
             $parameter->setDefaultValue('null');
             $parameter->setType('array');
             $method->addParameter($parameter);
             $method->setContent('return $this->getRelated(\'' . $relationship->getAlias() . '\', $parameters);');
             $this->getAbstractClass()->addMethod($method);
             $method = new Object\Method();
             $method->setAccess('public');
             $method->setDescription('Gets the count of related ' . $relationship->getAlias());
             $method->setName('count' . ucfirst($relationship->getAlias()));
             $method->setReturnType('int');
             $method->setContent('return $this->get' . ucfirst($relationship->getAlias()) . '()->count();');
             $this->getAbstractClass()->addMethod($method);
         } else {
             $method = new Object\Method();
             $method->setAccess('public');
             $method->setDescription('Gets the related ' . $relationship->getAlias());
             $method->setName('get' . ucfirst($relationship->getAlias()));
             if ($this->getAbstractClass()->hasMethod($method->getName())) {
                 $method->setName('getRelated' . ucfirst($relationship->getAlias()));
             }
             $method->setReturnType('Child' . $relationship->getRemoteShortModel() . '|false');
             $method->setContent('return $this->getRelated(\'' . $relationship->getAlias() . '\');');
             $this->getAbstractClass()->addMethod($method);
             if ($field->isNullable()) {
                 $method = new Object\Method();
                 $method->setAccess('public');
                 $method->setDescription('Checks if a relationship exists');
                 $method->setName('has' . ucfirst($relationship->getAlias()));
                 $method->setReturnType('bool');
                 $method->setContent('return !is_null($this->get' . ucfirst($relationship->getAlias()) . '());');
                 $this->getAbstractClass()->addMethod($method);
             }
         }
     }
     return $buffer;
 }
示例#2
0
 /**
  * Adds a new method
  *
  * @param Method $method
  *
  * @return void
  * @throws \Exception
  */
 public function addMethod(Method $method)
 {
     $methods = $this->getMethods();
     $key = $method->getName();
     if (array_key_exists($key, $methods)) {
         throw new \Exception('A Method with the name of: ' . $method->getName() . ' already exists');
     }
     $methods[$key] = $method;
     ksort($methods);
     $this->setMethods($methods);
 }