Пример #1
0
 public function testRender()
 {
     $element = new Select();
     $this->assertEquals("<select>\n</select>", $element->render());
     $element->setAttribute('id', 'test');
     $this->assertEquals("<select id=\"test\">\n</select>", $element->render());
     $element->setAttribute('class', 'red');
     $this->assertEquals("<select id=\"test\" class=\"red\">\n</select>", $element->render());
     $element->setOption('values', [10 => 'foo', 20 => 'bar']);
     $this->assertEquals("<select id=\"test\" class=\"red\">\n<option value=\"10\">foo</option>\n<option value=\"20\">bar</option>\n</select>", $element->render());
     $element = new Select('category');
     $element->setOption('valueColumn', 'id');
     $collection = new Collection();
     $el1 = new DummyModel();
     $el1->id = 100;
     $el1->title = 'test100';
     $collection->append($el1);
     $el2 = new DummyModel();
     $el2->id = 200;
     $el2->title = 'test200';
     $collection->append($el2);
     $element->setOption('values', $collection);
     $this->assertEquals("<select name=\"category\">\n<option value=\"100\">test100</option>\n<option value=\"200\">test200</option>\n</select>", $element->render());
     $element->setAttribute('id', 'test');
     $this->assertEquals("<select name=\"category\" id=\"test\">\n<option value=\"100\">test100</option>\n<option value=\"200\">test200</option>\n</select>", $element->render());
     $element->setOption('null', true);
     $this->assertEquals("<select name=\"category\" id=\"test\">\n<option value=\"0\">----</option>\n<option value=\"100\">test100</option>\n<option value=\"200\">test200</option>\n</select>", $element->render());
     $element->setSelected(200);
     $this->assertEquals("<select name=\"category\" id=\"test\">\n<option value=\"0\">----</option>\n<option value=\"100\">test100</option>\n<option value=\"200\" selected=\"selected\">test200</option>\n</select>", $element->render());
 }
Пример #2
0
 public function testReduce()
 {
     $collection = new Collection([1, 2, 3, 4]);
     $reduced = $collection->reduce(0, function ($carry, $item) {
         return $carry + $item;
     });
     $this->assertEquals(10, $reduced);
 }
Пример #3
0
 public function findAllByQuery($class, $query, $params = [])
 {
     if ($query instanceof QueryBuilder) {
         $params = $query->getParams();
         $query = $query->getQuery($this);
     }
     $result = $class::getDbConnection()->query($query, $params)->fetchAll(\PDO::FETCH_CLASS, $class);
     if (!empty($result)) {
         $ret = new Collection($result);
         $ret->setNew(false);
     } else {
         $ret = new Collection();
     }
     return $ret;
 }
Пример #4
0
 /**
  * @param string $key
  * @param array $options
  * @return mixed
  * @throws Exception
  */
 public function getRelationLazy($key, $options = [])
 {
     /** @var \T4\Orm\Model $class */
     $class = get_class($this);
     $relations = $class::getRelations();
     if (empty($relations[$key])) {
         throw new Exception('No such column or relation: ' . $key . ' in model of ' . $class . ' class');
     }
     $relation = $relations[$key];
     switch ($relation['type']) {
         case $class::BELONGS_TO:
             /** @var \T4\Orm\Model $relationClass */
             $relationClass = $relation['model'];
             $thisColumnName = $class::getRelationLinkName($relation);
             $thatColumnName = $class::getBelongsToThatLinkColumnName($relation);
             $subModel = $relationClass::findByColumn($thatColumnName, $this->{$thisColumnName}, $options);
             if (empty($subModel)) {
                 return null;
             } else {
                 return $subModel;
             }
             break;
         case $class::HAS_ONE:
             /** @var \T4\Orm\Model $relationClass */
             $relationClass = $relation['model'];
             $thisColumnName = $class::getHasOneThisLinkColumnName($relation);
             $thatColumnName = $class::getRelationLinkName($relation);
             $subModel = $relationClass::findByColumn($thatColumnName, $this->{$thisColumnName}, $options);
             if (empty($subModel)) {
                 return null;
             } else {
                 return $subModel;
             }
             break;
         case $class::HAS_MANY:
             /** @var \T4\Orm\Model $relationClass */
             $relationClass = $relation['model'];
             $thisColumnName = $class::getHasManyThisLinkColumnName($relation);
             $thatColumnName = $class::getRelationLinkName($relation);
             return $relationClass::findAllByColumn($thatColumnName, $this->{$thisColumnName}, $options);
             break;
         case $class::MANY_TO_MANY:
             /** @var \T4\Orm\Model $relationClass */
             $relationClass = $relation['model'];
             $linkTable = $class::getRelationLinkName($relation);
             $pivots = $relationClass::getPivots($class, $key);
             if (!empty($pivots)) {
                 $pivotColumnsSql = ', ' . implode(', ', array_map(function ($x) {
                     return 'j1.' . $x;
                 }, array_keys($pivots)));
             } else {
                 $pivotColumnsSql = '';
             }
             $query = new Query();
             $query->select('t1.*' . $pivotColumnsSql)->from($relationClass::getTableName())->join($linkTable, 't1.' . $relationClass::PK . '=j1.' . static::getManyToManyThatLinkColumnName($relation), 'left')->where('(j1.' . static::getManyToManyThisLinkColumnName($relation) . '=:id)' . (isset($options['where']) ? ' AND (' . $options['where'] . ')' : ''));
             if (isset($options['order'])) {
                 $query->order($options['order']);
             }
             $query->params([':id' => $this->getPk()]);
             /** @var \T4\Orm\Model $relationClass */
             $result = $relationClass::getDbConnection()->query($query)->fetchAllObjects($relationClass);
             if (!empty($result)) {
                 $ret = new Collection($result);
                 $ret->setNew(false);
                 return $ret;
             } else {
                 return new Collection();
             }
     }
 }
