Beispiel #1
0
 /**
  * Public constructor
  * If an associative array is passed as an argument, hydrate object using calls fromArray().
  */
 public function __construct()
 {
     $args = func_get_args();
     // if an array or object load using fromArray
     if (!empty($args[0]) && (Validate::isAssociativeArray($args[0]) || is_object($args[0]))) {
         $this->fromArray((array) $args[0]);
     }
 }
Beispiel #2
0
 /**
  * LDAP options:
  *
  *      host *required
  *      port
  *      baseDn
  *      accountDomainName
  *      accountDomainNameShort
  *      accountCanonicalForm
  *      defaultUsername
  *      defaultPassword
  *      searchSizeLimit - Limit entries.
  *      searchTimeout - Timeout in seconds.
  *
  * @param array $options
  * @throws ServiceException
  */
 public function __construct(array $options)
 {
     if (empty($options['host'])) {
         throw new ServiceException('LDAP host required.');
     }
     $host = explode(':', $options['host']);
     unset($options['host']);
     $isIp = \NObjects\Validate::isIp($host[0]);
     // set defaults
     $this->setHost($host[0]);
     $this->setPort(self::DEFAULT_PORT);
     $this->setSsl(false);
     if (!empty($host[1])) {
         $this->setPort($host[1]);
         if ($host[1] == self::DEFAULT_PORT_SSL) {
             $this->setSsl(true);
         }
     }
     $this->setSearchSizeLimit(self::DEFAULT_SEARCH_SIZE_LIMIT);
     $this->setSearchTimeout(self::DEFAULT_SEARCH_TIMEOUT);
     $this->setNetworkTimeout(self::DEFAULT_NETWORK_TIMEOUT);
     // if host is domain name parse defaults from name
     if (!$isIp) {
         $domain = explode('.', $host[0]);
         if (count($domain) > 1) {
             $this->setBaseDn('DC=' . implode(',DC=', $domain));
             $this->setAccountDomainName($domain[count($domain) - 2] . '.' . $domain[count($domain) - 1]);
             $this->setAccountDomainNameShort($domain[count($domain) - 2]);
         }
     }
     // set options/override defaults
     if (!empty($options)) {
         $properties = $this->getProperties();
         foreach ($options as $k => $v) {
             $method = 'set' . $k;
             if (in_array($k, $properties) && method_exists($this, $method)) {
                 $this->{$method}($v);
             }
         }
     }
     // set default accountCanonicalForm if not in options
     if (!$this->getAccountCanonicalForm()) {
         if ($this->getAccountDomainNameShort()) {
             $this->setAccountCanonicalForm(self::ACCOUNT_NAME_FORM_BACKSLASHES);
         } else {
             $this->setAccountCanonicalForm(self::ACCOUNT_NAME_FORM_PRINCIPAL);
         }
     }
 }
Beispiel #3
0
 public function testIsInString()
 {
     $this->assertTrue(Validate::inString('robo', 'robotech'));
     $this->assertTrue(Validate::inString(1, '123456'));
     $this->assertFalse(Validate::inString('test', 'robotech'));
     $this->assertFalse(Validate::inString(10, '123456'));
 }
Beispiel #4
0
 /**
  * Return object as an associative array (property & value pair).
  *
  * @param callable $valueClosure
  * @return array
  */
 public function toArray($valueClosure = null)
 {
     $refl = new ReflectionClass($this);
     // Collect properties from this class as well as private property names from parents
     // which may have accessor methods.
     $props = array_unique(array_merge($refl->getProperties(), $refl->getAncestorPrivateProperties()));
     // adding support for public properties
     foreach ($this as $k => $v) {
         $props[] = (object) array('name' => $k, 'value' => $v);
     }
     $array = array();
     foreach ($props as $rp) {
         $func = $this->_getAccessorMethod($rp->name);
         // skip properties that start with an _
         // skip functions that don't exist
         if ($rp->name[0] == '_' || !method_exists($this, $func) && !isset($this->{$rp->name})) {
             continue;
         }
         // if public use temp object and don't cache
         if ($rp instanceof \ReflectionProperty) {
             if ($rp->isPublic()) {
                 $val = $rp->getValue($this);
             } else {
                 $val = $this->{$func}();
             }
         } else {
             $val = $rp->value;
         }
         $base = __CLASS__;
         switch (true) {
             // is NObjects\Object instance
             case $val instanceof $base:
                 $array[$rp->name] = $val->toArray($valueClosure);
                 break;
                 // intercept array types (ArrayObject, Collection) and convert $val to array
             // intercept array types (ArrayObject, Collection) and convert $val to array
             case method_exists($val, 'toArray'):
                 $val = $val->toArray();
                 break;
             case $val instanceof \ArrayObject:
                 $val = $val->getArrayCopy();
                 break;
             default:
                 $array[$rp->name] = self::valueClosure($valueClosure, $val);
                 break;
         }
         // do extra work when $val is an array
         if (is_array($val)) {
             foreach ($val as $k => $v) {
                 if ($v instanceof $base) {
                     $val[$k] = $v->toArray($valueClosure);
                 } else {
                     if (\NObjects\Validate::isAssociativeArray($v)) {
                         $newObj = new \NObjects\Object($v);
                         $val[$k] = $newObj->toArray($valueClosure);
                     } else {
                         $val[$k] = self::valueClosure($valueClosure, $v);
                     }
                 }
             }
             $array[$rp->name] = $val;
         }
     }
     return $array;
 }