function it_should_attach_custom_data_to_my_spl()
 {
     $o1 = new \StdClass();
     Object::set($o1, 'name', 'Alan');
     $o2 = new \StdClass();
     Object::set($o2, 'name', 'John');
     $o3 = new \StdClass();
     Object::set($o3, 'name', 'Ringo');
     $spl = new \SplObjectStorage();
     $spl->attach($o1, "I am DATA 1");
     $spl->attach($o2, "I am DATA 2");
     $spl->attach($o3, "I am DATA 3");
     foreach ($spl as $value) {
         $spl->getInfo();
     }
 }
示例#2
0
 public function testFindBy()
 {
     $a = array((object) array('id' => 123, 'name' => 'foo', 'group' => 'primary', 'value' => 123456), (object) array('id' => 456, 'name' => 'bar', 'group' => 'primary', 'value' => 1468), (object) array('id' => 499, 'name' => 'baz', 'group' => 'secondary', 'value' => 2365), (object) array('id' => 789, 'name' => 'ter', 'group' => 'primary', 'value' => 2468));
     $b = Object::findBy($a, 'name', 'baz');
     $this->assertInstanceOf('\\stdClass', $b);
     $this->assertEquals(2365, $b->value);
     $this->assertObjectHasAttribute("name", $b);
     $this->assertObjectHasAttribute("group", $b);
     $this->assertObjectHasAttribute("value", $b);
     $c = Object::findBy($a, 'value', 2468);
     $this->assertInstanceOf('\\stdClass', $c);
     $this->assertEquals("primary", $c->group);
     $d = Object::findBy($a, 'group', 'primary');
     $this->assertInstanceOf('\\stdClass', $d);
     $this->assertEquals("foo", $d->name);
     $e = Object::findBy($a, 'value', 2000, 'lt');
     $this->assertInstanceOf('\\stdClass', $e);
     $this->assertEquals(1468, $e->value);
 }
示例#3
0
 public function testCanSetAnGetValues()
 {
     $object = $this->object;
     $getset = Object::setAndGet($object, 'set', 'get');
     $get = Object::get($object, 'set');
     $this->assertEquals($getset, 'get');
     $this->assertEquals($get, $getset);
 }
示例#4
0
 function validate_format()
 {
     return Object::values(Arrays::clean(Arrays::each($this->values(), function ($value, $key) {
         $field = $this->get_fields()[$key];
         if (empty($value)) {
             if (isset($field['required']) && $field['required']) {
                 return $this->build_error('invalid', array('%field%' => $field['label']));
             }
         } else {
             switch ($field['type']) {
                 case 'string':
                     if (isset($field['maxlength'])) {
                         $maxlength = $field['maxlength'];
                         if (strlen($value) > $maxlength) {
                             return $this->build_error('invalid', array('%field%' => $field['label'], '%maxlength%' => $maxlength));
                         }
                     }
                     break;
                 case 'email':
                     if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
                         return $this->build_error('invalid', array('%field%' => $field['label']));
                     }
                     break;
                 case 'number':
                     if (!is_numeric($value)) {
                         return $this->build_error('invalid', array('%field%' => $field['label']));
                     } else {
                         $value = intval($value);
                         if (isset($field['minvalue']) && isset($field['maxvalue'])) {
                             $min = $field['minvalue'];
                             $max = $field['maxvalue'];
                             if ($value < $min || $max < $value) {
                                 return $this->build_error('range', array('%field%' => $field['label'], '%minvalue%' => $min, '%maxvalue%' => $max));
                             }
                         } else {
                             if (isset($field['minvalue'])) {
                                 $min = $field['minvalue'];
                                 if ($value < $min) {
                                     return $this->build_error('minvalue', array('%field%' => $field['label'], '%minvalue%' => $min));
                                 }
                             } else {
                                 if (isset($field['maxvalue'])) {
                                     $max = $field['maxvalue'];
                                     if ($max < $value) {
                                         return $this->build_error('maxvalue', array('%field%' => $field['label'], '%maxvalue%' => $max));
                                     }
                                 } else {
                                     if (isset($field['select'])) {
                                         if (!Arrays::contains($field['select'], $value)) {
                                             return $this->build_error('select', array('%field%' => $field['label'], '%select%' => implode(', ', $field['select'])));
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     break;
             }
         }
     })));
 }
示例#5
0
 /**
  * Resolve value by uses as attribute as raw.
  *
  * @param  \SimpleXMLElement  $content
  * @param  string  $use
  * @param  mixed  $default
  *
  * @return mixed
  */
 protected function getRawValueAttribute(SimpleXMLElement $content, $use, $default = null)
 {
     list($value, $attribute) = explode('::', $use, 2);
     $parent = $content;
     if (!empty($value) && is_null($parent = Object::get($content, $value))) {
         return $default;
     }
     $attributes = $parent->attributes();
     return Arrays::get($attributes, $attribute, $default);
 }