Example #1
0
 /**
  * Creates a new record object with default values.
  *
  * @param array $config Possible options are:
  *                      - `'exists'`     _boolean_: A boolean or `null` indicating if the entity exists.
  *                      - `'autoreload'` _boolean_: If `true` and exists is `null`, autoreload the entity
  *                                                  from the datasource
  *
  */
 public function __construct($config = [])
 {
     $defaults = ['exists' => false, 'autoreload' => true, 'data' => []];
     $config += $defaults;
     parent::__construct($config);
     /**
      * Cached value indicating whether or not this instance exists somehow. If this instance has been loaded
      * from the database, or has been created and subsequently saved this value should be automatically
      * setted to `true`.
      *
      * @var Boolean
      */
     $this->exists($config['exists']);
     if ($this->exists() === false) {
         return;
     }
     if ($this->exists() !== true) {
         if ($config['autoreload']) {
             $this->reload();
         }
         $this->set($config['data']);
     }
     $this->_persisted = $this->_data;
     if ($this->exists() !== true) {
         return;
     }
     if (!($id = $this->id())) {
         throw new ChaosException("Existing entities must have a valid ID.");
     }
     $source = $this->schema()->source();
     $this->uuid($source . ':' . $id);
 }
Example #2
0
            $document = new Document();
            expect($document->valid())->toBe(false);
            $data = ['id' => 1, 'title' => 'test record', 'body' => 'test body'];
            $document = new Document(['data' => $data]);
            expect($document->valid())->toBe(true);
        });
    });
    describe("->count()", function () {
        it("returns 0 on empty", function () {
            $document = new Document();
            expect($document)->toHaveLength(0);
        });
        it("returns the number of items in the collection", function () {
            $data = ['id' => 1, 'title' => 'test record', 'body' => 'test body', 'enabled' => true, 'null' => null, 'onject' => new stdClass()];
            $document = new Document(['data' => $data]);
            expect($document)->toHaveLength(6);
        });
    });
    describe("->to('array')", function () {
        it("exports into an array", function () {
            $data = ['id' => 1, 'title' => 'test record'];
            $document = new Document(['data' => $data]);
            expect($document->to('array'))->toBe($data);
        });
        it("exports nested relations", function () {
            $data = ['name' => 'amiga_1200.jpg', 'title' => 'Amiga 1200', 'tags' => [['name' => 'tag1']]];
            $image = new Document(['data' => $data]);
            expect($image->data())->toEqual($data);
        });
    });
});