Пример #5
0
 public function findAllByQuery($class, $query, $params = [])
 {
     if ($query instanceof QueryBuilder) {
         $params = $query->getParams();
         $query = $query->getQuery($this);
     }
     if ($query instanceof Query) {
         $params = array_merge($params, $query->params);
         $query = $this->makeQueryString($query);
     }
     /** @var \T4\Orm\Model $class */
     $result = $class::getDbConnection()->query($query, $params)->fetchAllObjects($class);
     if (!empty($result)) {
         $ret = new Collection($result);
         $ret->setNew(false);
     } else {
         $ret = new Collection();
     }
     return $ret;
 }
Пример #6
0
 protected static function getAllChildrenByIndex(Collection $tree, $index)
 {
     $lvl = $tree[$index]->__lvl;
     $tail = array_slice($tree->getArrayCopy(), $index + 1);
     $ret = [];
     foreach ($tail as $el) {
         if ($el->__lvl <= $lvl) {
             break;
         }
         $ret[] = $el;
     }
     return new Collection($ret);
 }
Пример #7
0
 public function testArrayable()
 {
     $collection = new Collection();
     $collection[] = 1;
     $collection[] = 2;
     $collection[] = 3;
     $this->assertEquals([1, 2, 3], $collection->toArray());
     $collection1 = new Collection();
     $collection1->fromArray([3, 4, 5]);
     $collection2 = new Collection();
     $collection2[] = 3;
     $collection2[] = 4;
     $collection2[] = 5;
     $this->assertEquals($collection1, $collection2);
 }
Пример #8
0
 public function testGroup()
 {
     $collection = new Collection([['date' => '2000-01-01', 'title' => 'First'], ['date' => '2000-01-01', 'title' => 'Second'], ['date' => '2000-01-02', 'title' => 'Third'], ['date' => '2000-01-04', 'title' => 'Fourth']]);
     $grouped = $collection->group('date');
     $this->assertEquals(new Collection(['2000-01-01' => new Collection([['date' => '2000-01-01', 'title' => 'First'], ['date' => '2000-01-01', 'title' => 'Second']]), '2000-01-02' => new Collection([['date' => '2000-01-02', 'title' => 'Third']]), '2000-01-04' => new Collection([['date' => '2000-01-04', 'title' => 'Fourth']])]), $grouped);
     $grouped = $collection->group(function ($x) {
         return date('m-d', strtotime($x['date']));
     });
     $this->assertEquals(new Collection(['01-01' => new Collection([['date' => '2000-01-01', 'title' => 'First'], ['date' => '2000-01-01', 'title' => 'Second']]), '01-02' => new Collection([['date' => '2000-01-02', 'title' => 'Third']]), '01-04' => new Collection([['date' => '2000-01-04', 'title' => 'Fourth']])]), $grouped);
 }