Bolt\Legacy\Storage::getContentObject PHP 메소드

getContentObject() 공개 메소드

Get an object for the content of a specific contenttype. This will be \Bolt\Legacy\Content, unless the contenttype defined another class to be used.
public getContentObject ( array | string $contenttype, array $values = [] ) : Content
$contenttype array | string
$values array
리턴 Content
    public function getContentObject($contenttype, $values = [])
    {
        // Make sure $contenttype is an array, and not just the slug.
        if (!is_array($contenttype)) {
            $contenttype = $this->getContentType($contenttype);
        }
        // If the contenttype has a 'class' specified, and the class exists,
        // Initialize the content as an object of that class.
        if (!empty($contenttype['class']) && class_exists($contenttype['class'])) {
            $content = new $contenttype['class']($this->app, $contenttype, $values);
            // Check if the class actually extends \Bolt\Legacy\Content.
            if (!$content instanceof Content) {
                throw new \Exception($contenttype['class'] . ' does not extend \\Bolt\\Legacy\\Content.');
            }
        } else {
            $content = new Content($this->app, $contenttype, $values);
        }
        return $content;
    }

Usage Example

예제 #1
0
 public function testGetContentObject()
 {
     $app = $this->getApp();
     $storage = new Storage($app);
     $content = $storage->getContentObject('pages');
     $this->assertInstanceOf('Bolt\\Legacy\\Content', $content);
     $fields = $app['config']->get('contenttypes/pages/fields');
     $mock = $this->getMock('Bolt\\Legacy\\Content', null, [$app], 'Pages');
     $content = $storage->getContentObject(['class' => 'Pages', 'fields' => $fields]);
     $this->assertInstanceOf('Pages', $content);
     $this->assertInstanceOf('Bolt\\Legacy\\Content', $content);
     // Test that a class not instanceof Bolt\Legacy\Content fails
     $mock = $this->getMock('stdClass', null, [], 'Failing');
     $this->setExpectedException('Exception', 'Failing does not extend \\Bolt\\Legacy\\Content.');
     $content = $storage->getContentObject(['class' => 'Failing', 'fields' => $fields]);
 }
All Usage Examples Of Bolt\Legacy\Storage::getContentObject